Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net ScriptManager.RegisterClientScriptBlock注册脚本两次_Asp.net_Asp.net Ajax_Updatepanel_Servercontrol - Fatal编程技术网

Asp.net ScriptManager.RegisterClientScriptBlock注册脚本两次

Asp.net ScriptManager.RegisterClientScriptBlock注册脚本两次,asp.net,asp.net-ajax,updatepanel,servercontrol,Asp.net,Asp.net Ajax,Updatepanel,Servercontrol,在自定义服务器控件的OnPreRender上,我正在注册一个警报,以便在每次部分回发后加载页面时弹出(add_pageLoaded(function(){alert('Hi')})。我只希望这个被调用一次,但是它被调用的次数和部分回发的次数一样多。例如,在下面的代码中,如果打开页面并单击“单击我”,您将收到一个警报,再次单击,您将收到两个警报,三个-->三个警报,依此类推。我的理解是,使用scriptKey参数的ScriptManager.RegisterClientScriptBlock应该检

在自定义服务器控件的OnPreRender上,我正在注册一个警报,以便在每次部分回发后加载页面时弹出(add_pageLoaded(function(){alert('Hi')})。我只希望这个被调用一次,但是它被调用的次数和部分回发的次数一样多。例如,在下面的代码中,如果打开页面并单击“单击我”,您将收到一个警报,再次单击,您将收到两个警报,三个-->三个警报,依此类推。我的理解是,使用scriptKey参数的ScriptManager.RegisterClientScriptBlock应该检查脚本是否已注册,如果已注册,则不要再次注册。 我无法在上注册此脚本!因为我的自定义控件将在回发到页面时插入,所以使用!iPostBack将不包括我的脚本

以下是此问题的一个简单示例:

ASPX:


我找到了解决办法

在页面事件中,在执行任何需要执行的操作后,需要从所连接的事件中删除该函数

例如:

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

function DoSomething() {
  alert('Hello');
  Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(DoSomething);
}

我将此控件插入部分回发的页面中,因此无法检查!Page.IsPostBack,因为控件将在回发时添加。我在没有iPostBack的情况下尝试过,它似乎总是以未注册的形式返回。我认为您不能使用Page.ClientScript方法来检查使用ScriptManager注册的脚本
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);
        }
    }
}
        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);
        }
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(DoSomething);

function DoSomething() {
  alert('Hello');
  Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(DoSomething);
}