Can't get ScriptManager.RegisterStartupScript in WebControl nested in UpdatePanel to work

前端 未结 10 1285
不思量自难忘°
不思量自难忘° 2020-12-12 17:20

I am having what I believe should be a fairly simple problem, but for the life of me I cannot see my problem. The problem is related to ScriptManager.RegisterStartupScript,

相关标签:
10条回答
  • 2020-12-12 17:49

    DO NOT Use GUID For Key

    ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel) 
           Guid.NewGuid().ToString(), myScript, true);
    

    and if you want to do that , call Something Like this function

     public static string GetGuidClear(string x)
     {
          return x.Replace("-", "").Replace("0", "").Replace("1", "")
                  .Replace("2",  "").Replace("3", "").Replace("4", "")
                  .Replace("5", "").Replace("6", "").Replace("7", "")
                  .Replace("8", "").Replace("9", "");
     }
    
    0 讨论(0)
  • 2020-12-12 17:49

    What worked for me, is registering it on the Page while specifying the type as that of the UpdatePanel, like so:

    ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel) Guid.NewGuid().ToString(), myScript, true);
    
    0 讨论(0)
  • 2020-12-12 17:54

    When you call ScriptManager.RegisterStartupScript, the "Control" parameter must be a control that is within an UpdatePanel that will be updated. You need to change it to:

    ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), script, true);
    
    0 讨论(0)
  • 2020-12-12 17:58

    I had an issue with Page.ClientScript.RegisterStartUpScript - I wasn't using an update panel, but the control was cached. This meant that I had to insert the script into a Literal (or could use a PlaceHolder) so when rendered from the cache the script is included.

    A similar solution might work for you.

    0 讨论(0)
  • 2020-12-12 18:00

    I think you should indeed be using the Control overload of the RegisterStartupScript.

    I've tried the following code in a server control:

    [ToolboxData("<{0}:AlertControl runat=server></{0}:AlertControl>")]
    public class AlertControl : Control{
        protected override void OnInit(EventArgs e){
            base.OnInit(e);
            string script = "alert(\"Hello!\");";
            ScriptManager.RegisterStartupScript(this, GetType(), 
                          "ServerControlScript", script, true);
        }
    }
    

    Then in my page I have:

    protected override void OnInit(EventArgs e){
        base.OnInit(e);
        Placeholder1.Controls.Add(new AlertControl());
    }
    

    Where Placeholder1 is a placeholder in an update panel. The placeholder has a couple of other controls on in it, including buttons.

    This behaved exactly as you would expect, I got an alert saying "Hello" every time I loaded the page or caused the update panel to update.

    The other thing you could look at is to hook into some of the page lifecycle events that are fired during an update panel request:

    Sys.WebForms.PageRequestManager.getInstance()
       .add_endRequest(EndRequestHandler);
    

    The PageRequestManager endRequestHandler event fires every time an update panel completes its update - this would allow you to call a method to set up your control.

    My only other questions are:

    • What is your script actually doing?
    • Presumably you can see the script in the HTML at the bottom of the page (just before the closing </form> tag)?
    • Have you tried putting a few "alert("Here");" calls in your startup script to see if it's being called correctly?
    • Have you tried Firefox and Firebug - is that reporting any script errors?
    0 讨论(0)
  • 2020-12-12 18:00
    ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),script,  true );
    

    The "true" param value at the end of the ScriptManager.RegisterStartupScript will add a JavaScript tag inside your page:

    <script language='javascript' defer='defer'>your script</script >
    

    If the value will be "false" it will inject only the script witout the --script-- tag.

    0 讨论(0)
提交回复
热议问题