2012-11-07 33 views
2

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

+0

你想在复选框检查更改上做什么?要检查中继器中的另一个复选框。为此,您应该使用java-script或jQuery。 –

+0

你可以使用jquery ..请参考以下链接: http://stackoverflow.com/questions/11687370/select-a-single-item-in-checkbox-list-using-c-sharp –

回答

1

只要确保你不要在回发站上绑定中继器,并且在中继器上启用该视图状态。

没有理由为什么你应该绑定到中继器上的itemcreated事件,只是为了附加到oncheckedchanged事件,因为这是毫无意义的。

0

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

+0

CheckBox1_CheckedChanged not fired .. – user1804985

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

什么都没有发生.. – user1804985

+0

你是否在repeaterItems的复选框中设置了需要的JQuery链接和类? –