ASP.NET Ajax计时器控件 [英] ASP.NET Ajax Timer Control
本文介绍了ASP.NET Ajax计时器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在设计一个测验网站.
我想定期显示下一个问题,例如30秒后.
我正在使用Ajax Timer控件.但是当我运行代码时,问题的周期性变化不会得到反映.
仅当我单击按钮时,它才会更改.
下面是代码.
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.
<%@ 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);
}
}
}
有人可以协助吗?
问候
MK
Can anybody assist ?
Regards
MK
推荐答案
intQuestionNo每次都初始化为1.尝试将其存储在"并从"视图状态中获取.
顺便说一下,您的间隔现在是3秒.
祝你好运!
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!
在执行ShowQuestion(intQuestionNo);之前尝试在Timer1_Tick中递增intQuestionNo;
希望对您有所帮助.
---------------------------------------------
我的网站:
因瓜鲁柳斯(Imóveisem Guarulhos)遇到了一个麻烦
Try increment intQuestionNo in Timer1_Tick before execute ShowQuestion(intQuestionNo);
I hope this help.
---------------------------------------------
My site:
Imóveis em Guarulhos a venda
我也遇到了这个问题,谁可以提供有效的方法.谢谢.
I also encountered this problem,Who can provide an effective method.Thanks.
这篇关于ASP.NET Ajax计时器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文