复选框事件未被触发的中继器控件问题

3

我有一个重复控件中的三个复选框,需要实现类似于单选按钮列表的概念。当我点击第一行的第一个复选框时,其他两个复选框的已选属性应设置为false。如果我选择第二个,则第一个和第三个将变为未选状态。

我的设计代码如下:

      <asp:Repeater ID="repeaterItems" runat="server" 
                                                                    onitemdatabound="repeaterItems_ItemDataBound" >
    <ItemTemplate>
       <table id="mytable" cellspacing="0" width="100%" align="center">
          <tr>
               <td style="width: 18px;">
               <asp:Label ID="id" runat="server" Text='<%#Bind("fld_id")%>'></asp:Label>
               </td>
               <td style="width: 301px;">
              <asp:Label ID="Label1" runat="server" Text='<%#Bind("fld_Question")%>'></asp:Label>
               </td>
               <td style="width: 10px;">   
                    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
               </td>
               <td style="width: 30px;">
                     <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox2_CheckedChanged"/>
               </td>
               <td style="width: 33px;">
                     <asp:CheckBox ID="CheckBox3" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox3_CheckedChanged"/>
               </td>
             </tr>
           </table>
        </ItemTemplate>
     </asp:Repeater>

代码如下:

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk1 = sender as CheckBox;
        RepeaterItem item = chk1.Parent as RepeaterItem;
        CheckBox chk2 = item.FindControl("CheckBox2") as CheckBox;
        chk2.Checked = false;
    }

    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {

    }

    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {

    }

    protected void repeaterItems_ItemCreated1(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem ri = (RepeaterItem)e.Item;

        if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem)
        {
            CheckBox chk = (CheckBox)e.Item.FindControl("CheckBox1");
            chk.CheckedChanged += new EventHandler(CheckBox1_CheckedChanged);
        }
    }

CheckBox1_CheckedChanged事件没有触发。请帮忙修复此错误。我花了很多时间来解决这个问题,但是无法解决。


你想在复选框选中更改时做什么?想要在重复器中检查另一个复选框。为此,你应该使用JavaScript或jQuery中的一个。 - शेखर
你可以使用jQuery来完成这个任务。请参考以下链接: http://stackoverflow.com/questions/11687370/select-a-single-item-in-checkbox-list-using-c-sharp - Ram Singh
3个回答

3

请确保在页面回传时不要绑定重复控件,同时启用视图状态。

if(!Page.IsPostBack)
{
   Repeater.Datasource = dataset(what ever source);
   Repeater.Databind;
}

你没有必要在重复器上绑定itemcreated事件,只是为了附加到oncheckedchanged事件,因为这是无意义的。


0

你也可以通过 JQuery 实现

<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript" src="Scripts/jquery-1.8.2.min.js"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $('.chkGroup').change(function () {
                var parentTr = $(this).parent().parent();
                var currentCheckID = $(this).find('input').attr("id");
                $(parentTr).find('.chkGroup input').each(function () {
                    if (currentCheckID != $(this).attr("id")) {

                        $(this).removeAttr("checked");
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="repeaterItems" runat="server" onitemdatabound="repeaterItems_ItemDataBound">
            <ItemTemplate>
                <table id="mytable" cellspacing="0" width="100%" align="center">
                    <tr>
                        <td style="width: 18px;">
                            <asp:Label ID="id" runat="server" Text='<%#Bind("ID")%>'></asp:Label>
                        </td>
                        <td style="width: 301px;">
                            <asp:Label ID="Label1" runat="server" Text='<%#Bind("Name")%>'></asp:Label>
                        </td>
                        <td style="width: 10px;">
                            <asp:CheckBox ID="CheckBox4" runat="server" CssClass="chkGroup" />
                        </td>
                        <td style="width: 30px;">
                            <asp:CheckBox ID="CheckBox5" runat="server" CssClass="chkGroup" />
                        </td>
                        <td style="width: 33px;">
                            <asp:CheckBox ID="CheckBox6" runat="server" CssClass="chkGroup" />
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>

你是否在repeaterItems的复选框中设置了所需的JQuery链接和类? - Naresh Pansuriya

-1

您可以使用相同的事件CheckBox1_CheckedChanged处理程序来处理所有三个复选框的OnCheckedChanged事件。 通过使用发送方对象,您可以知道哪个复选框引发了事件,然后将另外两个复选框的checked属性设置为false


这并没有回答问题。 - martin36

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接