Ali_dotNet
Ali_dotNet

Reputation: 3279

find ASP.NET dynamically created radio button selected value using javascript

I use VS2010,C# to develop my ASP.NET web app, I'm creating a vote page for my users, I get some questions from my DB, each question is displayed in a dynamically created row, also there are 5 options (very good, good, bad....), user must select one. I use following code to create a radio button for each choice, of course all five radio buttons of a row have a unique group name:

                tr = new TableRow();
            tr.HorizontalAlign = HorizontalAlign.Right;


            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Center;
            RadioButton r = new RadioButton();
            r.Text = "";
            r.GroupName = i.ToString();
            tc.Controls.Add(r);
            tr.Cells.Add(tc);


            tc = new TableCell();
            tc.HorizontalAlign = HorizontalAlign.Center;
            r = new RadioButton();
            r.Text = "";
            r.GroupName = i.ToString();
            tc.Controls.Add(r);
            tr.Cells.Add(tc);

// five radio buttons are created in each row

now I'm going to find user choices, I think the best approach is using a JavaScript function to find selected value for each question, then perform the calculations, how can I do so? I don't want to use AutoPostback for radio buttons as it can be really slow,

thanks

Upvotes: 0

Views: 1449

Answers (1)

evasilchenko
evasilchenko

Reputation: 1870

Just add a submit button. In the submit event handler, use c# to browse through the controls and get the selected values. You don't need JavaScript for this.

Update

Basically, you will need to assign some similar names to your dynamically generated controls. I.E. Question1Radio1, Question1Radio2, etc..

After that you can use the Request.Form method to retrieve the values from the radio buttons by calling Request.Form("Question1Radio1"), Request.Form("Question1Radio2"), etc.. in your submit handler.

Upvotes: 1

Related Questions