Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I am designing a quiz website.

I want to periodically display the next questions, say after 30 seconds.

I am using a Ajax Timer control for the same. But the periodic change of questions is not reflected when I run the code.

It changes only when I click the button.

Below is the code.

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BusinessQuestions.aspx.cs" Inherits="BusinessQuestions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="height: 640px">
        <br />
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:Timer ID="Timer1" runat="server" Interval="3000" OnTick="Timer1_Tick">
        </asp:Timer>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

        </Triggers>
            <ContentTemplate>
                <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Verdana"
                    Font-Size="Medium" Text="Label"></asp:Label>
                <br />
                <br />
                <br />
                <br />
                <asp:RadioButtonList ID="RadioButtonList1" runat="server" BackColor="#003399"
                    BorderColor="#0000CC" BorderStyle="Solid" ForeColor="White">
                </asp:RadioButtonList>
                <br />
                <br />
            </ContentTemplate>
        </asp:UpdatePanel>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Next" />
    </div>
    </form>
</body>
</html>




Now is the code behind file.

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

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


    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        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;

        
    }





    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 == null)
        {
            intScore += 0;
        }

        
        else 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);
        }

    }
}



Can anybody assist ?

Regards

MK
Posted

intQuestionNo is initialized to 1 every time. Try 'store it in' and 'get it from' the viewstate.

Your interval is now 3 seconds by the way.

Good luck!
 
Share this answer
 
Try increment intQuestionNo in Timer1_Tick before execute ShowQuestion(intQuestionNo);

I hope this help.





---------------------------------------------
My site:
Imóveis em Guarulhos a venda
 
Share this answer
 
v2
Comments
Mukund Kallapur 26-Nov-10 11:48am    
Hey...

Thanks for the answer...but its not working...Can u pls give any other solution or atleast a hint ?
silvioyf 30-Nov-10 19:56pm    
You found a solution? Which part is not working?
I also encountered this problem,Who can provide an effective method.Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900