不检查单选按钮列表中的项目 [英] Not checking an item in a Radio Button List

查看:63
本文介绍了不检查单选按钮列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我正在为测验应用程序使用单选按钮列表.

我想实现用户可以不作任何选择的功能.

也就是说,我没有使用强制验证.

但这在我检查问题是对还是错并计算分数时失败.

它说空引用异常.

以下是代码段.

Dear All,

I am using a Radio Button List for my quiz application.

I want to implement the functionality that the user can leave an answer unchecked.

That is, I am not using the Mandatory Validation.

But this fails when I am checking whether the question is right or wrong and while calculating score.

It says Null Reference Exception.

Below is the code snippet.

protected void Button1_Click(object sender, EventArgs e)
    {
        intTotalQuestion = (int)ViewState["TotalQuestion"];
        intQuestionNo = (int)ViewState["QuestionNo"];
        intScore = (int)ViewState["Score"];
        Answers = (ArrayList)ViewState["AnswerHistory"];
        
         if (RadioButtonList1.SelectedItem.Value == ViewState["CorrectAnswer"].ToString())
        {
            intScore += 10;
        }
        else
        {
            intScore = intScore - 1;
        }
        if (intQuestionNo == intTotalQuestion)
        {
            Response.Write("Your Score is " + intScore.ToString() + " Points");
            Response.Write("\n\n");
            Response.Write("Quiz over");
        }
        else
        {
            intQuestionNo = intQuestionNo + 1;
            ShowQuestion(intQuestionNo);
        }
    }



在此获取错误:



Getting the error here :

if (RadioButtonList1.SelectedItem.Value == ViewState["CorrectAnswer"].ToString())



对你们来说,这可能是一个简单的问题...但是当我是初学者时,我正面临着这个问题...:confused :: confused :: confused :: confused :: confused :: confused:

请协助...

谢谢
MK



This might as a simple problem to you guys...But I am facing this problem as I am a beginner...:confused::confused::confused::confused::confused::confused:

Kindly assist...

Thanks
MK

推荐答案

如果尚未选择项目,则SelectedItem为null.尝试以下方法:

If they haven''t selected an item, then SelectedItem is null. Try this instead:

if (RadioButtonList1.SelectedItem != null && 
    RadioButtonList1.SelectedItem.Value == ViewState["CorrectAnswer"].ToString())



&&运算符将对if语句的第一部分求值,如果它== null,它将停止撤消操作,并且您不会收到错误.



The && operator will evaluate the first part of the if statement and if it == null, it will stop evaulating and you won''t get the error.


这篇关于不检查单选按钮列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆