【问题标题】:CheckBox Gridview Enable and DisableCheckBox Gridview 启用和禁用
【发布时间】:2011-11-13 22:03:53
【问题描述】:

我有一个网格视图,其中复选框开始禁用。当我单击网格视图中的编辑按钮时,我想启用它们。这是标记

<asp:GridView ID="grd_Bookcode" runat="server" DataSourceID="sqldatasource1" 
autogeneratecolumns="False" onrowcommand="grd_Bookcode_RowCommand1" 
onrowdatabound="grd_Bookcode_RowDataBound">
<Columns>
    <asp:BoundField DataField="BookCode" HeaderText="Book Code"/>
    <asp:BoundField DataField="mag_name" HeaderText="Name"/>
    <asp:BoundField DataField="display_date" HeaderText="Display Date"/>
   <asp:TemplateField HeaderText = "PC">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("82_PC").ToString() == "1" ? true:false %>' Enabled="false" />
        </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="eReader">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Eval("83_eReader").ToString() == "1" ? true:false %>' Enabled="false" />
        </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText="Tablet">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox3" runat="server" Checked='<%# Eval("84_Tablet").ToString() == "1" ? true:false %>' Enabled="false"/>
        </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText="Mobile">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox4" runat="server" Checked='<%# Eval("85_Mobile").ToString() == "1" ? true:false %>' Enabled="false" />
        </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText="None">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox5" runat="server" Checked='<%# Eval("86_None").ToString() == "1" ? true:false %>' Enabled="false" />
        </ItemTemplate>
   </asp:TemplateField>
    <asp:CommandField ShowEditButton="True" />
</Columns>

然后这是我尝试使用的代码。基本上,当我点击编辑按钮时,我希望复选框本身被启用。无论出于何种原因,当页面重新加载时,该复选框根本没有启用。在单击编辑按钮后,我刚开始尝试启用“Checkbox1”,但最终想要启用所有 5 个复选框。

 protected void grd_Bookcode_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = grd_Bookcode.Rows[index];

            CheckBox chk = (CheckBox)row.FindControl("CheckBox1");
            chk.Enabled = true;


        }
    }

【问题讨论】:

  • 您正在使用的代码发生了什么?
  • 这段代码没有启用复选框吗?你能否对这个问题更清楚一点。
  • 问题是什么?什么不起作用?如果您调试,您是否设法到达此代码行: CheckBox chk = (CheckBox)row.FindControl("CheckBox1"); ?
  • 对不起,我对这个问题做了一些修改。 Davide,我通过那行代码没有问题,编译也没有错误。问题是页面重新加载时复选框未启用。

标签: .net asp.net gridview checkbox


【解决方案1】:

如果您希望编辑控件不同于标准控件,则应使用“EditItemTemplate”。这将允许编辑行在行的模式发生变化时具有不同的控件、值等。

例子:

        <Columns>
            <asp:TemplateField HeaderText="PC">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("82_PC").ToString() == "1" ? true:false %>' Enabled="false" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked="true" Enabled="false" />
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>

【讨论】:

  • 如果它解决了您的问题,请不要忘记将其标记为答案!
  • @Zachary 非常感谢!!我现在一直在为 AGES 寻找这个,让它变得又好又简单!
【解决方案2】:

我猜你可以遍历 GridView 的所有行并启用复选框,如下所示:

    protected void grd_Bookcode_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            for (int index = 0; index < GridView1.Rows.Count; index++)
            {
                CheckBox chk = grd_Bookcode.Rows[index].FindControl("CheckBox" + index + 1) as CheckBox;
                chk.Enabled = true;
            }
        }
    }

希望这会有所帮助!

【讨论】:

  • 对,这就是我要做的,但使用 edititemtemplate 只是一种更简单的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 2011-05-31
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
相关资源
最近更新 更多