Retrieving values from dynamically created RadioBu

2020-05-07 19:26发布

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

I add items to each tmpRBL in a for loop.

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

}

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.

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.

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.

标签: c# asp.net
2条回答
Bombasti
2楼-- · 2020-05-07 19:41

Thank's a lot. In the same way, I, finnaly managed to put other controls under control :-). Here is code-behind in vb.net.

Me now very lucky :-)

Creating & Retrieving values from dynamically created RadioButtonList & TextBox

Private Property RadioButtonList As RadioButtonList
    Private Property TextBoxList As TextBox


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        LoadControls()

    End Sub           



    Protected Sub Button111_Click(sender As Object, e As EventArgs)
        Dim majstor_rbl(3) As String
        Dim majstor_txt(3) As String

        For i As Integer = 1 To 3
            RadioButtonList = TryCast(PlaceHolder1.FindControl(i & "rbl"), RadioButtonList)
            majstor_rbl(i) = RadioButtonList.SelectedValue
            TextBoxList = TryCast(PlaceHolder1.FindControl(i & "txt"), TextBox)
            majstor_txt(i) = TextBoxList.Text
        Next

        txt_podatak4.Text = majstor_rbl(1) & " - " & majstor_txt(1)
        txt_podatak5.Text = majstor_rbl(2) & " - " & majstor_txt(2)
        txt_podatak6.Text = majstor_rbl(3) & " - " & majstor_txt(3)
    End Sub

    Private Sub LoadControls()
        For j As Integer = 1 To 3

            Dim tmpRBL As Object = New RadioButtonList
            Dim tmpTXT As Object = New TextBox

            tmpRBL.ID = j & "rbl"
            tmpTXT.ID = j & "txt"



            For i As Integer = 1 To 3
                Dim tmpItem As Object = New ListItem("&nbsp;&nbsp;&nbsp;", i.ToString())
                tmpRBL.Items.Add(tmpItem)
            Next


            tmpRBL.RepeatLayout = RepeatLayout.Flow
            tmpRBL.RepeatDirection = RepeatDirection.Horizontal


            PlaceHolder1.Controls.Add(tmpTXT)
            PlaceHolder1.Controls.Add(tmpRBL)
            PlaceHolder1.Controls.Add(New LiteralControl("</br>"))
        Next

    End Sub

.aspx

    <asp:PlaceHolder runat="server" ID="PlaceHolder1"/>
    <asp:Button runat="server" ID="Button3" OnClick="Button111_Click" Text="Submit" />
<asp:TextBox ID="txt_podatak4" runat="server" CssClass="form-control"></asp:TextBox>
<asp:TextBox ID="txt_podatak5" runat="server" CssClass="form-control"></asp:TextBox>
<asp:TextBox ID="txt_podatak6" runat="server" CssClass="form-control"></asp:TextBox>
查看更多
狗以群分
3楼-- · 2020-05-07 19:53

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);
}
查看更多
登录 后发表回答