checkbox控件没有被选中

heerxiong 2011-03-30 03:09:55
<asp:Repeater runat="server" ID="repMyCoupon">
<ItemTemplate>
<asp:CheckBox runar="server" id="chk" text='<%#Eval(id)%>'/>
</ItemTemplate>
</asp:Repeater>

foreach (RepeaterItem rep in this.repMyCoupon.Items)
{
if (rep.ItemType != ListItemType.Header && rep.ItemType != ListItemType.Footer)
{
CheckBox chk = rep.FindControl("chk") as CheckBox;
if (chk.Checked)
{
Response.Write("<script>alert('" + chk.Text + "');</script>");
}
else
{
Response.Write("<script>alert('" + chk.Checked.ToString() + "')</script>");
}
}
}
我在repeater控件里绑定了checkbox控件,选中一个,提交后,checked属性还是false
...全文
86 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangwenquan123 2011-03-30
  • 打赏
  • 举报
回复
        
<!--我测试都可以啊-->
<asp:Repeater ID="Repeater4" runat="server">
<HeaderTemplate><table></HeaderTemplate><ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%#Eval("ID") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

    protected void Button1_Click(object sender, EventArgs e)
{
string value = "";
foreach (RepeaterItem i in Repeater4.Items)
{
CheckBox ch = i.FindControl("CheckBox1") as CheckBox;
if (ch.Checked)
value += ch.Text;
}
Response.Write(value);
}
huangwenquan123 2011-03-30
  • 打赏
  • 举报
回复
lz今天不是发了个帖,我也是按上面回复,这样获取不到?你把你的代码都帖出来看看
huangwenquan123 2011-03-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wxr0323 的回复:]
rep.FindControl
这样获取不到的。

repeater.item[索引].findcontrol

你在哪个事件里。?

e.item[e.rowindex].findcontrol()
[/Quote]看lz代码应该是在button事件提交,rep.FindControl这样可以获取到的,因为遍历的是repeater.items的每一项
               <td>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%#Eval("ID") %>' />
</td>


    protected void Button1_Click(object sender, EventArgs e)
{
string value="";
foreach (RepeaterItem i in Repeater1.Items)
{
CheckBox ch = i.FindControl("CheckBox1") as CheckBox;
if (ch.Checked)
value += ch.Text;
}
Response.Write(value);
}
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wxr0323 的回复:]
rep.FindControl
这样获取不到的。

repeater.item[索引].findcontrol

你在哪个事件里。?

e.item[e.rowindex].findcontrol()
[/Quote]
+1


protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBox chk = e.Item.FindControl("chk") as CheckBox;
}
}
子夜__ 2011-03-30
  • 打赏
  • 举报
回复
rep.FindControl
这样获取不到的。

repeater.item[索引].findcontrol

你在哪个事件里。?

e.item[e.rowindex].findcontrol()

shiyuyao1987 2011-03-30
  • 打赏
  • 举报
回复
绑定数据的代码写在if(!isPostBack)里面

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧