62,074
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" %>
<%@ Register Src="Demo1.ascx" TagName="Demo1" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void method(string id)
{
var scp = string.Format("alert('触发了【{0}】');", id.Replace("'", "\\'"));
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", scp, true);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<uc1:Demo1 ID="Demo11" runat="server" 任务id="abcd" 摘要="我们的测试" 到期时间="2011-1-23 1:30:25"
On计时到啦="method" />
<br />
<br />
<uc1:Demo1 ID="Demo12" runat="server" 任务id="抢购123" 摘要="另一个的测试" 到期时间="2011-1-23 1:40:25"
On计时到啦="method" />
</form>
</body>
</html>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Demo1.ascx.cs" Inherits="Demo1" %>
距离<%= this.摘要 %>还有<asp:Label ID="Label1" runat="server" />秒!
using System;
using System.Web.UI;
public partial class Demo1 : System.Web.UI.UserControl, IPostBackEventHandler
{
public string 任务id
{
get
{
EnsureChildControls();
return (string)ViewState["business object"];
}
set
{
EnsureChildControls();
ViewState["business object"] = value;
}
}
public string 摘要
{
get
{
EnsureChildControls();
return (string)ViewState["abst"];
}
set
{
EnsureChildControls();
ViewState["abst"] = value;
}
}
public DateTime 到期时间
{
get
{
EnsureChildControls();
return (DateTime)(ViewState["end time"] ?? DateTime.Now);
}
set
{
EnsureChildControls();
ViewState["end time"] = value;
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
var scp = "window.setInterval(function(){" +
string.Format("var s=new Date(\"{0}\");", this.到期时间.ToString("yyyy/M/d H:m:s")) +
"var t=(s - new Date())/1000;" +
"document.getElementById('" + this.Label1.ClientID + "').innerText=t.toString();" +
"if(t<=0)" + this.Page.ClientScript.GetPostBackEventReference(this, string.Empty) + ";" +
"},1000);";
ScriptManager.RegisterStartupScript(this, this.GetType(), this.GetHashCode().ToString(), scp, true);
}
public event Action<string> 计时到啦; //事件唯一的string类型参数,用来说明所触发的任务id号。
public void RaisePostBackEvent(string eventArgument)
{
if (this.计时到啦 != null)
this.计时到啦(this.任务id);
}
}
<%@ Page Language="C#" %>
<%@ Register Src="Demo1.ascx" TagName="Demo1" TagPrefix="uc1" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<uc1:Demo1 ID="Demo11" runat="server" 任务id="abcd" 摘要="我们的测试" 到期时间="2011-1-23 1:30:25" />
<br />
<br />
<uc1:Demo1 ID="Demo12" runat="server" 任务id="抢购123" 摘要="另一个的测试" 到期时间="2011-1-23 1:40:25" />
</form>
</body>
</html>