如何从动态RadioButtonList数组中获取SelectedValue?

时间:2012-09-29 04:24:42

标签: c# asp.net

任何人都可以帮助我从动态RadioButtonList中获取所选值,我的choices[x].SelectedValue事件中Button_Click总是得到空值。

有谁知道如何解决这个问题?谢谢。这是我的代码背后:

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; }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

因为您正在动态创建RadioButtonList控件,所以您需要确保在每个Postback上构建项目

否则,服务器将无法理解有关控件的任何信息。您可以在屏幕上看到它们,但无法访问它们。

因此,将创建RadioButtonList控件的代码移到外面   if(!IsPostBack)阻止然后尝试...

修改 而不是这个

score = ComputeGradeClass.Score_Counter(ques[x].Text.ToString(),     choices[x].SelectedValue);

尝试

score = ComputeGradeClass.Score_Counter(ques[x].Text.ToString(), rb.SelectedValue);