开发者

Asp.net, C# Selecting radiobuttons and labels from a panel

I ran into a new problem on my project;

I have a panel on my page in which I add labels, radiobuttons and 2 imagebuttons using pure programming code (because the number of labels and radiobuttons can be different).

I gave each radiobutton a special ID so I know in which content of the panel it belongs. Now the big problem is I don't know how to get things fro开发者_JAVA技巧m the panel.

Let's say the panel was filled with labels and radiobuttons (labels for questions and radiobuttons for the answering score 0-10) and I scored every question as asked, how do I for example select every radiobutton from the panel with an id that ends with '5' and get it's value ?

Name of the panel = pnlMain

my code : http://pastebin.com/gv8ycMY4

Thanks I Hope you guys could help me out here because I'm really stuck on this.

grtz, Nico


You could itterate through the panel's controls to get each radiobuttonlist, then itterate through those controls to get the radio buttons.

foreach (Control RBL in pnlMain.Controls)
{
    if (RBL is RadioButtonList)
    {
        foreach (ListItem LI in (RBL as RadioButtonList).Items)
        {
            if (LI.Text.EndsWith("5") && LI.Selected)
            {
                // Do something with the radiobutton
            }
        }
    }
}


Found the answer finally;

If you want to keep these dynamic controls after postback etc :

F.E. me, i used a SelectedIndexChange to fill up my panel with dynamic controls. I made another method named laden() in wich i wrote my code to display these dynamic controls. If you want these controls to pass postbacks etc simply place the following peace of code into the Page_Load method:

    if (Page.IsPostBack) {
        laden();
    }

Wich means, if you get a postback, it will Re-load these controls after postback. Even if you got data inserted in a textbox or radiobuttons wich where selected, it will still be the same as before the postback, no data lost.

enjoy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜