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

Last Reply 7 months ago By pandeyism

Posted 7 months 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>
This question does not have replies that have been liked.