读书人

DataList里头的RadioButtonList绑定数

发布时间: 2013-07-16 22:38:05 作者: rapoo

DataList里面的RadioButtonList绑定数据
DataList里头的RadioButtonList绑定数据

上面是我数据库的数据
下面是我前台的代码



<asp:DataList ID="DataList1" runat="server" Height="241px" Width="775px" >
<ItemTemplate>
<table>
<tr>
<td>
<%#Container.ItemIndex+1 %>
、<asp:Label ID="timu" runat="server" Text='<%#Eval("question") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" >
<asp:ListItem Value="A"></asp:ListItem>
<asp:ListItem Value="B"></asp:ListItem>


<asp:ListItem Value="C" ></asp:ListItem>
<asp:ListItem Value="D" ></asp:ListItem>

</asp:RadioButtonList>



我想让RadioButtonList分别绑定数据中的ABCD选项,求解!
[解决办法]
在DataList的ItemDataBound直接绑定
这种方法就不用在前台进行绑定了,后台绑定方法代码如下
 private void dlOption_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item
[解决办法]
e.Item.ItemType==ListItemType.AlternatingItem
[解决办法]
e.Item.ItemType==ListItemType.EditItem)
{
RadioButtonList rbt=(RadioButtonList)e.Item.FindControl("rblOption");
string data=Convert.ToString(DataBinder.Eval(e.Item.DataItem,"examOption"));
DataTable dtOpt=this.radblBind(data);
for(int i=0;i<dtOpt.Rows.Count;i++)


{
//直接用ListItem绑定 ListItem it=new ListItem(dtOpt.Rows[i]["strOpt"].ToString(),dtOpt.Rows[i]["strLet"].ToString());
rbt.Items.Add(it);
}
// 用DataBind绑定RadioButtonList rbt.DataSource=new DataView(dtOpt);
// rbt.DataTextField="strOpt";
// rbt.DataValueField="strLet";
// rbt.DataBind();
}
}

读书人网 >C#

热点推荐