vb.net 如何使用数据库中的值创建动态 RadioButtonList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20833312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:17:38  来源:igfitidea点击:

How to Create Dynamic RadioButtonList with values from the DataBase

asp.netvb.netsql-server-2012radiobuttonlistdynamic-controls

提问by Amarnath Balasubramanian

My table structure

我的表结构

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

Now with the above table values i need to create dynamic radiobuttonlist controllike below

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

Group 1

  • Option 1
  • Option 2
  • Option 3
  • Option 4

Group 2

  • Option 1
  • Option 2
  • Option 3
  • Option 4

第一组

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

第 2 组

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

Is there any possibility to do this?

有没有可能做到这一点?

And the number of radiobutton list is also dynamic. It may have any number of radiobutton list. I need to give unique ID for each radiobuttonlistand get values from all the radiobuttonlist.

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

And this is similar to the Online Examinationbut it is not the samefor your better understanding i have mentioned Online Examination.

这与在线考试类似,但为了您更好地理解我提到的在线考试,它不一样

回答by sk2185

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

回答by Kumod Singh

THis will give your answer:

这会给你答案:

 <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;
            }
        }

回答by Umm E Habiba Siddiqui

Basically, if you create a control dynamically, you will need to reload those controls (with same id) in every post back of the page.

基本上,如果您动态创建控件,则需要在页面的每个回传中重新加载这些控件(具有相同的 ID)。

Otherwise, they will become null, and you won't be able to access them.

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

Here is a sample. It loads RadioButtonListcontrol dynamically and displays the selected value back when a button is clicked.

这是一个示例。它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);
}