我有一个转发器,还有一个复选框。当我在checkedchanged事件中单击第一个时,行信息显示在下面。但是第一次点击后我遇到了麻烦。有时信息是相同的。因为foreach总能看到第一次点击。例如,我检查了第二个,我看到了信息。然后我再次点击第二个确定我看到了信息,但这次我再次点击了第一个。 Foreach可以取第一个复选框,最后一个仍然在回发之前检查它再次执行第二个操作。 有没有什么办法解决这一问题? 这是我的示例代码。
<asp:Repeater ID="rptInformations" runat="server">
<ItemTemplate>
<asp:CheckBox ID="ckChoose" runat="server" OnCheckedChanged="ckChoose_CheckedChanged" AutoPostBack="true" />
<div id="foo" runat="server"> ... some basic titles ...</div>
</ItemTemplate>
<asp:Repeater>
<div id="info" runat="server"> ... informations in here (textboxes, labels ..etc)</div>
CodeBehind:
foreach (RepeaterItem item in rptInformations.Items)
{
//CheckBox ckChoose= (CheckBox)sender;
CheckBox ckChoose= item.FindControl("ckChoose") as CheckBox;
if (cBoxChoose.Checked)
{
... database process ...
}
}
答案 0 :(得分:1)
至于我的理解,你每次检查复选框时都试图触发一个事件,它会根据它所属的转发器的行显示某些信息,为此我在一个名为Repeater的页面上做了这个例子。 aspx,使用的事件只会触发一个复选框而不是全部:
Repeater.aspx:
<asp:Repeater runat="server" ID="repeater">
<ItemTemplate>
<asp:CheckBox ID="ckChoose" runat="server" AutoPostBack="true" OnCheckedChanged="ckChoose_CheckedChanged"/>
<div id="foo" runat="server"> <%# Eval("Field2")%></div>
</ItemTemplate>
</asp:Repeater>
<div id="info" runat="server"> ... informations in here (textboxes, labels ..etc)</div>
Repeater.aspx.cs(Codebehind):
partial class Repeater1 : System.Web.UI.Page
{
private System.Collections.Generic.List<Item> Elements()
{
Generic.List<Item> itemList = new Generic.List<Item>();
itemList.Add(new Item("1", "One"));
itemList.Add(new Item("2", "Two"));
return itemList;
}
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack) {
this.repeater.DataSource = this.Elements();
this.repeater.DataBind();
}
}
protected void ckChoose_CheckedChanged(object sender, System.EventArgs e)
{
CheckBox chk = (CheckBox)sender;
this.info.InnerHtml = "checkbox:" + chk.ID + " foo:" + ((HtmlGenericControl)chk.Parent.FindControl("foo")).InnerText;
}
}
希望这有帮助。
答案 1 :(得分:0)
我想我用namingcontainer修复它。它很完美。
foreach (RepeaterItem item in Repeater1.Items)
{
CheckBox cBoxx = item.FindControl("ckChoose") as CheckBox;
current_cbId = cBoxx.NamingContainer.UniqueID.ToString();
if (cBoxx.Checked)
{
... some operations ...
}
if (cBoxChoose.NamingContainer.UniqueID.ToString() != current_cbId)
cBoxChoose.Checked = false;
}