ASP.NET中的后退按钮逻辑 [英] Back Button Logic in ASP.NET

查看:81
本文介绍了ASP.NET中的后退按钮逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我正在开发在线测验网站.

我有问题.我想实现后退按钮",以便可以重新检查他的回答.

我尝试实现它,但是它仅适用于2个问题,然后显示分数.

下面是代码.

请协助.

Dear All,

I am developing an Online Quiz website.

I am having questions. I want to implement Back Button, so that can re-check for what he has answered.

I tried to implement that, but it works only for 2 questions and then it displays the score.

Below is the code.

Kindly assist.

public partial class BusinessQuestions : System.Web.UI.Page
{
    int intTotalQuestion;
    int intQuestionNo = 1;
    int intScore = 0;
    ArrayList Answers;
    int marks = 0;
    int not_answered = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
       
        HyperLink2.Visible = false;
        if (!IsPostBack)
        {
            intTotalQuestion = 15;
            ViewState["StartTime"] = DateTime.Now;
            ShowQuestion(intQuestionNo);
        }


    }

    public void ShowQuestion(int intQuestionNo)
    {
        string XmlPath = Server.MapPath("Business-Questions.xml");
        XPathDocument xdoc = new XPathDocument(XmlPath);

        XPathNavigator xNav = xdoc.CreateNavigator();
        XPathNodeIterator xNodeIterator;

        string strXpath;
        int intLoop;
        
        strXpath = "/quiz/mchoice[" + intQuestionNo.ToString() + "]";
        xNodeIterator = xNav.Select(strXpath + "/question");
        while(xNodeIterator.MoveNext())
        {


            Label1.Text = intQuestionNo.ToString() + ". " + xNodeIterator.Current.Value;

            xNodeIterator = xNav.Select(strXpath + "/answer");
            RadioButtonList1.Items.Clear();

            intLoop = 0;
            while (xNodeIterator.MoveNext())
            {
                intLoop += 1;

                //intScore = 0;
                RadioButtonList1.Items.Add(new ListItem(xNodeIterator.Current.Value, intLoop.ToString()));
                if (xNodeIterator.Current.GetAttribute("correct", "") == "yes")
                {
                    ViewState["CorrectAnswer"] = intLoop;
                }
            }

                
               
            }
    
            ViewState["TotalQuestion"] = intTotalQuestion;
            ViewState["QuestionNo"] = intQuestionNo;
            ViewState["Score"] = intScore;
            ViewState["AnswerHistory"] = Answers;
            Session["n"] = marks;
            Session["not_checked"] = not_answered;

        
    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        //Button1.Attributes.Add("onclick", "return confirm(''Are you sure you proceed?'');");


        intTotalQuestion = (int)ViewState["TotalQuestion"];
        intQuestionNo = (int)ViewState["QuestionNo"];
        //intScore = (int)ViewState["Score"];
        Answers = (ArrayList)ViewState["AnswerHistory"];
        marks = (int)Session["n"];
        not_answered = (int)Session["not_checked"];

        if (RadioButtonList1.SelectedItem == null)
        {
            //intScore += 0;
            marks += 0;
            not_answered += 1;
            
        }

        
        else if (RadioButtonList1.SelectedItem.Value == ViewState["CorrectAnswer"].ToString())
        {
            //intScore += 10;
            marks += 10;
        }
        else
        {
            //intScore = intScore - 1;
            marks = marks - 1;
        }

        if (intQuestionNo == intTotalQuestion)
        {
            if (not_answered > 8)
            {
                //Button1.Attributes.Add("onclick", "return confirm(''You have answered very less questions. Do u Wish to answer more'');");
                Button1.Attributes.Add("onclick", "confirm1()");

            }

            else
            {





                //Response.Write("Your Score is " + intScore.ToString() + " Points");
                //Response.Write("\n\n");
                //Response.Write("Quiz over");

                Label3.Visible = true;
                //Label3.Text = "You have scored : " + intScore.ToString() + "Points";
                Label3.Text = "You have scored : " + marks.ToString() + "Points";
                HyperLink2.Visible = true;
            }

            


        }
        else
        {

            intQuestionNo = intQuestionNo + 1;
            ShowQuestion(intQuestionNo);
        }

    }


   

    
    protected void HiddenField1_ValueChanged(object sender, EventArgs e)
    {
        if (HiddenField1.Value == "1")
            Response.Redirect("~/KRS HomePage.aspx");
        //else
            //Response.Redirect("~/BusinessQuestions.aspx");
    }

//This is the Previous Button...
    protected void Button2_Click(object sender, EventArgs e)
    {
         
        intQuestionNo = intQuestionNo - 1;
        ShowQuestion(intQuestionNo);
    

            
    }
}

推荐答案

嗨Mukund,

希望您的下一个按钮能正常工作,您需要实现后退按钮,这将分为三个部分

1.根据问题编号显示后退按钮
2.显示上一个问题
3.显示先前选择的答案

1.要显示后退"按钮,只需在显示问题"方法的末尾添加一个if条件

Hi Mukund,

I hope your next button is working fine, you need to implement the back button, this will be three part

1. Show the back button depending on the question number
2. Display previous question
3. Display previously selected answer

1. To display the Back button, you can just add a if condition in the end of Show Question method

if(intQuestionNo > 1)
{
  Button2.Visible = true;
}
else
{
  Button2.Visible = false;
}



2.要显示上一个问题,只需从视图状态中获取当前问题编号,然后使用当前问题编号– 1
来调用show question方法.



2. To show the previous question just take the current question number from view state and call the show question method with current question no – 1

protected void Button2_Click(object sender, EventArgs e)
{
    intQuestionNo = (int)ViewState["QuestionNo"];
    intQuestionNo = intQuestionNo - 1;
    ShowQuestion(intQuestionNo);
}




3.要显示先前选择的选项,首先您需要保存问题明智的选择答案,我想您已经声明了相同的数组列表.在button1中进行更改,单击以将选定的ID存储到数组列表(Array [问题号– 1],因为您的问题从1的数组开始,数组索引从0开始)

在显示问题方法中进行更改以显示最后选择的值,然后在单选按钮列表添加循环中进行检查.

这就是概念,希望能解决您的问题




3. To display the previously selected option, first you need to save the question wise selected answer, I think you are declared an array list for the same. Make the changes in the button1 click to store the selected id to array list (Array[question no – 1], because your question start form 1 array index start from 0)

Make the changes in the show question method to display last selected value, do the checking in the radio button list adding loop.

This will be the concept and hope it will solve your problem


这篇关于ASP.NET中的后退按钮逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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