Add ScriptManager to Page Programmatically?

后端 未结 7 2126
夕颜
夕颜 2020-12-13 00:30

I am developing a WebPart (it will be used in a SharePoint environment, although it does not use the Object Model) that I want to expose AJAX functionality in. Because of t

7条回答
  •  轮回少年
    2020-12-13 01:27

    I ran into this problem with a custom ascx server control. I tried many solutions involving adding script to the OnInit events of the control (which doesn't get executed until after it checks for the ScriptManager control), adding logic inside of server tags on the control, and adding things to about every other event. No good. I finally built a control that inherits from ScriptManagerProxy and then uses ktrauberman's piece of code, slightly modified, to add a ScriptManager if needed:

      public class ProxiedScriptManager : ScriptManagerProxy
      {
        protected override void OnInit(EventArgs e)
        {
          //double check for script-manager, if one doesn't exist, 
          //then create one and add it to the page
          if (ScriptManager.GetCurrent(this.Page) == null)
          {
            ScriptManager sManager = new ScriptManager();
            sManager.ID = "sManager_" + DateTime.Now.Ticks;
            Controls.AddAt(0, sManager);
          }
    
          base.OnInit(e);
        }
      }
    

    That did it for me.

提交回复
热议问题