简体   繁体   English

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

[英]Controlling Checkboxes inside a Gridview Row in ASP.NET

Not really sure how to handle this issue but here goes... 不太确定如何处理此问题,但是这里...

I have a gridview with two checkboxes for each row, below is an example of the item template: 我有一个gridview,每行有两个复选框,下面是项目模板的示例:

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

I would like the 'enabled' property of the ChildCheckbox to be controlled by the 'Checked' property of the MasterCheckbox ... so in other words the ChildCheckbox is only enabled when the MasterCheckbox has been checked. 我希望ChildCheckbox的'enabled'属性由MasterCheckbox的'Checked'属性控制...换句话说,ChildCheckbox仅在MasterCheckbox被选中时才启用。

I know that I will need to append a handler onto the MasterCheckbox control to invoke some javascript to perform the necessary actions on the client-side - this will probably be done in the row_databound() method? 我知道我将需要在MasterCheckbox控件上附加一个处理程序,以调用一些javascript在客户端执行必需的操作-这可能会在row_databound()方法中完成吗?

I can't quite figure out the javascript required to get this to work, so any hints/tips would be welcome. 我不太清楚要使其正常工作所需的javascript,因此欢迎您提供任何提示。

Thanks 谢谢

Dal 达尔

First you dont need to answer your own question,you can add comments into your very first question. 首先,您不需要回答自己的问题,您可以在第一个问题中添加评论。

Since you are using GridView , I think you are binding something for MasterCheckBox, so let's say that it's boolean value in a dataTable. 由于您使用的是GridView,我认为您正在为MasterCheckBox绑定某些内容,因此可以说它是dataTable中的布尔值。 For Example it's a row contaning column with name IsMasterChecked 例如,这是名称为IsMasterChecked的行合并

You can handle Enabling the other one with binding custom expressions as 您可以处理通过绑定自定义表达式来启用另一个

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

or 要么

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

Hope this helps. 希望这可以帮助。

Off the top of my head, I think what you will have to do is something along the lines of the following... 我想,您要做的就是遵循以下内容...

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

With the following code behind. 后面有以下代码。

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")); 
}

It's not terribly efficient but works ok. 它效率不是很高,但是可以。

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

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