ASP.NET AJAX:Registering your script during a partial postback call

By now, everyone knows that you should use the RegisterXXX methods of the ScriptManager class to insert a script block during a partial postback (that is, when you”re using ASP.NET AJAX and UpdatePanels). Even though this is “common knowledge”, what most don”t know is that  using one of those methods and associating  a script block with a page (ie, passing a reference to the page object as the first parameter od a method) might not be enough for inserting the script on the page. Lets run a small example:

<%@ 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 override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        manager.RegisterAsyncPostBackControl(bt);
    }

    void InsertScript(Object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript( this, this.GetType(), “hello”, “alert(”hello”);”, true);
    }

</script>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
    <title>Untitled Page</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
        <asp:ScriptManager runat=”server” id=”manager” />
        <asp:UpdatePanel runat=”server” id=”panel” UpdateMode=”Conditional”>
            <contenttemplate>
                <%= DateTime.Now.ToString() %>
            </contenttemplate>
        </asp:UpdatePanel>       
        <asp:Button runat=”server” ID=”bt” Text=”start partial postback” OnClick=”InsertScript”  />
    </form>
</body>
</html>

If you run this code, you won”t get the alert message. If you remove the UpdatePanel or if you set its update mode to Always, then it”ll start working. Why? well,because in the current version you”re only able to insert a script on a page during a partial postback if:

  • there aren”t any UpdatePanel controls on a page;
  • you”ve associated the script with a control placed on an UpdatePanel that is being refreshed;
  • you”ve associated the control with a page and there exists at least an UpdatePanel that is getting refreshed.

In the previous example,we”ve associated the script with the page, but notice that the only UpdatePanel that is on the page won”t be refreshed since its mode is set to Conditional and the control that started the partial postback isn”t registered as trigger nor did we call its Refresh method during the postback. So, if setting the UpdateMode property of the panel to Always is overkill, the only thing I think you can do (that is, if you can”t wait for the next version of the platform šŸ™‚ ) is add an empty UpdatePanel to the page and set its Visible property to false (and that should garantee that your scripts are always injected on the page).

Advertisements

~ by Luis Abreu on November 15, 2007.

4 Responses to “ASP.NET AJAX:Registering your script during a partial postback call”

  1. ASPInsiders commented that you need to set up web.config properly. What specifically is wrong or needs to be done with it?

  2. aspinsiders, please clarify…

  3. I did what you recommended, no luck (”Always” and false visible updatepanel).

  4. Hello guys,

    ASPInsiders is the aggregate feed of the asp insiders group. If you follow the link, you”ll see that it”s another post by me that talks about problems with the authentitcation services that will occur if you don”t set up the web. config correctly. In this case (ie, regarding the code example that is presented on this post), you don”t need to do anything to the config file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: