简体   繁体   English

在GridView中启用和禁用按钮

[英]Enabling And Disabling Buttons In GridView

I have added a button column to a gridview. 我在gridview中添加了一个按钮列。 I am populating the gridview with a datable through code then binding the datable to the grid. 我正在使用数据通过代码填充gridview,然后将数据绑定到网格。

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..... 我现在需要查看数据的第一列,并检查列的文本是否为“NA”,如果它是该列中的按钮必须被禁用.....

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 . 最好的办法是在TemplateColumnButton实现OnDataBinding方法。

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. 代码已本地化为Button控件,如果其他Buttons需要相同的功能,则可以重复使用。
  3. Comparing to the DataSource value and not what the visual output is (this could tie into business logic for both rendering and checking). DataSource值相比,而不是视觉输出(这可能与渲染和检查的业务逻辑相关)。

Hope that helps. 希望有所帮助。

Try this in the DataBound event handler: 在DataBound事件处理程序中尝试此操作:

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. 不要忘记将OnDataBound="GridView1_DataBound"添加到OnDataBound="GridView1_DataBound"的标记中。

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 你最想要的是Eval(“someColumn”)而不是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”将值设置为启用或禁用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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