请教如何做出只触发一次的事件
设计要求用url+参数触发服务器端一个sql server job. 同时还要在页面上显示job正在执行,请等候. job完事了页面显示结果.
我是这么搞的. 把触发job的代码写在一个timer控件里. 然后用一个隐藏的 Textlabel 里的数值控制是否执行timer里的内容. timer每10秒触发一次, 第一次触发把Textlabel值改成0. 以后检查这个Textlabel值如果是0就不再触发job了.
这么搞理论上job只会触发一次. 可实际情况是在一些情况下居然连续触发了两次job. 检查后发现问题应该就出在用textlabel的内容做判断触发的依据. 也就是说在第一次触发timer到第二次触发前的10秒内,textlabel的值没有更改.
对ajax的timer控件不熟悉, 谁能帮忙给看看代码, 到底问题出在什么地方.多谢了,分分献上.
default.aspx
<body style="border: 0 none white; background-color: #EEF3FB; color: #424242; width: 500px; margin: 3px;">
<form id="form1" runat="server">
<div style="width: 500px">
<asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="900">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="LabelResult" runat="server"></asp:Label>
<asp:Image ID="ImageJobRunning" runat="server"
ImageUrl="~/Images/imgloading.gif" Visible="False" ImageAlign="Top" />
<asp:Label ID="Label3" runat="server" Text="1" Visible="False"></asp:Label>
<asp:Label ID="LabelURL" runat="server" Visible="False"></asp:Label>
<br />
</ContentTemplate>
</asp:UpdatePanel>
<div>
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="10000">
</asp:Timer>
</div>
<asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
default.aspx.cs
namespace ImportCatchmentWebApp
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string catchmentName = Request.QueryString["CatchmentName"];
if (Convert.ToInt32(Label3.Text.ToString()) == 1)
{
if (catchmentName != null)
{
LabelResult.Text = "Job is Running, please wait for up to 10 minutes.";
ImageJobRunning.Visible = true;
}
else
LabelResult.Text = "Request failed,need parameter: \"Catchment Name\"";
}
else if (Convert.ToInt32(Label3.Text.ToString()) == 2)
{
{
string url = LabelURL.Text;
Response.Redirect(url, true);
}
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
int result=-1;
int loadID=0;
string errorString="";
if (Convert.ToInt32(Label3.Text.ToString()) == 1) //第一次执行时 textlabel是1
{
Label3.Text = "0"; //第一次执行把值改成0, 意图只执行一次控件里的内容
string userName = Request.ServerVariables["LOGON_USER"];
string catchmentName = Request.QueryString["CatchmentName"];
if (catchmentName != null)
{
ImportCatchmentWebApp icwApp = new ImportCatchmentWebApp();
result = icwApp.RunImportCatchment(catchmentName, userName); //执行sql server job
loadID = icwApp.GetLoadID();
if (result == 0)
{
LabelResult.Text = "Import successful, job finished. Please save the socio report when prompted.";
ImageJobRunning.Visible = false;
string url = icwApp.GetReportLink();
//If Catchment import is successful, redirect to
//the reporting service link
//Response.Redirect(url, true);
Label3.Text = "2";
LabelURL.Text = url;
}
else
{
errorString = icwApp.GetErrorMessage(result);
LabelResult.Text = "Trade Area import failed.<br>" +
"Please contact your System Administrator with the following error:<br>"+
"LoadID = "+loadID+"<br>"+
"Error Code = "+result+" - "+errorString;
ImageJobRunning.Visible = false;
}
}
}
}
}
}