Hello,
I'm writing a project about exams. The number of answers in the exam are 4. and each one has a RadioButton near it that can be pressed to choose the answer.
What I do to check which of the radio buttons is selected is that I manually add an event (since the number of questions is unknown), and in that event (Checked) I add the event id to a list.

Questions are taken from database (oledbconnection etc.)

while (dr.Read())
                {
                    if (i % 2 == 0)
                        color = Color.MediumBlue;
                    else
                        color = Color.LightBlue;

                    TableRow final = new TableRow();
                    TableCell tc_image = new TableCell();
                    System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
                    image.ImageUrl = dr["Question_Image"].ToString();
                    image.Width = 200;
                    image.Height = 200;
                    tc_image.Controls.Add(image);
                    tc_image.BackColor = color;
                    final.Controls.Add(tc_image);

                    Table answers = new Table();
                    TableRow answerrow1 = new TableRow();
                    TableCell table = new TableCell();
                    table.HorizontalAlign = HorizontalAlign.Right;
                    TableCell answer1_question = new TableCell();
                    answer1_question.BackColor = color;
                    answer1_question.HorizontalAlign = HorizontalAlign.Right;
                    TableCell answer1_radio = new TableCell();
                    answer1_radio.BackColor = color;
                    answer1_radio.HorizontalAlign = HorizontalAlign.Right;
                    Label answer1 = new Label();
                    answer1.Text = dr["Question_Answer1"].ToString();
                    answer1_question.Controls.Add(answer1);
                   [B] RadioButton radiobutton1 = new RadioButton();
                    radiobutton1.ID = dr["Question_ID"].ToString() + "-1";
                    radiobutton1.GroupName = "Answers" + i.ToString();
                    radiobutton1.CausesValidation = true;
                    radiobutton1.CheckedChanged += new System.EventHandler(this.radiobutton1_Checked);
                    answer1_radio.Controls.Add(radiobutton1);[/B]
                    TableCell tc1 = new TableCell();
                    tc1.HorizontalAlign = HorizontalAlign.Right;
                    tc1.Controls.Add(answer1_question);
                    tc1.Controls.Add(answer1_radio); 
                    answerrow1.Controls.Add(tc1);

                    TableRow answerrow2 = new TableRow();
                    TableCell answer2_question = new TableCell();
                    answer2_question.BackColor = color;
                    answer2_question.HorizontalAlign = HorizontalAlign.Right;
                    TableCell answer2_radio = new TableCell();
                    answer2_radio.BackColor = color;
                    answer2_radio.HorizontalAlign = HorizontalAlign.Right;
                    Label answer2 = new Label();
                    answer2.Text = dr["Question_Answer2"].ToString();
                    answer2_question.Controls.Add(answer2);
                    [B]RadioButton radiobutton2 = new RadioButton();
                    radiobutton2.ID = dr["Question_ID"].ToString() + "-2";
                    radiobutton2.GroupName = "Answers" + i.ToString();
                    radiobutton2.CausesValidation = true;
                    radiobutton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
                    answer2_radio.Controls.Add(radiobutton2);[/B]
                    TableCell tc2 = new TableCell();
                    tc2.HorizontalAlign = HorizontalAlign.Right;
                    tc2.Controls.Add(answer2_question);
                    tc2.Controls.Add(answer2_radio);
                    //answers.Controls.Add(tc2);
                    //answers.Controls.Add(tc1);
                    answerrow2.Controls.Add(tc2);

                    TableRow answerrow3 = new TableRow();
                    TableCell answer3_question = new TableCell();
                    answer3_question.HorizontalAlign = HorizontalAlign.Right;
                    TableCell answer3_radio = new TableCell();
                    answer3_radio.HorizontalAlign = HorizontalAlign.Right;
                    Label answer3 = new Label();
                    answer3.Text = dr["Question_Answer3"].ToString();
                    answer3_question.Controls.Add(answer3);
                    [B]RadioButton radiobutton3 = new RadioButton();
                    radiobutton3.ID = dr["Question_ID"].ToString() + "-3";
                    radiobutton3.GroupName = "Answers" + i.ToString();
                    radiobutton3.CausesValidation = true;
                    radiobutton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);                    
                    answer3_radio.Controls.Add(radiobutton3);[/B]
                    TableCell tc3 = new TableCell();
                    //tc3.BackColor = color;
                    tc3.Controls.Add(answer3_question);
                    tc3.Controls.Add(answer3_radio);
                    //answers.Controls.Add(tc3);
                    //answers.Controls.Add(tc1);
                    answerrow3.Controls.Add(tc3);

                    TableRow answerrow4 = new TableRow();
                    TableCell answer4_question = new TableCell();
                    answer4_question.HorizontalAlign = HorizontalAlign.Right;
                    TableCell answer4_radio = new TableCell();
                    answer4_radio.HorizontalAlign = HorizontalAlign.Right;
                    Label answer4 = new Label();
                    answer4.Text = dr["Question_Answer4"].ToString();
                    answer4_question.Controls.Add(answer4);
                    [B]RadioButton radiobutton4 = new RadioButton();
                    radiobutton4.ID = dr["Question_ID"].ToString() + "-4";
                    radiobutton4.GroupName = "Answers" + i.ToString();
                    radiobutton4.CausesValidation = true;
                    radiobutton4.CheckedChanged += new System.EventHandler(this.radioButton4_CheckedChanged);                  
                    answer4_radio.Controls.Add(radiobutton4);[/B]
                    TableCell tc4 = new TableCell();
                    tc4.HorizontalAlign = HorizontalAlign.Right;                 
                    tc4.Controls.Add(answer4_question);
                    tc4.Controls.Add(answer4_radio);               
                    answerrow4.Controls.Add(tc4);

                    answers.Controls.Add(answerrow1);
                    answers.Controls.Add(answerrow2);
                    answers.Controls.Add(answerrow3);
                    answers.Controls.Add(answerrow4);
                    answers.BackColor = color;
                    table.Controls.Add(answers);
                    table.BackColor = color;
                    
                    final.Controls.Add(table);

                    Label Question = new Label();
                    Question.Text = dr["Question_Text"].ToString();
                    TableCell questionq = new TableCell();
                    questionq.BackColor = color;
                    questionq.HorizontalAlign = HorizontalAlign.Right;
                    questionq.Controls.Add(Question);
                    final.Controls.Add(questionq);

                    Label numbering = new Label();
                    numbering.Text = "-" + i.ToString() + " " + "q.num-";
                    TableCell numberingcell = new TableCell();
                    numberingcell.HorizontalAlign = HorizontalAlign.Right;
                    numberingcell.Controls.Add(numbering);
                    numberingcell.BackColor = color;
                    final.Controls.Add(numberingcell);
                    Table1.Controls.Add(final);
                    Table1.Rows[i - 1].BackColor = Color.Green;                 
                    i++;
                }
 private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            int o = 0;
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            int o = 0;
        }
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            int o = 0;
        }
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            int o = 0;
        }

What I do is a bit complicated imo but I do that because I want it to be very ordered.
The TableRow is (final), in it there are 4 cells (image,answers,question,number). Inside answers cell there is another table that has in it the radio button, and the answer text.
Here is how it looks after executing:
http://i43.tinypic.com/148f6ti.png

So why the event is not firing even when we click on the radio button?

Recommended Answers

All 2 Replies

When adding controls to an asp.net page dynamically, It has to be done in Page_Load or Page_init events. Then only the viewstate will be maintained between postbacks. The events for those conctrol will fire properly.

In which event are you adding table and radio button controls to the page?

Here is how I do it:
From page_load i call a function (GetQuestions) that goes to the database and gets every question ID that are in the same exam.
Everytime GetQuestions gets an ID to the question that is in the exam, it sends it to the function that is written in my 1st post and this fuction adds the Row to the table which has the questions and answers and image..
I'll try to put everything in the Page_Init and see what happen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.