Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从动态RadioButtonList数组中获取SelectedValue?_C#_Asp.net - Fatal编程技术网

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

C# 如何从动态RadioButtonList数组中获取SelectedValue?,c#,asp.net,C#,Asp.net,有人能帮我从动态RadioButtonList中获取所选值吗?我总是在选项[x]中获取空值。在我的按钮中单击事件,SelectedValue 有人知道如何解决这个问题吗?非常感谢。下面是我的代码: public static string[] questions; public static string[] ans; Random rand = new Random(); string[] option = new string[3]; static int items; public sta

有人能帮我从动态
RadioButtonList
中获取所选值吗?我总是在
选项[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);
}

由于您正在动态创建RadioButtonList控件,因此需要确保在每次回发时生成项目。

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

因此,将创建RadioButtonList控件的代码移到外部关闭 如果(!IsPostBack)阻塞,然后重试

编辑 而不是这个

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


按钮单击事件中的更正这应该是代码,但仍然不起作用受保护的无效按钮提交单击(对象发送者,事件参数e){int score=0;for(int x=0;xscore = ComputeGradeClass.Score_Counter(ques[x].Text.ToString(), choices[x].SelectedValue);
score = ComputeGradeClass.Score_Counter(ques[x].Text.ToString(), rb.SelectedValue);