日期:2014-05-18  浏览次数:20905 次

DataList里面的RadioButtonList绑定数据


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


<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直接绑定
这种方法就不用在前台进行绑定了,后台绑定方法代码如下
C# code
 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();
            }
        }