How do I disable buttons in a gridview using C#?

I have a gridview which has a command button on it.  I would like to disable the command button if certain criteria is met.  For example if the datakey for that row is "A" then disable button else button is enabled for subsequent action.  Can this even be done?
mbartAsked:
Who is Participating?
 
Pratima PharandeConnect With a Mentor Commented:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <Columns >
       
            <asp:CommandField ShowEditButton="True"  />
        </Columns>
        </asp:GridView>

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (GridView1.DataKeys[e.Row.RowIndex].Value == "A")
            {
                CommandField objComField =
       (CommandField)e.Row.Cells[0].Controls[0];
                objComField.Visible = false;
            }
       
        }

       
    }
0
 
lucky_jamesCommented:
it would be good if you handle the same using client side scripting eg. javascript
for c# to handle these things you would be making a server call, and if you can avoid that, it is good.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this can be done, in the ItemDataBound event.
the following article shows how to change the backcolor for certain rows:
http://odetocode.com/Articles/225.aspx

what you have to do instead, is that you have to use e.Item.FindControl("your link button") and disable it when the corresponding value is A
0
 
monarch_ilhanCommented:

<asp:TemplateField>
	<ItemTemplate>
<asp:Button runat="server" ID="btn1" Text="Create" Enabled='<%# DataBinder.Eval(Container, "DataItem.FL_STATUS").ToString()=="A" %>' />
	</ItemTemplate>
</asp:TemplateField>

Open in new window

0
 
mbartAuthor Commented:
This is exactly what I am looking for, thank you so much for the quick response.
0
All Courses

From novice to tech pro — start learning today.