[英]How to get SelectedValue from array of dynamic RadioButtonList?
誰能幫助我從動態RadioButtonList
中獲取選定的值,但我的Button_Click
事件中的choices[x].SelectedValue
始終為空值。
有誰知道如何解決這個問題? 謝謝。 這里是我的代碼背后:
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_No_Of_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);
}
這是我的課:
using System;
using System.Collections.Generic;
using System.Linq;
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_No_Of_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 = 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 = 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 = 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 = 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 = 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 = 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; }
}
}
}
因為您是動態創建RadioButtonList控件,所以需要確保在每個“回發”上都構建項目。
否則,服務器將不了解有關控件的任何信息。 您可以在屏幕上看到它們,但無法訪問它們。
因此,將創建RadioButtonList控件的代碼移到外部, 如果(!IsPostBack)塊關閉,然后嘗試...
編輯而不是這個
score = ComputeGradeClass.Score_Counter(ques[x].Text.ToString(), choices[x].SelectedValue);
嘗試
score = ComputeGradeClass.Score_Counter(ques[x].Text.ToString(), rb.SelectedValue);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.