简体   繁体   English

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

[英]How to get SelectedValue from array of dynamic RadioButtonList?

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 event. 谁能帮助我从动态RadioButtonList中获取选定的值,但我的Button_Click事件中的choices[x].SelectedValue始终为空值。

Does anyone knows how to fix this problem? 有谁知道如何解决这个问题? Thank you. 谢谢。 Here's my Code Behind: 这里是我的代码背后:

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

Heres my Class: 这是我的课:

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

Because you are creating the RadioButtonList controls dynamically , you need to make sure you build your items on every Postback.. 因为您是动态创建RadioButtonList控件,所以需要确保在每个“回发”上都构建项目

Otherwise the Server will not understand anything about the controls. 否则,服务器将不了解有关控件的任何信息。 You can see them on the screen but you cannot access them.. 您可以在屏幕上看到它们,但无法访问它们。

So move your code of creating the RadioButtonList's controls to the outside off if (!IsPostBack) block and then try... 因此,将创建RadioButtonList控件的代码移到外部, 如果(!IsPostBack)块关闭,然后尝试...

EDIT Instead of this 编辑而不是这个

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

Try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM