【问题标题】:Enabling /Disabling ButtonField of GridView using Checkbox使用复选框启用/禁用 GridView 的 ButtonField
【发布时间】:2014-03-24 09:41:47
【问题描述】:

我有网格视图

  • 一列是 ItemTemplate 列,它具有 Checkbox 字段。
  • 其他 2 列是数据绑定列。其中一列是 ButtonField,属于 Button 类型。

我希望此按钮最初设置为禁用模式

选中复选框后,它应该启用该特定行按钮字段。有人可以帮忙吗?

我的示例尝试

.aspx 文件

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Email_NotificationConnection %>" 
        SelectCommand="SELECT [Customer_Name] FROM [Customer]"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" EnableModelValidation="True">
        <Columns>
            <asp:BoundField DataField="Customer_Name" HeaderText="Customer_Name" 
                SortExpression="Customer_Name" />
            <asp:TemplateField>

            <ItemTemplate>
            <asp:CheckBox runat="server" ID="non_prod_all_select" OnCheckedChanged="CheckBox2_CheckedChanged1"  />
                                        </ItemTemplate>
                                      <HeaderStyle Width="30px" /></asp:TemplateField>
            <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Button" />
        </Columns>
    </asp:GridView>

.aspx.cs 文件

  protected void CheckBox2_CheckedChanged1(Object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    GridViewRow gridrow = ((GridViewRow)(chk.Parent));
    if (chk.Checked)
    {
        Button btn = (Button)(gridrow.FindControl("Button"));
        btn.Enabled = true;
    }
    else
    {
        Button btn = (Button)(gridrow.FindControl("Button"));
        btn.Enabled = false;
    }
}

【问题讨论】:

  • 你试过什么???什么是.aspx??? .aspx.cs...??
  • @Ganesh_Devlekar 我什么都没试过。请帮忙
  • 尝试一下并提出错误队友我们在这里帮助您不要为您的队友编写代码
  • @Amitesh 添加了代码。
  • 这段代码哪里出错了

标签: asp.net gridview c#-3.0


【解决方案1】:

尝试使用以下代码:

GridView1 的 ASPX 代码:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Email_NotificationConnection %>" 
        SelectCommand="SELECT [Customer_Name] FROM [Customer]"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" EnableModelValidation="True">
        <Columns>
            <asp:BoundField DataField="Customer_Name" HeaderText="Customer_Name" 
                SortExpression="Customer_Name" />
            <asp:TemplateField>

            <ItemTemplate>
            <asp:CheckBox runat="server" AutoPostBack="true" ID="non_prod_all_select" OnCheckedChanged="CheckBox2_CheckedChanged1"  />
            </ItemTemplate>
             <HeaderStyle Width="30px" /></asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>

代码隐藏(用于 CheckBox Check Changed 事件处理程序):

protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView3.Rows)
            {
                ((Button)row.FindControl("Button1")).Enabled = ((CheckBox)row.FindControl("non_prod_all_select")).Checked;

            }
         }

所做的更改:

1.将 CheckBox 的 AutoPostBack 设置为 true。

2.去掉了Button Field,在Grid的第三列添加了一个带有button的模板字段(这样asp:Button控件在后面的代码中可以很容易的读懂)

3.更改代码背后的代码以执行必要的操作。

注意:我已在本地检查此代码,并且按预期工作。所以只要用这个替换你的旧代码,如果有任何问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    相关资源
    最近更新 更多