【问题标题】:Repeater control checkbox event not fired中继器控制复选框事件未触发
【发布时间】:2018-09-27 18:09:19
【问题描述】:

我在转发器控件中有三个复选框。我需要像单选按钮列表这样的概念。当我单击第一行第一个复选框时,其他两个被选中的属性设置为 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 事件未触发。请帮我解决这个错误。 我花了更多时间来解决这个问题,但我不能..

【问题讨论】:

  • 您想对复选框检查更改做什么。想要选中中继器中的另一个复选框。为此,您应该使用 java-script 或 jQuery。
  • 你可以用jquery来做。请参考以下链接:stackoverflow.com/questions/11687370/…

标签: c# asp.net checkbox repeater


【解决方案1】:

只需确保您没有在回发时绑定转发器,并且在转发器上启用了视图状态。

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

没有理由绑定到转发器上的 itemcreated 事件,只是为了附加到 oncheckedchanged 事件,因为这是没有意义的。

【讨论】:

    【解决方案2】:

    你也可以通过 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链接和类吗?
    【解决方案3】:

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

    【讨论】:

    • 这不能回答问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 2011-11-17
    • 2016-04-13
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多