Parfacydaydib
New Member
Can anyone help me in getting the selected value from my dynamic radiobuttonlist, I always get a null value in my choices[x].SelectedValue in my Button_Click Eventdoes anyone knows how to fix this problem? Thank you.Heres my Code Behind: \[code\] public static string[] questions; public static string[] ans; Random rand = new Random(); string[] option = new string[3]; static int items; public static Label[] ques; public static RadioButtonList[] choices; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetRandomQuestionClass.Get_N
f_Items(Convert.ToInt32(Session["user"])); items = GetRandomQuestionClass.NO_OF_ITEMS; GetRandomQuestionClass.LOAD_QUESTION_ID(Session["QuizLessonCategory"].ToString()); ques = new Label[items]; questions = new string[items]; ans = new string[items]; choices = new RadioButtonList[items]; for (int x = 0; x < items; x++) { //int i = 0; ques[x] = new Label(); ques[x].ID = "ques" + x.ToString(); questions[x] = GetRandomQuestionClass.LOAD_QUESTIONS(x); ques[x].Text = Convert.ToString(x + 1) + ".) " + GetRandomQuestionClass.LOAD_QUESTIONS(x); choices[x] = new RadioButtonList(); choices[x].ID = "choices" + x.ToString(); GetRandomQuestionClass.GET_OPTIONS(x); ans[x] = GetRandomQuestionClass.LOAD_ANSWER(x); holder.Controls.Add(ques[x]); choices[x].Items.Add(new ListItem (GetRandomQuestionClass.LOAD_ANSWER(x))); choices[x].Items.Add(new ListItem (GetRandomQuestionClass.OPT1)); choices[x].Items.Add(new ListItem (GetRandomQuestionClass.OPT2)); choices[x].Items.Add(new ListItem (GetRandomQuestionClass.OPT3)); holder.Controls.Add(choices[x]); } } } protected void ButtonSubmit_Click(object sender, EventArgs e) { int score=0; for (int x = 0; x < items; x++) { RadioButtonList rb; //rb=(RadioButtonList)FindControl("choices0"); rb = (RadioButtonList)Page.FindControl("choices0"); score = ComputeGradeClass.Score_Counter(ques[x].Text.ToString(), choices[x].SelectedValue); } ComputeGradeClass.Grade(score); }\[/code\]Heres my Class: using System; using System.Collections.Generic; using System.Linq;\[code\] using System.Text; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace CAIClassLibrary { public class GetRandomQuestionClass { //ConfigurationManager.ConnectionStrings["myCon"].ConnectionString);// public static SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\MultimedaiCAIDB.mdf;Integrated Security=True;User Instance=True"); public static SqlCommand cmd = new SqlCommand(); public static DataSet ds = new DataSet(); public static SqlDataAdapter da = new SqlDataAdapter(); public static SqlDataReader dr; public static string question,answer,opt1,opt2,opt3,opt4,opt5; public static int noofitems; public static int[] questionno; public static string section; public static string[] opt; public static int[] questionsID; public static int NO_OF_ITEMS { get { return noofitems; } set { noofitems = value; } } public static void Get_N
f_Items(int studid) { cmd = new SqlCommand("Select [Section Code] from StudentAccountTBL where [Student ID]=@ID", con); //cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@ID", SqlDbType.NVarChar).Value = http://stackoverflow.com/questions/12649897/studid; con.Open(); dr = cmd.ExecuteReader(); dr.Read(); section = dr[0].ToString(); con.Close(); cmd = new SqlCommand("Select [No of Items] from QuizScheduleTBL where Section=@Section",con); //cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@Section",SqlDbType.NVarChar).Value = http://stackoverflow.com/questions/12649897/section; con.Open(); dr = cmd.ExecuteReader(); dr.Read(); noofitems = Convert.ToInt16(dr[0]); con.Close(); } public static void LOAD_QUESTION_ID(string category) { questionsID = new int[noofitems]; int x = 0; cmd = new SqlCommand("Select TOP " + noofitems + " [Question ID] from QuestionTBL where [Lesson Category]=@Category Order By NEWID()", con); cmd.Parameters.Add("@Category",SqlDbType.NVarChar).Value = http://stackoverflow.com/questions/12649897/category; con.Open(); dr = cmd.ExecuteReader(); dr.Read(); while (dr.Read()) { questionsID[x] = Convert.ToInt16(dr[0]); x++; } con.Close(); } public static string LOAD_QUESTIONS(int ques_id) { cmd = new SqlCommand("Select Question from QuestionTBL where [Question ID] = @QuestionID",con); cmd.Parameters.Add("@QuestionID",SqlDbType.Int).Value = http://stackoverflow.com/questions/12649897/questionsID[ques_id]; con.Open(); dr = cmd.ExecuteReader(); dr.Read(); question = dr[0].ToString(); con.Close(); return question; } public static string LOAD_ANSWER(int ques_id) { cmd = new SqlCommand("Select Answer from QuestionTBL where [Question ID] = @QuestionID",con); cmd.Parameters.Add("@QuestionID", SqlDbType.Int).Value = http://stackoverflow.com/questions/12649897/questionsID[ques_id]; con.Open(); dr = cmd.ExecuteReader(); dr.Read(); answer = dr[0].ToString(); con.Close(); return answer; } public static void GET_OPTIONS(int ques_id) { cmd = new SqlCommand("Select Option1,Option2,Option3,Option4,Option5 from QuestionTBL where [Question ID] = @ID", con); cmd.Parameters.Add("ID",SqlDbType.Int).Value = http://stackoverflow.com/questions/12649897/ques_id; con.Open(); dr = cmd.ExecuteReader(); dr.Read(); opt1 = dr[0].ToString(); opt2 = dr[1].ToString(); opt3 = dr[2].ToString(); opt4 = dr[3].ToString(); opt5 = dr[4].ToString(); con.Close(); } public static string OPT1 { get { return opt1; } set { opt1 = value; } } public static string OPT2 { get { return opt2; } set { opt2 = value; } } public static string OPT3 { get { return opt3; } set { opt3 = value; } } public static string OPT4 { get { return opt4; } set { opt4 = value; } } public static string OPT5 { get { return opt5; } set { opt5 = value; } } } }\[/code\]

