[Solved] Dynamically created ASP.Net RadioButtonList control loses selected value after PostBack

Last Reply one month ago By pandeyism

Posted one month ago

Dynamically created radiobuttons lose value on seleting radiobuttons Values are reset

what do I do?

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
    GetMenuHtml(2)
End Sub

Private Sub GetMenuHtml(menuId As Integer)
    Dim tr As TableRow
    Dim td As TableCell
    Dim rbuttons As RadioButtonList = New RadioButtonList()
    rbuttons.ID = promptText.Replace(" ", "")
    rbuttons.RepeatLayout = RepeatLayout.Flow
    rbuttons.AutoPostBack = True
    rbuttons.EnableViewState = True
    rbuttons.RepeatDirection = RepeatDirection.Horizontal
    rbuttons.Items.Add(New ListItem(TranslateText("Yes"), "li" + promptText))
    rbuttons.Items.Add(New ListItem(TranslateText("No"), "li" + promptText))
    CType(rbuttons.Items, IStateManager).TrackViewState()
    AddHandler rbuttons.SelectedIndexChanged, AddressOf SaveRadioButtons
    td.Controls.Add(rbuttons)

    td.Controls.Add(New LiteralControl("<br/>"))
    td.Controls.Add(New LiteralControl("<br/>"))
    tr.Controls.Add(td)

    TableContent.Rows.Add(tr)
End Sub

Private Sub SaveRadioButtons(sender As Object, e As EventArgs)
    Dim rb As RadioButtonList = DirectCast(sender, RadioButtonList)
    Dim value As String = rb.selectedValue
End Sub

 

<%@ page title="" language="vb" autoeventwireup="false" codebehind="interview.aspx.vb" inherits="Interview.Interview" %>

<head>
</head>
<body bgcolor="#F0F0F0" lang="en-NL">
    <form id="Form2" runat="server">
    <asp:Literal ID="contentHeader" runat="server"></asp:Literal>
    <asp:Table ID="TableContent" border="0" CellSpacing="0" CellPadding="0" align="center"
        Style="width: 450.0pt; background: white; text-align: center" runat="server">
    </asp:Table>
    <div style="text-align: center; width: 450.0pt; background-color: white; margin: 0 auto;">
        <asp:Button ID="ButtonSave" runat="server" Text="Save" OnClick="ButtonSave_Click" /></div>
    <asp:Literal ID="contentFooter" runat="server"></asp:Literal>
    </form>
</body>
Posted one month ago

Hi anvina,

Refer below sample code.

HTML

<asp:Table ID="TableContent" runat="server">
</asp:Table>
<asp:Button ID="ButtonSave" runat="server" Text="Save" OnClick="ButtonSave_Click" />

Code

C#

protected void Page_PreInit(object sender, EventArgs e)
{
    GetMenuHtml();
}

private void GetMenuHtml()
{
    TableRow tr = new TableRow();
    TableCell td = new TableCell();
    RadioButtonList rbuttons = new RadioButtonList();
    rbuttons.ID = "rbl1";
    rbuttons.RepeatLayout = RepeatLayout.Flow;
    rbuttons.AutoPostBack = true;
    rbuttons.EnableViewState = true;
    rbuttons.RepeatDirection = RepeatDirection.Horizontal;
    rbuttons.Items.Add(new ListItem("Yes", "1"));
    rbuttons.Items.Add(new ListItem("No", "0"));
    rbuttons.SelectedIndexChanged += SaveRadioButtons;
    td.Controls.Add(rbuttons);
    td.Controls.Add(new LiteralControl("<br/>"));
    td.Controls.Add(new LiteralControl("<br/>"));
    tr.Controls.Add(td);
    TableContent.Rows.Add(tr);
}
protected void ButtonSave_Click(object sender, EventArgs e)
{
    string message = "";
    for (int i = 0; i < TableContent.Rows.Count; i++)
    {
        foreach (RadioButtonList list in TableContent.Rows[i].Cells[0].Controls.OfType<RadioButtonList>())
        {
            message += list.SelectedValue + ": " + list.SelectedItem.Text;
        }
    }
    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);
}

private void SaveRadioButtons(object sender, EventArgs e)
{
    RadioButtonList rb = (RadioButtonList)sender;
    string value = rb.SelectedValue;
    string text = rb.SelectedItem.Text;
}

VB.Net

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)
        GetMenuHtml()
    End Sub

    Private Sub GetMenuHtml()
        Dim tr As TableRow = New TableRow()
        Dim td As TableCell = New TableCell()
        Dim rbuttons As RadioButtonList = New RadioButtonList()
        rbuttons.ID = "rbl1"
        rbuttons.RepeatLayout = RepeatLayout.Flow
        rbuttons.AutoPostBack = True
        rbuttons.EnableViewState = True
        rbuttons.RepeatDirection = RepeatDirection.Horizontal
        rbuttons.Items.Add(New ListItem("Yes", "1"))
        rbuttons.Items.Add(New ListItem("No", "0"))
        rbuttons.SelectedIndexChanged += AddressOf SaveRadioButtons
        td.Controls.Add(rbuttons)
        td.Controls.Add(New LiteralControl("<br/>"))
        td.Controls.Add(New LiteralControl("<br/>"))
        tr.Controls.Add(td)
        TableContent.Rows.Add(tr)
    End Sub

    Protected Sub ButtonSave_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim message As String = ""
        For i As Integer = 0 To TableContent.Rows.Count - 1
            For Each list As RadioButtonList In TableContent.Rows(i).Cells(0).Controls.OfType(Of RadioButtonList)()
                message += list.SelectedValue & ": " + list.SelectedItem.Text
            Next
        Next
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('" & message & "');", True)
    End Sub
    Private Sub SaveRadioButtons(ByVal sender As Object, ByVal e As EventArgs)
        Dim rb As RadioButtonList = CType(sender, RadioButtonList)
        Dim value As String = rb.SelectedValue
        Dim text As String = rb.SelectedItem.Text
    End Sub

Screenshot