关于Repeater 的问题 ,望大神帮忙解答

cv521zw 2017-08-11 03:00:39
是这样的 我想做一个答题的页面,题目和选项分别存在2张表中,我用的Repeater,展示题目,然后在ItemDataBound事件中动态添加了RadioButtonList并绑定数据源(因为服务器控件的id不允许自己拼接绑定数据如:ID="answer<%#Eval("tm_id")%>",如果有更好的自定义id的办法望告诉我),这时页面可以正常展示题目和选项。
但是我选择之后点击按钮想要获得选中项时出了问题,获取不到选中项,经过debug跟踪发现遍历Repeater1.Items.Count时只遍历了题目数量的行数,而动态添加的RadioButtonList找不到了,所以什么都获取不到,望大神帮看看。

页面:
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br/>
<div>
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" >
<ItemTemplate>
<%#Eval("tm_tm")%><br/>
=======================<br />
</ItemTemplate>
</asp:Repeater>
</div>
</form>

后台:

protected void Button1_Click(object sender, EventArgs e)
{
string lab = Label1.Text;
for (int i = 0; i < Repeater1.Items.Count; i++)
{
for (int j = 1; j < 4; j++)
{
RadioButtonList cb = (RadioButtonList)Repeater1.Items[i].FindControl("rbl_" + j);
if (cb != null)
{
lab = lab + cb.SelectedValue;
}
}
}
Label1.Text = lab;

}
int a = 1;
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButtonList rbl1 = new RadioButtonList();
rbl1.ID = "rbl_" + a;
e.Item.Controls.Add(rbl1);
if (e.Item.FindControl("rbl_" + a) != null)
{
RadioButtonList rbl = e.Item.FindControl("rbl_" + a) as RadioButtonList;
rbl.DataSource = select_r(a);
rbl.DataTextField = "da_da";
rbl.DataValueField = "da_x";
rbl.DataBind();
a++;
}
}
}
...全文
1062 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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