如何使用DataBase中的值创建Dynamic RadioButtonList

时间:2013-12-30 04:22:54

标签: asp.net vb.net sql-server-2012 radiobuttonlist dynamic-controls

我的表结构

Group | Values
------  ------
Group1   Option 1
Group1   Option 2
Group1   Option 3
Group1   Option 4

Group2   Option 1
Group2   Option 2
Group2   Option 3
Group2   Option 4

现在有了上面的表值,我需要创建动态 radiobuttonlist控件,如下所示

  

第1组

     
      
  • 选项1
  •   
  • 选项2
  •   
  • 选项3
  •   
  • 选项4
  •   
     

第2组

     
      
  • 选项1
  •   
  • 选项2
  •   
  • 选项3
  •   
  • 选项4
  •   

有可能这样做吗?

并且radiobutton列表的数量也是dynamic。它可能有任意数量的radiobutton列表。我需要给unique ID for each radiobuttonlist并从所有radiobuttonlist中获取值。

这类似于在线考试但是不一样为了您更好地理解我提到的在线考试。

3 个答案:

答案 0 :(得分:1)

dtgrp = db.Getval(select distinct Group from table1)
dtval = db.GetVal(select Values from table1)

 For i = 0 To dtgrp.Columns.Count - 1
 Dim rdl As New RadioButtonList
 rdl.ID = dtgrp.Columns(i).ToString()
 rdl.Text = dtgrp.Columns(i).ToString() & vbCrLf
 For j = 0 To dtval.Rows.Count - 1
 If Not String.IsNullOrEmpty(dtval.Rows(j)(i).ToString()) Then
 rdl.Items.Add(dtval.Rows(j)(i).ToString())
 End If
 Next
 pnlgrp.Controls.Add(rdl)
 Next

答案 1 :(得分:1)

这会给你答案:

 <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder runat="server" ID="PlaceHolder1" />
        <asp:Button runat="server" ID="Button1" OnClick="Button1_Click" Text="Submit" />
        <asp:Label runat="server" ID="Label1" />
    </div>
    </form>

 protected void Button1_Click(object sender, EventArgs e)
        {
            LoadControls();
        }
        private void LoadControls()
        {


            string conString = ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from groupInfo order by group1", con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            string strGroupName = string.Empty;
            DataTable dt = ds.Tables[0];
            int flag = 0;

            RadioButtonList rblGroup = new RadioButtonList();
            Label lblGroup;
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                while (strGroupName == Convert.ToString(dt.Rows[i][0]))
                {
                    i++;
                    goto Outer;
                }

                strGroupName = Convert.ToString(dt.Rows[i][0]);
                rblGroup = new RadioButtonList();
                lblGroup = new Label();
                lblGroup.Text = strGroupName;
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (strGroupName == Convert.ToString(dt.Rows[j][0]))
                    {
                        ListItem lblItem = new ListItem(Convert.ToString(dt.Rows[j][1]), Convert.ToString(dt.Rows[j][1]));
                        rblGroup.Items.Add(lblItem);
                    }
                }
                PlaceHolder1.Controls.Add(lblGroup);
                PlaceHolder1.Controls.Add(rblGroup);

            Outer:
                continue;
            }
        }

答案 2 :(得分:0)

基本上,如果你动态创建一个控件,你需要在页面的每个帖子中重新加载那些控件(具有相同的id)。

否则,它们将变为null,您将无法访问它们。

这是一个示例。它会动态加载RadioButtonList控件,并在单击按钮时显示所选的值。

<asp:PlaceHolder runat="server" ID="PlaceHolder1"/>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" Text="Submit" />
<asp:Label runat="server" ID="Label1"/>

protected void Page_Load(object sender, EventArgs e)
{
    LoadControls();
}

protected void Button1_Click(object sender, EventArgs e)
{
    var radioButtonList = PlaceHolder1.FindControl("1") as RadioButtonList;
    Label1.Text = radioButtonList.SelectedValue;
}

private void LoadControls()
{
    var tmpRBL = new RadioButtonList();
    tmpRBL.ID = "1";

    for (int i = 1; i <= 5; i++)
    {
        var tmpItem = new ListItem(i.ToString(), i.ToString());
        tmpRBL.Items.Add(tmpItem);
    }

    PlaceHolder1.Controls.Add(tmpRBL);
}