2009-02-09 98 views
0

我有你的基本asp.net网页表单,其中包含一些强制页面超时并在5分钟后重定向的客户端JavaScript。主要是为了保护敏感信息。ASP.NET客户端回传

在超时时间,我想强制服务器回发,允许我保存表单值以备将来编辑。

我玩过ClientScript.GetPostBackClientHyperlink()和ClientScript.GetPostBackEventReference()。这两者似乎都会导致EventValidation问题。是的,我可以关闭事件验证,但有没有不同的或更好的工作?

理想情况下,我不想调用一个控件(必须显示),而只是使用某种类型的参数进行回发,我可以将serverside识别为超时条件的结果。

回答

1

没有很好的解决方案,所以我建立自己的。一个很简单的自定义控件。评论欢迎。

using System; 
using System.ComponentModel; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace BinaryOcean.Web.Library.WebControls 
{ 
    [ToolboxData("<{0}:PostBackTimer runat=\"server\" />"), DefaultProperty("Seconds"), DefaultEvent("Timeout")] 
    public class PostBackTimer : Control, IPostBackEventHandler 
    { 
     public PostBackTimer() { } 

     public string CommandArgument 
     { 
      get { return (string)ViewState["CommandArgument"] ?? string.Empty; } 
      set { ViewState["CommandArgument"] = value; } 
     } 

     public string CommandName 
     { 
      get { return (string)ViewState["CommandName"] ?? string.Empty; } 
      set { ViewState["CommandName"] = value; } 
     } 

     public bool Enabled 
     { 
      get { return (bool)(ViewState["Enabled"] ?? true); } 
      set { ViewState["Enabled"] = value; } 
     } 

     public int Seconds 
     { 
      get { return (int)(ViewState["Seconds"] ?? 0); } 
      set { ViewState["Seconds"] = value; } 
     } 

     [Description("PostBackTimer_OnTimeout")] 
     public event EventHandler Timeout = delegate { }; 

     [Description("PostBackTimer_OnCommand")] 
     public event CommandEventHandler Command = delegate { }; 

     public void RaisePostBackEvent(string eventArgument) 
     { 
      Timeout(this, EventArgs.Empty); 
      Command(this, new CommandEventArgs(CommandName, CommandArgument)); 
     } 

     protected override void OnPreRender(EventArgs e) 
     { 
      if (Enabled && Seconds > 0) 
      { 
       var postback = Page.ClientScript.GetPostBackEventReference(this, null); 
       var script = string.Format("setTimeout(\"{0}\",{1});", postback, Seconds * 1000); 
       Page.ClientScript.RegisterStartupScript(GetType(), "PostBackTimer_" + UniqueID, script, true); 
      } 

      base.OnPreRender(e); 
     } 
    } 
} 
1

这可能是矫枉过正,但您可以设置一个JavaScript定时器来触发Web服务调用。 .NET Web服务可以接受表单数据并将其保存。

0

难道你不能使用JavaScript计时器来“点击”提交按钮吗?这听起来像使用这种形式将是非常恼人的,但如果它在您尝试填写时继续发布回来。

0

with javascript,call __doPostBack("clientIdOfSubmitButton", null)。这会触发回发,就像该按钮(或任何其他您想要的控件)触发它一样。