简体   繁体   中英

Enabling And Disabling Buttons In GridView

I have added a button column to a gridview. I am populating the gridview with a datable through code then binding the datable to the grid.

I need to now look at the first column of the data and check if the text of the column is "NA" if it is the button in that column has to be disabled.....

How can I accomplish this? I am populating the data from code and the button is preadded 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();

The best thing to do is implement the OnDataBinding method for a Button in a TemplateColumn .

Eg:

<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 codebehind implement your logic:

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

The advantage to doing it in this manner over the other posted answers:

  1. No code in your markup
  2. Code is localized to the Button control and can be reused if other Buttons require the same functionality.
  3. Comparing to the DataSource value and not what the visual output is (this could tie into business logic for both rendering and checking).

Hope that helps.

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'll have to modify the code for your application.

Don't forget to add OnDataBound="GridView1_DataBound" to the markup for the GridView.

Maybe something like this. Did the button a little differently

<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'll want to you Eval("someColumn") most likely instead of GridView1.Rows[0].Cells[1].Text

you can try from markup as,

 <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>

也许您可以使用OnRowDataBound =“GridViewRowEventHandler”将值设置为启用或禁用?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM