Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET中控制Gridview行内的复选框_C#_Asp.net_Gridview - Fatal编程技术网

C# 在ASP.NET中控制Gridview行内的复选框

C# 在ASP.NET中控制Gridview行内的复选框,c#,asp.net,gridview,C#,Asp.net,Gridview,我不太确定如何处理这个问题,但接下来 我有一个gridview,每行有两个复选框,下面是项目模板的示例: <ItemTemplate> <asp:CheckBox ID="MasterCheckbox" runat="server"/> <asp:CheckBox ID="ChildCheckbox" runat="server" /> </ItemTemplate> 我希望ChildCheckbox的“enabled”

我不太确定如何处理这个问题,但接下来

我有一个gridview,每行有两个复选框,下面是项目模板的示例:

<ItemTemplate>
    <asp:CheckBox ID="MasterCheckbox" runat="server"/>
    <asp:CheckBox ID="ChildCheckbox" runat="server" />   
</ItemTemplate>

我希望ChildCheckbox的“enabled”属性由MasterCheckbox的“Checked”属性控制。。。因此,换句话说,只有选中MasterCheckbox时,才会启用ChildCheckbox

我知道我需要在MasterCheckbox控件上附加一个处理程序来调用一些javascript来在客户端执行必要的操作——这可能在row_databound()方法中完成

我不太清楚实现这一点所需的javascript,因此欢迎提供任何提示/提示

谢谢


Dal

首先,您不需要回答自己的问题,您可以在第一个问题中添加注释

因为您使用的是GridView,所以我认为您正在为MasterCheckBox绑定一些东西, 假设它是数据表中的布尔值。 例如,它是一个包含名称为IsMasterChecked的列的行

您可以使用绑定自定义表达式来处理启用另一个

<ItemTemplate>
   <asp:CheckBox ID="MasterCheckbox" runat="server" />
   <asp:CheckBox ID="ChildCheckbox" runat="server" Enabled='<%# Convert.ToBoolean(Eval("IsMasterChecked")) %>'/>   
</ItemTemplate>



希望这能有所帮助。

在我的脑海里,我想你必须做的是以下几点

  <asp:TemplateField HeaderText="Checkbox">
   <ItemTemplate>
   <asp:CheckBox ID="MasterCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="checkGridViewChkBox" />
   </ItemTemplate>
  </asp:TemplateField>

使用下面的代码

CheckBox MasterCheckbox;
CheckBox ChildCheckbox;

private void checkGridViewChkBox()
{
    int i;
    int x = GridView1.Rows.Count;

    for (i = 0; i < x; i++)   //loop through rows
    {
        findControls(i);

        if (MasterCheckbox.Checked)
        {
           ChildCheckbox.Enabled = true;
        }else{      
        ChildCheckbox.Enabled = false;      
        }      
    }

}

private void findControls(int i)
{                                                               
    MasterCheckbox = (CheckBox)(GridView1.Rows[i].FindControl("MasterCheckbox"));
    ChildCheckbox = (CheckBox)(GridView1.Rows[i].FindControl("ChildCheckbox")); 
}
CheckBox-MasterCheckbox;
CheckBox-ChildCheckbox;
私有void checkGridViewChkBox()
{
int i;
int x=GridView1.Rows.Count;
对于(i=0;i

虽然效率不太高,但工作正常。

谢谢玛拉-应该可以。。。即使你添加了评论,帖子是否也会向顶部移动?助教。
CheckBox MasterCheckbox;
CheckBox ChildCheckbox;

private void checkGridViewChkBox()
{
    int i;
    int x = GridView1.Rows.Count;

    for (i = 0; i < x; i++)   //loop through rows
    {
        findControls(i);

        if (MasterCheckbox.Checked)
        {
           ChildCheckbox.Enabled = true;
        }else{      
        ChildCheckbox.Enabled = false;      
        }      
    }

}

private void findControls(int i)
{                                                               
    MasterCheckbox = (CheckBox)(GridView1.Rows[i].FindControl("MasterCheckbox"));
    ChildCheckbox = (CheckBox)(GridView1.Rows[i].FindControl("ChildCheckbox")); 
}