简体   繁体   English

未触发Repeater控件复选框事件

[英]Repeater control checkbox event not fired

Am having three check box in repeater control. 在转发器控制中有三个复选框。 I need the concept like radio button list.When i click the first row first checkbox, other two are checked property is set to false. 我需要像单选按钮列表这样的概念。当我单击第一行第一个复选框时,其他两个被检查属性设置为false。 suppose i check the second one means first and third are unchecked. 假设我检查第二个意味着第一个和第三个未经检查。

My Design code is here : 我的设计代码在这里:

      <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>

code is : 代码是:

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 event not fire. CheckBox1_CheckedChanged事件没有触发。 Please help me to fix this error. 请帮我修复此错误。 I spent more time to solve this issue but i can't.. 我花了更多的时间来解决这个问题,但我不能..

Just make sure that you are not binding the repeater on post back, and that viewstate is enabled on the repeater. 只需确保您没有在回发后绑定转发器,并且在转发器上启用了该视图状态。

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

There is no reason why you should bind to the itemcreated event on the repeater, just to attach to the oncheckedchanged event, as this is pointless. 没有理由为什么你应该绑定到转发器上的itemcreated事件,只是为了附加到oncheckedchanged事件,因为这是毫无意义的。

you can achieve by also JQuery 你也可以通过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>

You can use the same event CheckBox1_CheckedChanged handler for all the three check-boxes OnCheckedChanged . 您可以对所有三个复选框OnCheckedChanged使用相同的事件CheckBox1_CheckedChanged处理程序。 By using the sender object you could know which checkbox raised the event and then set the other two check-boxes checked property to false. 通过使用发送方对象,您可以知道哪个复选框引发了事件,然后将其他两个复选框已checked属性设置为false.

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

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