日期:2014-05-17  浏览次数:20457 次

RadioButtonList绑定单选题
怎样能让RadioButtonList同时绑定显示数据库里的4个字段值的?

并按以下格式排列?

 ○A 字段一
 ○B 字段二
 ○C 字段三
 ○D 字段四
就是单选题 答案从数据库中绑定,RadioButtonList 在Repeater里面  



------解决方案--------------------
例子
HTML code

<script runat="server">

    String[] d = { "A", "B", "C" ,"D","E"};
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            rbanswer.DataSource = new string[] { "111", "333" };
            rbanswer.DataBind();
            for(int i=0;i<rbanswer.Items.Count;i++)
            {
                rbanswer.Items[i].Text = d[i] + " " + rbanswer.Items[i].Text;
            }
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
  <form id="form1" runat="server">
    <asp:RadioButtonList ID="rbanswer" runat="server" RepeatColumns="0" RepeatDirection="Vertical" RepeatLayout="Table" TextAlign="Right"></asp:RadioButtonList>
  </form>
</body>
</html>

------解决方案--------------------
C# code
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RadioButtonList Radiobuttonlist1 = e.Item.FindControl("Radiobuttonlist1") as RadioButtonList;
        Radiobuttonlist1.Items.Add(new ListItem("A" + DataBinder.Eval(e.Item.DataItem, "字段1").ToString(), "A"));
        Radiobuttonlist1.Items.Add(new ListItem("B" + DataBinder.Eval(e.Item.DataItem, "字段2").ToString(), "B"));
        Radiobuttonlist1.Items.Add(new ListItem("C" + DataBinder.Eval(e.Item.DataItem, "字段3").ToString(), "C"));
        Radiobuttonlist1.Items.Add(new ListItem("D" + DataBinder.Eval(e.Item.DataItem, "字段4").ToString(), "D"));
    }