ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.[Solved] Dynamically created ASP.Net RadioButtonList control loses selected value after PostBack<p>Hi <a class="username" rel="anvina"> anvina</a>,</p> <p>Refer below sample code.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:Table ID=&#34;TableContent&#34; runat=&#34;server&#34;&gt; &lt;/asp:Table&gt; &lt;asp:Button ID=&#34;ButtonSave&#34; runat=&#34;server&#34; Text=&#34;Save&#34; OnClick=&#34;ButtonSave_Click&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">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 = &#34;rbl1&#34;; rbuttons.RepeatLayout = RepeatLayout.Flow; rbuttons.AutoPostBack = true; rbuttons.EnableViewState = true; rbuttons.RepeatDirection = RepeatDirection.Horizontal; rbuttons.Items.Add(new ListItem(&#34;Yes&#34;, &#34;1&#34;)); rbuttons.Items.Add(new ListItem(&#34;No&#34;, &#34;0&#34;)); rbuttons.SelectedIndexChanged += SaveRadioButtons; td.Controls.Add(rbuttons); td.Controls.Add(new LiteralControl(&#34;&lt;br/&gt;&#34;)); td.Controls.Add(new LiteralControl(&#34;&lt;br/&gt;&#34;)); tr.Controls.Add(td); TableContent.Rows.Add(tr); } protected void ButtonSave_Click(object sender, EventArgs e) { string message = &#34;&#34;; for (int i = 0; i &lt; TableContent.Rows.Count; i++) { foreach (RadioButtonList list in TableContent.Rows[i].Cells[0].Controls.OfType&lt;RadioButtonList&gt;()) { message += list.SelectedValue + &#34;: &#34; + list.SelectedItem.Text; } } ClientScript.RegisterClientScriptBlock(this.GetType(), &#34;alert&#34;, &#34;alert(&#39;&#34; + message + &#34;&#39;);&#34;, true); } private void SaveRadioButtons(object sender, EventArgs e) { RadioButtonList rb = (RadioButtonList)sender; string value = rb.SelectedValue; string text = rb.SelectedItem.Text; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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 = &#34;rbl1&#34; rbuttons.RepeatLayout = RepeatLayout.Flow rbuttons.AutoPostBack = True rbuttons.EnableViewState = True rbuttons.RepeatDirection = RepeatDirection.Horizontal rbuttons.Items.Add(New ListItem(&#34;Yes&#34;, &#34;1&#34;)) rbuttons.Items.Add(New ListItem(&#34;No&#34;, &#34;0&#34;)) rbuttons.SelectedIndexChanged += AddressOf SaveRadioButtons td.Controls.Add(rbuttons) td.Controls.Add(New LiteralControl(&#34;&lt;br/&gt;&#34;)) td.Controls.Add(New LiteralControl(&#34;&lt;br/&gt;&#34;)) 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 = &#34;&#34; 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 &amp; &#34;: &#34; + list.SelectedItem.Text Next Next ClientScript.RegisterClientScriptBlock(Me.GetType(), &#34;alert&#34;, &#34;alert(&#39;&#34; &amp; message &amp; &#34;&#39;);&#34;, 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</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p>&nbsp;<img src="https://i.imgur.com/R0CTeFs.gif" alt="" width="248" height="218" /></p>https://www.aspforums.net:443/Threads/193646/Solved-Dynamically-created-ASPNet-RadioButtonList-control-loses-selected-value-after-PostBack/https://www.aspforums.net:443/Threads/193646/Solved-Dynamically-created-ASPNet-RadioButtonList-control-loses-selected-value-after-PostBack/Fri, 16 Aug 2019 07:58:46 GMT