簡體   English   中英

計算檢查的radiobuttons

[英]Counting the checked radiobuttons

我使用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");
    }
}

您應該放置控制此過程的代碼塊。 否則我們無法幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM