2009-02-09 68 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)。這會觸發回發,就像該按鈕(或任何其他您想要的控件)觸發它一樣。