在中继器控件中有三个复选框。我需要像单选按钮列表这样的概念。当我单击第一行第一个复选框时,其他两个被选中的属性设置为 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 事件未触发。请帮我解决这个错误。 我花了更多时间来解决这个问题,但我不能..
最佳答案
只需确保您没有在回发时绑定(bind)转发器,并且在转发器上启用了 View 状态。
if(!Page.IsPostBack)
{
Repeater.Datasource = dataset(what ever source);
Repeater.Databind;
}
没有理由你应该绑定(bind)到中继器上的 itemcreated 事件,只是为了附加到 oncheckedchanged 事件,因为这是没有意义的。
关于c# - 未触发中继器控件复选框事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262955/