开发者

How would I register a piece of script, just once, given numerous subsequent asynchronous postbacks? Please read on

I have been left sratching my head开发者_开发百科! I have used the appropriate overload as suggested by MSDN to only render a script at least once over many async postback...and here is what I have....What is going on!!

  if (!ScriptManager.GetCurrent(Page).GetRegisteredClientScriptBlocks().Any(x=> x.Key ==   jscriptKey))
        {
          //  Page.RegisterClientScriptBlock(jscriptKey, "");
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), jscriptKey, sb.ToString(), true);
        }


Client script block registrations do not persist in ScriptManager through any hidden mechanism. That is, if you look at GetRegisteredClientScriptBlocks() it is always going to be empty after a postback (async or otherwise). However, though ClientScriptManager may be unaware that a script has already been added to the non-asynchronously-updated part of the page, anything that was added there will, of course, still be there after an async postback. You need to keep track of your registrations in your code somehow -- and use logic to only register them once.

What's your situation? Do you have a usercontrol or something that needs to register script? You could either check Page.IsPostBack, and only register if it's not a postback, or if that's not good enough (because the control could be loaded for the first time on a postback) then set a flag in ViewState so it knows when it's already registered its script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜