从检索动态创建的单选按钮列表值 [英] Retrieving values from dynamically created RadioButtonList

查看:68
本文介绍了从检索动态创建的单选按钮列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code-背后(C#)文件动态声明RadioButtonLists了用一个while()循环在我的数据库问题的任何给定数量:

My code-behind (c#) file declares RadioButtonLists dynamically for any given number of questions in my database using a while() loop:

我添加项目到每个tmpRBL在for循环中。

I add items to each tmpRBL in a for loop.

我注册的每个RadioButtonList的,而我在while()循环的每次迭代的开始创建一个子面板。

I register each RadioButtonList to a child panel which I create at the beginning of each iteration of the while() loop.

然后我每个面板添加到父面板。

I then add each panel to the parent panel.

while(reader.Read()
{ 
    ...

    RadioButtonList tmpRBL = new RadioButtonList();
    temp = "RadioButtonList" + count.ToString();
    tmpRBL.ID = temp;
    tmpRBL.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Vertical;
    tmpRBL.TextAlign = System.Web.UI.WebControls.TextAlign.Left;

    ...

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

    ...

    p.Controls.Add(tmpRBL);

    ...

    questionPanel.Controls.Add(p);

}

我怎样才能检索这些动态创建RadioButtonLists的selectedIndexes?我花了一天尝试在网上从其他类​​似问题的各种修补程序的大部分时间,但没有运气。

How can I retrieve the selectedIndexes of these dynamically created RadioButtonLists? I've spent the better part of the day trying various fixes from other similar questions online, but have had no luck.

如果我使用'检查元素在Chrome中,我能看到RadioButtonLists在他们所需的位置显然是与我已分配(RadioButtonList1,RadioButtonList2等)的ID,但我的一切,我尝试用空对象结束。

If I use 'Inspect Element' in Chrome, I am able to see the RadioButtonLists in their desired locations ostensibly with the ID I have assigned (RadioButtonList1, RadioButtonList2, etc), but I everything I try ends up with a null object.

我是比较新的C#,这是我第一次与动态控制,所以非常感谢提前为处理提供任何帮助。

I'm relatively new to C# and this is my first time dealing with dynamic controls, so big thanks in advance for any help offered.

推荐答案

基本上,如果你动态创建一个控件,您将需要重新加载这些控件(用相同的ID ),在页面的每一个岗位回来。

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

否则,他们将成为空,您将无法访问它们。

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

下面是一个示例。动态加载单选按钮列表控制和单击一个按钮时,显示选定的值了。

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

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

这篇关于从检索动态创建的单选按钮列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆