2012-04-06 151 views
0

在我的自定義服務器控件的OnPreRender上,我註冊了一個警報,在每次頁面在部分回傳後加載時彈出(add_pageLoaded(function(){alert('Hi')})) 。我只希望這個被調用一次,但它被調用的次數與部分回發一樣多。例如,在下面的代碼中,如果您打開頁面並單擊「單擊我」,您將看到一個警報,再次單擊,您將看到兩個警報,三個>三個警報等等。我的理解是ScriptManager.RegisterClientScriptBlock,使用scriptKey參數假設檢查腳本是否已經註冊,如果是的話,不要再註冊它。 我無法在!IsPostBack上註冊此腳本!因爲我的自定義控件將在回發期間插入到頁面中,所以使用!IsPostBack將不包含我的腳本。ScriptManager.RegisterClientScriptBlock註冊腳本兩次

下面是這個問題的一個簡單的例子:

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"           Inherits="RegisterClientScriptExperimentation._Default" %> 
<%@ Register Assembly="RegisterClientScriptExperimentation"   Namespace="RegisterClientScriptExperimentation" TagPrefix="cc" %> 
<!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"> 
    <div>  
     <asp:ScriptManager ID="ScriptManager1" runat="server"/>  
     <asp:UpdatePanel runat="server"> 
      <ContentTemplate> 
       <asp:Panel ID="panel" runat="server"/>      
      </ContentTemplate> 
     </asp:UpdatePanel>   
    </div> 
    </form> 
</body> 
</html> 

後面的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Text; 

namespace RegisterClientScriptExperimentation 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected override void OnInit(EventArgs e) 
     { 
      base.OnInit(e); 
      panel.Controls.Add(new CustomControl()); 
     } 
    } 

    public class CustomControl : CompositeControl 
    { 
     private Button button; 
     public CustomControl() 
     { 
      button = new Button(); 
      button.Text = "Click me"; 
     } 
     protected override void CreateChildControls() 
     { 
      Controls.Clear(); 
      Controls.Add(button); 
     } 
     protected override void OnPreRender(EventArgs e) 
     { 
      base.OnPreRender(e); 

      StringBuilder _text = new StringBuilder(); 
      _text.Append("var prm = Sys.WebForms.PageRequestManager.getInstance();"); 
      _text.Append("prm.add_pageLoaded(function() { alert('Page Loaded'); });"); 

      if (null != System.Web.UI.ScriptManager.GetCurrent(Page) && System.Web.UI.ScriptManager.GetCurrent(Page).IsInAsyncPostBack) 
       System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "customControlScript", _text.ToString(), true); 
      else 
       Page.ClientScript.RegisterStartupScript(this.GetType(), "customControlScript", _text.ToString(), true); 
     } 
    } 
} 

回答

0

我找到了解決這個。

在你的頁面事件中,當你執行你需要做的任何事情之後,你需要從你插入的事件中去掉這個函數。

例如:

var prm = Sys.WebForms.PageRequestManager.getInstance(); 
prm.add_pageLoaded(DoSomething); 

function DoSomething() { 
    alert('Hello'); 
    Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(DoSomething); 
} 
0
 if(!Page.ClientScript.IsClientScriptBlockRegistered("customControlScript") && !Page.IsPostBack) 
     { 

     if (null != System.Web.UI.ScriptManager.GetCurrent(Page) && System.Web.UI.ScriptManager.GetCurrent(Page).IsInAsyncPostBack)    
      System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "customControlScript", _text.ToString(), true);    
     else 
      Page.ClientScript.RegisterStartupScript(this.GetType(), "customControlScript", _text.ToString(), true); 
     } 
+0

我插入這個控制插入部分回發的網頁,所以我不能檢查Page.IsPostBack因爲控制將在回發加入!我嘗試沒有IsPostBack,它似乎總是返回未註冊。我不認爲你可以使用Page.ClientScript方法來檢查使用ScriptManager註冊的腳本 – BlueChameleon 2012-04-06 18:23:05