问题描述
请教各位高手,我想利用Repeater控件中添加RadioButtonList控件来实现考试系统中的单项选择题的功能,之前好像这个论坛有一篇类似的帖子,但是我试过了,不能用,不知道那个发帖的兄台居然说可以用,有点奇怪,我已经将Repeater控件做了数据绑定,代码是:privatevoidBindData(){SqlConnectioncon=DB.CreateConnection();con.Open();SqlCommandcmd=newSqlCommand("select*fromtb_timu",con);SqlDataAdaptersda=newSqlDataAdapter();sda.SelectCommand=cmd;DataSetds=newDataSet();sda.Fill(ds,"timu");DataTabledt=ds.Tables["timu"];Repeater1.DataSource=dt;Repeater1.DataBind();}数据库表是tb_timu,该表里面有四个字段option_A,option_B,option_C,option_D,然后现在是想用RadioButtonList控件绑定到这四个字段来显示一道题目的四个选项,请问该如何实现。之前有看过网上的一些代码,说在Repeater的ItemDataBind中添加绑定,试了各种方法都不行,下面是参考了一个网上的帖子,在ItemDataBind中添加的代码,还是不行protectedvoidRepeater1_ItemDataBound(objectsender,RepeaterItemEventArgse){foreach(RepeaterItemrpinRepeater1.Items){RadioButtonListrbtnlist=rp.FindControl("danxuanList")asRadioButtonList;rbtnlist.Items.Add(newListItem("A"+DataBinder.Eval(e.Item.DataItem,"option_a").ToString(),"A"));rbtnlist.Items.Add(newListItem("B"+DataBinder.Eval(e.Item.DataItem,"option_b").ToString(),"B"));rbtnlist.Items.Add(newListItem("C"+DataBinder.Eval(e.Item.DataItem,"option_c").ToString(),"C"));rbtnlist.Items.Add(newListItem("D"+DataBinder.Eval(e.Item.DataItem,"option_d").ToString(),"D"));}}下面是对应的HTML代码:<asp:RepeaterID="Repeater1"runat="server"onitemdatabound="Repeater1_ItemDataBound"><HeaderTemplate>一、单项选择题</HeaderTemplate><ItemTemplate><ulstyle="list-style-type:none"><li><%#(Container.ItemIndex+1)%>、</li><asp:RadioButtonListID="danxuanList"runat="server"></asp:RadioButtonList><asp:TextBoxID="TextBox1"runat="server"></asp:TextBox></ul></ItemTemplate></asp:Repeater>有没有人能教一下该怎么实现这功能,不胜感激
解决方案
解决方案二:
你没理解databound事件原理,首先foreach去掉,然后通过e.findcontrol.不懂m我
解决方案三:
引用1楼alunlearnNet的回复:
你没理解databound事件原理,首先foreach去掉,然后通过e.findcontrol.不懂m我
您说的我也有试过,是下面这样的代码吧?protectedvoidRepeater1_ItemDataBound(objectsender,RepeaterItemEventArgse){RadioButtonListrbtnlist=e.Item.FindControl("danxuanList")asRadioButtonList;rbtnlist.Items.Add(newListItem("A"+DataBinder.Eval(e.Item.DataItem,"option_a").ToString(),"A"));rbtnlist.Items.Add(newListItem("B"+DataBinder.Eval(e.Item.DataItem,"option_b").ToString(),"B"));rbtnlist.Items.Add(newListItem("C"+DataBinder.Eval(e.Item.DataItem,"option_c").ToString(),"C"));rbtnlist.Items.Add(newListItem("D"+DataBinder.Eval(e.Item.DataItem,"option_d").ToString(),"D"));}好像也不行提示