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:
I create a webpage which will be display multiple choice questions and options with radio buttons dynamically by getting value from database using linq to sql method in asp.net c# but i don't know how to retrieve value after click the option

What I have tried:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TalentTestData.DAL;

namespace TalentTest
{
    public partial class Test1 : System.Web.UI.Page
    {
        private readonly string _connectionstring = ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                ReadQuestionsIntoTable();

            }
            
        }    
        private void ReadQuestionsIntoTable()
        {
            try
            {
                using (var context = new ExamDataDataContext(_connectionstring))
                {
                    var question = context.Questions.Select(x => x.Question1).ToList();
                    int rowCnt = question.Count;
                    List<string> Choice_data = new List<string>();
                    var Choice = context.Questions.Select(X => new { X.Choice_1, X.Choice_2, X.Choice_3, X.Choice_4 }).ToList();
                    foreach (var item in Choice)
                    {
                        Choice_data.Add(item.Choice_1);
                        Choice_data.Add(item.Choice_2);
                        Choice_data.Add(item.Choice_3);
                        Choice_data.Add(item.Choice_4);
                    }

                    int count = 0;
                    int choice_count = 0;
                    List<string> group_names = new List<string>();
                    for (int rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
                    {
                        TableRow tr1 = new TableRow();
                        tab.Rows.Add(tr1);
                        TableCell tc = new TableCell { Text = question[count] };
                        tr1.Cells.Add(tc);

                        TableRow tr2 = new TableRow();
                        tab.Rows.Add(tr2);
                        for (int j = 0; j < 4; j++)
                        {
                            // create a cell for the choice
                            TableCell Cell = new TableCell
                            {
                                Width = 1000,
                                // align the choices on the left
                                HorizontalAlign = HorizontalAlign.Left
                            };
                            tr2.Cells.Add(Cell);
                            RadioButton rb = new RadioButton
                            {
                                // assign the radio button to Group + QuestionID
                                GroupName = "Group" + rowCtr.ToString(),
                                Text = Choice_data[choice_count],
                                ID = "Radio",
                                Visible = true
                            };
                            Cell.Controls.Add(rb);
                            choice_count++;
                        }
                        group_names.Add("Group" + rowCtr.ToString());
                        TableRow spacer = new TableRow { Height = 30 };
                        TableCell spacerCell = new TableCell { Height = 30 };
                        spacer.Cells.Add(spacerCell);
                        tab.Rows.Add(spacer);
                        count++;
                    }
                }
            }
            catch(Exception e)
            {
                Console.WriteLine("Exception caught:", e);
            }


        }
        
    }
}
Posted
Updated 23-Jan-18 23:14pm
v3
Comments
[no name] 24-Jan-18 5:13am    
From your above code i understood that you are trying to create radio buttons dynamically.But Why didn't you defined your ID in such a way that it should be different from one another.ID should be like
rdbtnQuestionIDGroupName
Then while clicking on submit you can again loop through rows to get all radio buttons selected value
Member 13453281 24-Jan-18 5:31am    
i didn't understand exactly what you say
i'm very much new to asp.net and c# could you please elaborate the answer
Karthik_Mahalingam 24-Jan-18 5:14am    
for multiple choice question,use RadioButtonList Web Server Control[^]
[no name] 24-Jan-18 6:34am    
Please check my solution Member 13453281
Member 13453281 24-Jan-18 6:39am    
yaa, i checked that
but i don't get answer

1 solution

This can be a good example to start with.
[Dynamic Multiple choice type functionality]
 
Share this answer
 
v2

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