how to get dynamically generated radio buttons id's - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


how to get dynamically generated radio buttons id's

kalyanapukalyanapu Posts: 2Member
in my application i am using the following code to display
requested number of Questions and it's Answers in a page. after
clicking Finish button i have to save the selected options into Sql
database table


my code is:

static int noq;
protected void Page_Load(object sender, EventArgs e)
noq = Convert.ToInt16(Session["Noq"]);


DataSet ds1 = new DataSet("questionsds");
int NumberOfQuestions = 0;

public void ReadQuestionsIntoTable()

SqlConnection cn=new SqlConnection("user id=sa;
password=sa123; database=questionbank;data source=MERCURY-987BECC");
SqlDataAdapter da1 = new SqlDataAdapter("Select * From
Question Where idquestion IN(Select idQuestion From Category_Details
Where idSubCategory IN(Select idSubCategory From SubCategory Where
SubCategoryName='" + Session["SelectedSubCategory"].ToString()+ "'))",
SqlDataAdapter da2 = new SqlDataAdapter("Select * From
Answer_Detail Where idquestion IN(Select idQuestion From
Category_Details Where idSubCategory IN(Select idSubCategory From
SubCategory Where SubCategoryName='" +
Session["SelectedSubCategory"].ToString() + "'))", cn);

// Fill the questions and choices tables in memory
DataTable QuestionsTable=ds1.Tables["Questions"];
DataTable ChoicesTable=ds1.Tables["Choices"];

// create a data relation between the Questions and Choices

DataRelation QALink=new

// go through every row in the questions table
// and place each question in the Table Web Control

for (int i = 0; i < noq;i++ )
DataRow dr = QuestionsTable.Rows[i];
// create a row for the question and read it from the
TableRow tr = new TableRow();
TableCell aCell = new TableCell();

// get the text for the question and stick it in the cell
aCell.Text = dr["Qname"].ToString();
string[] AnswerArray = new string[50];
AnswerArray[NumberOfQuestions] = dr["Answer"].ToString();

// create a row for the choices and read from the database
int count = 0;

// go through the child rows of the question table

foreach (DataRow choiceRow in dr.GetChildRows(QALink))
TableRow tr2 = new TableRow();

// create a cell for the choice
TableCell aCell3 = new TableCell();
aCell3.Width = 1000;

// align the choices on the left
aCell3.HorizontalAlign = HorizontalAlign.Left;

// create a radio button in the cell
RadioButton rb = new RadioButton();

// assign the radio button to Group + QuestionID
rb.GroupName = "Group" +

// Assign the choice to the radio button
rb.Text = choiceRow["options"].ToString();

// Assign the radio button id corresponding to the
choice and question
rb.ID ="Radio" + NumberOfQuestions.ToString()
+Convert.ToChar(count + 65);

rb.Visible = true;

// add the radio button to the cell

// add a table row between each question
// as a spacer
TableRow spacer = new TableRow();
spacer.Height = 30;
TableCell spacerCell = new TableCell();
spacerCell.Height = 30;



after clicking button, how should i save the selected option into
database table(Result table). because during above code, when number
of question is 10(i.e.. noq=10), then 10 questions will be displayed
along with options.
the generated radio button id's are


how should i use these id's to save selected option into database..if
number of question is changed the generated id's also change..


  • bradwangbradwang Posts: 197Member
    Hello, I think I have previously come across with the same situation as you are now.

    My solution is to use JavaScript to save the user checked IDs to a hidden HTML control which has property: runat="server" and on any server method, I can access the above hidden HTML control and loop through the checked IDs and save them to the database.

    Just to give you some hints and hope this helps you a little bit... :)

    I'm Brad Wang...
    .NET Freelancer from China
    MSN: [email protected]
    Skype: brad_wang
Sign In or Register to comment.