开发者

How to disable a control in command field control in gridview

how to find a command field control in the gridview.

in a method not in the row data bound.

so far i have used this coding but i cant find the control.

<asp:CommandField Butt开发者_如何学GoonType="Image" ShowEditButton="True
                  HeaderText="Enter Leave"
                  EditImageUrl="~/IMAGES/edit-icon.gif">
    <ItemStyle HorizontalAlign="Center" />
</asp:CommandField>

source code:

ImageButton edit = (ImageButton)EmployeeDetails.FindControl("Image");
edit.Enabled = false;


You can disable column itself with,

GridView1.AutoGenerateEditButton = false;

from code behind pages.


Or you can use ItemTemplate instead of CommandField,

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton runat="server" ID="id" CommandName="Edit" Text="Edit" />
    </ItemTemplate>
</asp:TemplateField>

And at code behind you can iterate through rows of GridView and disable each LinkButton.

foreach(GridViewRow gvr in GridView1.Rows)
{
    LinkButton row = gvr.FindControl("id") as LinkButton;
    row.Enabled = false;
} 

First Edit :

I tried my second solution and it works. However, make sure your GridView is filled before you use foreach. Otherwise, GridView.Rows.Count would probably be 0.


Second Edit :

This works for CommandField too. Replace 0 with the location of CommandField in your GridView.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{        
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
         e.Row.Cells[0].Enabled = false;
    }
}


You miss to specify the row

Something like :

ImageButton edit = (ImageButton)EmployeeDetails.Rows[0].Cells[0].FindControl("Image");
edit.Enabled = false;

If you want to disable the column that contains the imageButton , you can do :

EmployeeDetails.Columns[0].Visible = false;


Try this:

try to hide controls at DataBound or RowDataBound event of GridView

protected void EmployeeDetails_DataBound(object sender, EventArgs e)
{
        ImageButton edit = (ImageButton)EmployeeDetails.Row.Cells[0].FindControl("Image");
        edit.Visible = false;  
        edit.Enabled = false; //OR use this line
}

particular column can be disabled in the following way

EmployeeDetails.Columns[0].Visible = false;

Hope this helps.


I had a similar issue. I simply disabled the view of the Column in BindData() function.

GridView1.Columns[0].Visible = false;

This worked for me, since my first column was Edit column and I have to enable it for specific users only.

Good luck!


Cast it as a DataControlFieldCell and then set Enabled to false.

Where: row.Controls[0] is your CommandField control

foreach (GridViewRow row in ManageDNXGridView.Rows)
{
    DataControlFieldCell editable = (DataControlFieldCell)row.Controls[0];
    editable.Enabled = false; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜