Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose i have a list of radio button which are created dyanamically inside the asp.net panel control .The Panel control will also be created dyanamically .
and the panel will be added to a Panel control which is designed by user at design time.



Note - pnlBox is the panel control which is designed by me at design time

Designed
Panel
||
contain Multiple Runtime Panels
||
Each Runtime Panel contain 5 runtime radio buttons .

Now i want to acess the the radiobutton value which is selected by user .

please give me some solutions frnds ...........

What I have tried:

the code which i have tried to add the control at run time in c#
C#
Panel pnl = new Panel();
         pnl.Height = 25;

         RadioButton rb1 = new RadioButton();
         rb1.Text = "Excellent"
         pnl.Controls.Add(rb1);

         RadioButton rb2 = new RadioButton();
         rb2.Text = "Very Good";
         pnl.Controls.Add(rb2);

         RadioButton rb3 = new RadioButton();
         rb3.Text = "Good";
         pnl.Controls.Add(rb3);

         pnlBox.Controls.Add(pnl)
Posted
Updated 22-Mar-16 0:53am
v2
Comments
Karthik_Mahalingam 22-Mar-16 5:47am    
you cant get the dynamically created controls in server side.
F-ES Sitecore 22-Mar-16 6:54am    
That's only true if the controls have been created on the client via javascript. If you're creating them in your code-behind then they function no differently from controls created via the markup.

1 solution

ASP.NET
<form id="form1" runat="server">
 
    <asp:Panel ID="pnlBox" runat="server">

    </asp:Panel>
    <asp:Button ID="ButtonSubmit" OnClick="ButtonSubmit_Click" Text="Submit" runat="server" />
</form>


code behind

C#
protected void Page_Load(object sender, EventArgs e)
{
    Panel pnl = new Panel();
    pnl.ID = "MyPanel";
    pnl.Height = 25;
 
    RadioButton rb1 = new RadioButton();
    rb1.Text = "Excellent";
    rb1.GroupName = "MyRadio";
    pnl.Controls.Add(rb1);
 
    RadioButton rb2 = new RadioButton();
    rb2.Text = "Very Good";
    rb2.GroupName = "MyRadio";
    pnl.Controls.Add(rb2);
 
    RadioButton rb3 = new RadioButton();
    rb3.Text = "Good";
    rb3.GroupName = "MyRadio";
    pnl.Controls.Add(rb3);

    pnlBox.Controls.Add(pnl);

}

protected void ButtonSubmit_Click(object sender, EventArgs e)
{
    Panel myPanel = (Panel) pnlBox.FindControl("MyPanel");
    RadioButton selected = myPanel.Controls.OfType<RadioButton>().Where(b => b.Checked).FirstOrDefault();

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900