0

dynamic から選択した値を取得するのを手伝ってくれる人はいますか?イベントRadioButtonListでは常に null 値を取得します。choices[x].SelectedValueButton_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; }
        }
    }
}
4

1 に答える 1

0

RadioButtonList コントロールを動的に作成しているため、ポストバックごとにアイテムを作成する必要があります。

そうしないと、サーバーはコントロールについて何も理解できません。画面には表示されますが、アクセスすることはできません..

したがって、RadioButtonList のコントロールを作成するコードを if (!IsPostBack)ブロックの外側に移動してから試してください...

EDIT これの代わりに

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

試す

score = ComputeGradeClass.Score_Counter(ques[x].Text.ToString(), rb.SelectedValue);
于 2012-09-29T04:33:09.000 に答える