locked
UpdatePanel doesn't work with ClientScriptManager? RRS feed

  • Question

  • User1471469962 posted

    Hi guys,

    I've been testing this for all day long, my final conclusion is AJAX UpdatePanel doesn't work with ClientScriptManager. I am using ClientScriptManager because I have to do some server side things and then use javascript to popup a new window within a Button's click. Everything works fine until today, I put the Button in an AJAX UpdatePanel, nothing else was changed, but it stopped working, no popup appears.

    I can live without the UpdatePanel, but just want to ask if there is a solution to use both of them. Thanks so much!

     

    Thursday, July 24, 2008 3:51 PM

Answers

  • User-1179452826 posted

    If you have access to AJAX (which i think you do as you're using update panels)...you should use ScriptManager.RegisterClientScriptBlock, ScriptManager.RegisterClientScriptInclude etc. They are equivalent static methods of the class ScriptManager. The first parameter is added, just pass the type of the control that'll be using the script (typeof(Page) is a good bet).

    If you have a scriptmanager on the page, it'll hook up the js. If not, it'll work just like ClientScriptManager.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 24, 2008 4:21 PM
  • User-489722809 posted

    they are static methods

    ScriptManager.RegisterClientScriptBlock(...)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 24, 2008 4:35 PM
  • User-1179452826 posted

    if you are using Ajax, you must have a ScriptManager or you will get an exception.

    You should not use the methods of the ScriptManager outside an UpdatePanel, they won't work.

    Well, not entirely. Take this scenario...he has pages that use updatepanels/js async callbacks. He has one page where he doesn't use AJAX at all (maybe for fileupload or something). He has the same piece of code generate some javascript alert to run after postback. On the page that doesn't use AJAX at all, he can still call ScriptManager.RegisterClientScriptBlock. It'll act as though ClientScriptManager were used. This is really helpful in building server controls that may or may not be used with an update panel. The ScriptManager class takes care of detecting if a ScriptManager instance is on the page or not, and registers the js accordingly.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 24, 2008 5:21 PM

All replies

  • User1096912014 posted

    You can not use UpdatePanels without ClientScriptManager. What exactly is your popup, where it is defined?

    I had similar problems when I added a GridView inside the UpdatePanel and needed to display some message when I selected something in the grid. I had to put my message controls into the UpdatePanel as well. 

     

    Thursday, July 24, 2008 4:09 PM
  • User-489722809 posted

    You can only use methods in the ScriptManager inside update panels

     check this http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.aspx

     scroll down to

    Registering Partial-Page Update Compatible Scripts

    Thursday, July 24, 2008 4:15 PM
  • User1471469962 posted

    You can not use UpdatePanels without ClientScriptManager. What exactly is your popup, where it is defined?

    I had similar problems when I added a GridView inside the UpdatePanel and needed to display some message when I selected something in the grid. I had to put my message controls into the UpdatePanel as well. 

     

    Are you sure? I'm pretty sure with my conclusion, let me show you some simple test code:

     

    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
    
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session["test"] = "HELLO!";
            ClientScriptManager clientScriptMgr = Page.ClientScript;
            StringBuilder sb = new StringBuilder();
            sb.Append(@"<script language='javascript'>");
            sb.Append(@"popup();");
            sb.Append(@"<");
            sb.Append(@"/script>");
            clientScriptMgr.RegisterClientScriptBlock(this.GetType(), "popup", sb.ToString());
        }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
        <script language="javascript">
        function popup()
        {
        window.open('dummyPage.aspx',null,'height=500,width=650');
        return false;
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" Text="LinkButton" OnClick="LinkButton1_Click" />
            </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        </form>
    </body>
    </html>
    
      

    This is the one with UpdatePanel, and it doesn't work. But if you just remove the UpdatePanel (remove these four lines:)

             <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>

             </ContentTemplate>
            </asp:UpdatePanel>

    It works perfectly... 

    Thursday, July 24, 2008 4:20 PM
  • User-1179452826 posted

    If you have access to AJAX (which i think you do as you're using update panels)...you should use ScriptManager.RegisterClientScriptBlock, ScriptManager.RegisterClientScriptInclude etc. They are equivalent static methods of the class ScriptManager. The first parameter is added, just pass the type of the control that'll be using the script (typeof(Page) is a good bet).

    If you have a scriptmanager on the page, it'll hook up the js. If not, it'll work just like ClientScriptManager.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 24, 2008 4:21 PM
  • User-489722809 posted

    if you are using Ajax, you must have a ScriptManager or you will get an exception.

    You should not use the methods of the ScriptManager outside an UpdatePanel, they won't work.

    Thursday, July 24, 2008 4:26 PM
  • User1471469962 posted

    Thanks.

    but ScriptManager doesn't seem to have RegisterClientScriptBlock or RegisterClientScriptInclude methods... 

    Thursday, July 24, 2008 4:30 PM
  • User1471469962 posted

    Sorry I'm confused.

    Who can tell me what my posted code should be... 

    Thursday, July 24, 2008 4:32 PM
  • User-489722809 posted

    they are static methods

    ScriptManager.RegisterClientScriptBlock(...)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 24, 2008 4:35 PM
  • User1471469962 posted

    guys, you are so awesome!! 

    Thursday, July 24, 2008 4:47 PM
  • User-1179452826 posted

    if you are using Ajax, you must have a ScriptManager or you will get an exception.

    You should not use the methods of the ScriptManager outside an UpdatePanel, they won't work.

    Well, not entirely. Take this scenario...he has pages that use updatepanels/js async callbacks. He has one page where he doesn't use AJAX at all (maybe for fileupload or something). He has the same piece of code generate some javascript alert to run after postback. On the page that doesn't use AJAX at all, he can still call ScriptManager.RegisterClientScriptBlock. It'll act as though ClientScriptManager were used. This is really helpful in building server controls that may or may not be used with an update panel. The ScriptManager class takes care of detecting if a ScriptManager instance is on the page or not, and registers the js accordingly.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 24, 2008 5:21 PM
  • User1471469962 posted

    Thanks again. This is really helpful. 

    Thursday, July 24, 2008 8:56 PM
  • User-489722809 posted

    "

    You use the RegisterClientScriptBlock method to register a client script block that is compatible with partial-page rendering and that has no Microsoft AJAX Library dependencies. Client script blocks that are registered by using this method are sent to the page only when control represents a control that is inside an UpdatePanel control that is being updated. To register a script block every time that an asynchronous postback occurs, use the RegisterClientScriptBlock(Page, Type, String, String, Boolean) overload of this method. 

    If you want to register a script block that does not pertain to partial-page updates, and if you want to register the script block only one time during initial page rendering, use the RegisterClientScriptBlock method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page.

    If addScriptTags is true, the RegisterClientScriptBlock method adds <script> tags around the script block. Pass false if you want to create <script> tags yourself, such as when you want to set the attributes of specific <script> tags. If addScriptTags is false and the script parameter contains multiple script blocks, an exception is thrown.

    The RegisterClientScriptBlock method adds a script block to the page after the opening <form> tag. The script blocks are not guaranteed to be output in the same order in which they are registered. If the order of the script blocks is important, concatenate your script blocks into a single string (for example, by using the StringBuilder object), and then register them as a single client script block."

     this is from this link: http://msdn.microsoft.com/en-us/library/bb350750.aspx

     the bolding is mine, and I have actually tried that, it does not work outside the UpdatePanel.

     

    Thursday, July 24, 2008 10:46 PM
  • User-1179452826 posted

    ScriptManager.RegisterWhatever should work regardless of an updatepanel being, or not being on the page. Could you show us an example of the code that isn't working for you? Please remember to use the {} code button (you get it if you click reply) when posting code.

    Friday, July 25, 2008 3:03 PM
  • User-489722809 posted

    not according to MS, if you use the first overload, the control needs to be part of an UpdatePanel or it will not work.

    With the second overload, it will insert the script only during an asyncronous partial postback.

    I don't have the buggy code anymore because I fixed it, for reasons beyond my control the project wasn't under source control so I don't have older versions of the code.

    Friday, July 25, 2008 4:02 PM
  • User1096912014 posted

    Just to verify - is this correct call ?

    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HighlightMess", " Effect.Pulsate('round',1,0.7);", true);

    Wednesday, July 30, 2008 2:50 PM
  • User1471469962 posted

    Just to verify - is this correct call ?

    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HighlightMess", " Effect.Pulsate('round',1,0.7);", true);

    Well, I used this.Page and typeof(Page) as the first two parameters and it worked fine.
    Wednesday, July 30, 2008 3:20 PM
  • User1096912014 posted

    Worked fine for me as well. Unfortunately, I'm having other more complex problems with this page, so I have to remove all UpdatePanels. I'm getting JavaScript error from validators even if I try to disable and make the validators invisible. I just posted my problem in AJAX forum (it's waiting approval).

    In few words I get a problem here

    function ValidatorOnLoad() {

    if (typeof(Page_Validators) == "undefined")

    return;

    var i, val;

    for (i = 0; i < Page_Validators.length; i++) {

    val = Page_Validators[i];

    if (typeof(val.evaluationfunction) == "string") {

    For some unknown reason two of the Page_Validarors objects are NULL in this array and thus I'm getting an error. I don't know who wrote this script (guess it's auto-generated by page and why there is no check for the enable status of validators here or for var being null) but unfortunately this problem prevents me from using UpdatePanels and I really would like to use them for this page.

    Wednesday, July 30, 2008 3:34 PM
  • User1471469962 posted

    Worked fine for me as well. Unfortunately, I'm having other more complex problems with this page, so I have to remove all UpdatePanels. I'm getting JavaScript error from validators even if I try to disable and make the validators invisible. I just posted my problem in AJAX forum (it's waiting approval).

    In few words I get a problem here

    function ValidatorOnLoad() {

    if (typeof(Page_Validators) == "undefined")

    return;

    var i, val;

    for (i = 0; i < Page_Validators.length; i++) {

    val = Page_Validators[i];

    if (typeof(val.evaluationfunction) == "string") {

    For some unknown reason two of the Page_Validarors objects are NULL in this array and thus I'm getting an error. I don't know who wrote this script (guess it's auto-generated by page and why there is no check for the enable status of validators here or for var being null) but unfortunately this problem prevents me from using UpdatePanels and I really would like to use them for this page.

    So the JS function works fine with a ClientScriptManager if you don't use UpdatePanel?
    Wednesday, July 30, 2008 3:58 PM
  • User1096912014 posted

    If I don't use UpdatePanels (but still use ScriptManager.RegisterClientScriptBlock(...) ) I don't have this weird error from validators. I get this error because I display JavaScript errors in IE. In FireFox this error is silently ignored. But since I do not want to have errors, I had to remove all UpdatePanels.

    Why I'm getting some Page_Validators to be null I could not figure out. But this error would be easily avoided if auto-generated code from MS would be a little bit more defensive (e.g. check for var =null before trying to access some of its properties). 

    Here is a link to my new thread where I gave more details about this problem

    http://forums.asp.net/p/1298185/2526055.aspx#2526055

    Wednesday, July 30, 2008 4:35 PM