Turning buttons on and off in a gridview

I added a button column to the gridview. I populate the gridview with numbered code and then snap it to the grid.

I need now to look at the first data column and check if the text of the column is β€œNA” if the button in this column should be disabled .....

How can i do this? I fill in the data from the code, and the button is pre-added to the grid in the markup

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:ButtonField Text="Delete" /> </Columns> </asp:GridView> GridView1.DataSource = dt; GridView1.DataBind(); 
+4
source share
5 answers

It is best to implement the OnDataBinding method for Button in TemplateColumn .

For instance:

 <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Button runat="server" ID="btnDelete" CommandName="Delete" Text="Delete" OnDataBinding="btnDelete_DataBinding" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

Then in your code, follow your logic:

 protected void btnDelete_DataBinding(object sender, System.EventArgs e) { Button btn = (Button)(sender); btn.Enabled = !Eval("TheFieldInYourDataSourceToCompare").ToString().Equals("NA"); } 

An advantage for this in this regard over other published answers:

  • There is no code in your markup
  • The code is localized in the Button control and can be reused if other Buttons require the same functionality.
  • Comparison with the value of the DataSource , not the visual result (this may be due to business logic for both rendering and validation).

Hope this helps.

+5
source

Try this in the DataBound event handler:

 protected void GridView1_DataBound(object sender, EventArgs e) { for (int i = 0; i < GridView1.Rows.Count; i++) { if (GridView1.Rows[i].Cells[1].Text == "NA") { // Disable the button } } } 

This is just a general idea. You will need to change the code for your application.

Remember to add OnDataBound="GridView1_DataBound" to the layout for the GridView.

+1
source

Maybe something like this. Whether the button was a little different.

 <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Button runat="server" ID="Btn_Delete" CommandName="Delete" Text="delete" Enabled='<%# GridView1.Rows[0].Cells[1].Text != "NA" %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

You most likely want Eval ("someColumn") instead of GridView1.Rows [0] .Cells [1] .Text

+1
source

Perhaps you can use OnRowDataBound = "GridViewRowEventHandler" to set the value to enable or disable?

0
source

you can try from markup like

  <asp:TemplateField HeaderText="QA signature"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Column6") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Eval("Column6") %>' Visible='<%# Eval("Column6") != "" %>' ></asp:Label> <asp:Button ID="Button2" runat="server" Text="Sign Off" CssClass="cmdButton" Visible='<%# Eval("Column6") == "" %>' /> </ItemTemplate> </asp:TemplateField> 
0
source

All Articles