计算检查的radiobuttons

时间:2016-08-27 19:20:02

标签: c# asp.net

我使用4个radiobuttons作为问题的选项。按钮点击后刷新问题和radiobuttons。将检查的radiobutton文本与答案进行比较,如果匹配,则增加计数器。我发现当我第一次检查一个单选按钮时,下一个问题上的相应单选按钮将被检查,如果是答案,它将增加计数器,如果我改变它以检查另一个单选按钮,这是实际答案,计数器是没有增加。我是否必须为我用来刷新问题的updatepanel指定任何属性。你花时间阅读这篇漫长的文章。

这是我的aspx代码

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style>
        .con{
            width:70%;
            margin:auto;
        }
        .testres{
            display:none;
        }
        .btnHome{
            display:none;
            color:rgb(0,159,37);
            text-decoration:none;
            background:none;
            border:1px solid grey;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div class="con">
    <div class="subject-name">
        <asp:Label runat="server" Text="Subject : " /><asp:Label ID="lblSubjectName" runat="server" Text="" />
    </div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
              <div class="question-options">
                <fieldset>
                    <legend><asp:Label ID="lblQ" runat="server" Text="Q.No. "/><asp:Label ID="lblQuesNo" runat="server" Text="" /></legend>
                    <asp:Panel ID="question" runat="server">
                        <asp:Label ID="lblQues" runat="server" Text="" />
                        <ul style="list-style-type:none">

                            <li><asp:RadioButton ID="RadioButton1"  runat="server" AutoPostBack="false" GroupName="answer" Text="" /></li>
                            <li><asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="false" GroupName="answer" Text=""  /></li>
                            <li><asp:RadioButton ID="RadioButton3" runat="server" AutoPostBack="false" GroupName="answer" Text="" /></li>
                            <li><asp:RadioButton ID="RadioButton4" runat="server" AutoPostBack="false" GroupName="answer" Text="" /></li>
                        </ul>
                    </asp:Panel>
                    <asp:Panel ID="Panel1" CssClass="testres" runat="server">
                        <asp:Label ID="Label1" runat="server" Text="Correct Answers : "></asp:Label>
                        <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
                    </asp:Panel>
                    <asp:Button ID="btnNext" runat="server" Text="Next" OnClick="btnNext_Click"/>
                    <asp:Button ID="btnHome" runat="server" Text="Home" CssClass="btnHome" OnClick="btnHome_Click"/>
                 </fieldset>
               </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</asp:Content>

这是背后的代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class OnlineTest_Question : System.Web.UI.MasterPage
{
    SqlConnection con = new SqlConnection("Password=password@123;Persist Security Info=True;User ID=sa;Initial Catalog=MyDatabasae;Data Source=.");
    SqlCommand cmd=new SqlCommand();
    SqlDataReader rdr;
    DataTable dt = new DataTable();
    DataTable dt1 = new DataTable();
    DataTable dt3 = new DataTable();
    static int rowcount=0;
    static int totalrowcount;
    string id = "";
    string subname = "";
    string correctans = "";
    int i = 1;
    static int correctcount = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cmd.CommandText = "select subject from subjectstb where subid='" + Request.QueryString["sid"] + "'";
            cmd.Connection = con;
            con.Open();
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                subname = rdr["subject"].ToString();
            }
            rdr.Close();
            con.Close();
            lblSubjectName.Text = subname;
            try
            {
                cmd.CommandText = "select * from questionstb where subject='" + Request.QueryString["sid"] + "'";
                con.Open();
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();
                dt.Load(rdr);
                rdr.Close();
                totalrowcount = dt.Rows.Count;
                lblQues.Text = dt.Rows[0]["question"].ToString();
                id = dt.Rows[0]["qid"].ToString();
                cmd.CommandText = "select * from optionstb where qid='" + id + "'";
                rdr = cmd.ExecuteReader();
                dt1.Load(rdr);
                RadioButton1.Text = dt1.Rows[0]["choice"].ToString();
                RadioButton2.Text = dt1.Rows[1]["choice"].ToString();
                RadioButton3.Text = dt1.Rows[2]["choice"].ToString();
                RadioButton4.Text = dt1.Rows[3]["choice"].ToString();
                lblQuesNo.Text = (rowcount + 1).ToString();

                correctans = dt1.Rows[0]["correct"].ToString();


                    if (RadioButton1.Checked == true && RadioButton1.Text == correctans)
                    {
                        correctcount = correctcount + 1;
                    }
                    else if (RadioButton2.Checked == true && RadioButton2.Text == correctans)
                    {
                        correctcount = correctcount + 1;
                    }
                    else if (RadioButton3.Checked == true && RadioButton3.Text == correctans)
                    {
                        correctcount = correctcount + 1;
                    }
                    else if (RadioButton4.Checked == true && RadioButton4.Text == correctans)
                    {
                        correctcount = correctcount + 1;
                    }


            }


            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }



    }

    protected void btnNext_Click(object sender, EventArgs e)
    {


        if(rowcount<5)
        {
            if(rowcount>=totalrowcount)                                         
            {
            lblQuesNo.Text = "Not Available";
            lblQues.Text = "No more questions available.";
            }
            else
            {
                cmd.CommandText = "select * from questionstb where subject='" + Request.QueryString["sid"] + "'";
                con.Open();
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();
                dt3.Load(rdr);
                rdr.Close();
                totalrowcount = dt3.Rows.Count;
                lblQuesNo.Text = (rowcount+1).ToString();
                lblQues.Text = dt3.Rows[rowcount]["question"].ToString();
                dt1.Load(rdr);

                id = dt3.Rows[rowcount]["qid"].ToString();
                cmd.CommandText = "select * from optionstb where qid='" + id + "'";
                rdr = cmd.ExecuteReader();
                DataTable dt2 = new DataTable();
                dt2.Load(rdr);
                rdr.Close();
                RadioButton1.Text = dt2.Rows[0]["choice"].ToString();
                RadioButton2.Text = dt2.Rows[1]["choice"].ToString();
                RadioButton3.Text = dt2.Rows[2]["choice"].ToString();
                RadioButton4.Text = dt2.Rows[3]["choice"].ToString();

                cmd.CommandText = "select * from optionstb where qid='"+id+"'";
                rdr = cmd.ExecuteReader();
                DataTable d=new DataTable();
                d.Load(rdr);
                correctans = d.Rows[0]["correct"].ToString();

                if (RadioButton1.Checked == true && RadioButton1.Text == correctans)
                {
                    correctcount = correctcount + 1;
                }
                else if (RadioButton2.Checked == true && RadioButton2.Text == correctans)
                {
                    correctcount = correctcount + 1;
                }
                else if (RadioButton3.Checked == true && RadioButton3.Text == correctans)
                {
                    correctcount = correctcount + 1;
                }
                else if (RadioButton4.Checked == true && RadioButton4.Text == correctans)
                {
                    correctcount = correctcount + 1;
                }

                rowcount = rowcount + 1;

             }

        }
        else
        {
            con.Close();
            Label2.Text = correctcount.ToString();
            question.Style.Add("display","none");
            Panel1.Style.Add("display", "block");
            btnNext.Text = "";
            lblQ.Text = "Result";
            btnHome.Style.Add("display", "block");
            lblQuesNo.Text = "";
            btnNext.Style.Add("background", "none");
            btnNext.Style.Add("border", "none");
        }

    }
    protected void btnHome_Click(object sender, EventArgs e)
    {
        rowcount = 0;
        correctcount = 0;
        Response.Redirect("testselect.aspx");
    }
}

1 个答案:

答案 0 :(得分:0)

您应该放置控制此过程的代码块。否则我们无法帮助你。