Archive for May, 2009

Running specific JavaScript after partial postback 7

I know how to run JavaScript after every postback, but on occasion I want to run some JS after a specific postback (i.e. show a flash message message that says the processing completed successfully). This hasn’t been as simple as I thought it should be. I even posted to StackOverflow to see if there was something I was missing or if it really is this messy. The only answer I’ve received to date suggested the method I already had in mind when posting the question, so I thought I would work out the full example and show you how it looks.

Background: First a little background. I have a UserControl that I use to display a nice status and/or error message on all my pages. It’s in the master page and has some really convenient JavaScript to show it. For all the ajaxy stuff we do it works great. It flashes in and fades out; however, we can’t show it on the partial postback events…yet. We need to run some JS on the client side after the postback is done to either show the error message or success message.

Overview: Basically, we are going to put a hidden field on the page and register an event handler to run after partial postback to see if there is any JavaScript in it, and execute there is. Pretty simple.

I created a user control to hold all the hidden fields and JS to register the postback events. It is going to get reused in a lot of places so it makes sense to make it reusable. It could be made into a server control, but I’ll forgo that for now.

There isn’t much to the user control.

<%@ Control Language="C#"
    AutoEventWireup ="true"
    CodeFile        ="DoAfterPostback.ascx.cs"
    Inherits        ="DoAfterPostback" %>        

<asp:HiddenField ID="DoAfterPostbackJavaScriptHiddenField" runat="server"
    EnableViewState="false" />

It’ll need some code to create a property to access the JavaScript we want to run and register the event handlers.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DoAfterPostback : System.Web.UI.UserControl
{
    public string DoAfterPostbackJavaScript { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(
            this.GetType(),
            "DoAfterPostbackJavaScriptEngine",
            @"
                var prm = Sys.WebForms.PageRequestManager.getInstance();
                prm.add_endRequest(function(s, e) {
                    $('[id$=_DoAfterPostbackJavaScriptHiddenField]').each(doPostbackJS);
                }); 

                function doPostbackJS(i){
                    eval(this.value);
                }

            ",true);
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        DoAfterPostbackJavaScriptHiddenField.Value = DoAfterPostbackJavaScript;
    }
}

We probably don’t need jQuery in there, but we’re already using it so I continued to use it here.

Now let’s get to using it. We’ll need a page for that.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="DoAfterPostback.ascx" tagname="DoAfterPostback" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        function showMsg(s) {
            alert(s);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <uc1:DoAfterPostback ID="DoAfterPostback1" runat="server" />
                    <asp:TextBox ID="TextBox1" runat="server" />
                    <asp:Button ID="Button1" runat="server"
                        Text    ="Button"
                        OnClick ="ButtonClicked" /><br />
                    <asp:Label ID="Label1" runat="server"
                        Text    ="Label" />
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>

We have a textbox, button and a label inside an UpdatePanel. We also have our user control in there. This will let the hidden field value change on partial postback and the new value to show up in our event handler. In addition, there is a trivial JavaScript function on the page for us to call after the postback:showMsg . Technically we could just throw the alert right in our control, but this more closely mimics how we’re going to do for real.

Finally we need some code on the server side to put some JS in our hidden field.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void ButtonClicked(object sender, EventArgs e)
    {
        var s = TextBox1.Text;
        if (string.IsNullOrEmpty(s)) return;

        Label1.Text = string.Format("The user says {0}.", s);
        DoAfterPostback1.DoAfterPostbackJavaScript = "showMsg('" + s + "');";
    }
}

That should be it. Now we can enter text, hit the button and see both our server side processing take place and the client side JavaScript execute after the postback as completed.

More training tips 0

The week is coming to an end, and I have a few more mental notes to make about training in general. For the first two, see the previous post.

3. Bring your own coffee mug and water bottle.  Not only is it nice to have a little bit of home with you on the road (my favorite coffee mug has special meaning to me), but it beats the tiny styrofoam cups they provided at our training. Ignoring the environmental impacts  of using a lot of toxic cups throughout the week, it is a pain to keep getting up to refill my coffee. Also, as one of my odd observations, the more coffee I drink in these cups, the faster it gets cold, so the faster I have to drink it, etc.

4. Get there early on the first day. As programmers, we might not be the most social kids on the playground. What I’ve found is there are either predefined cliques (e.g. coworkers) or defacto cliques (e.g. those sitting immediately next to you. After the first day, you know those people, your machine is setup just the way you like it, so you’re likely to sit there every day that week. Not just you though, everyone will most likely do the same. So if you arrive late, or even on time as I did, the first day, you’ll most likely be stuck in the back. You might not be able to read the text in the presentations or see over the heads and monitors of those in front of you.

5. Export your settings and tools to the web. This is a great trick for more than even training. I was lucky enough that my post before coming here was about my new IDE colors and I included my settings file. I was able to bring it down and now my IDE here looks exactly like the one at home. If I would have exported a few more settings, all the keystrokes would be the same as well. Beyond just the settings, any tools you might use can come in handy. This also helps if you leave your company, or get a new PC or log in somewhere else. Having those things in a central place is nice. I’ve even seen people store their settings files on Google Code so they can keep a history of them, or just for a central storage location.

6. Bring a storage device. Some of the labs, PDF’s etc are nice to have. They usually give you this as a download, but being able to take what you’ve worked on in class, back to the hotel room is pretty handy.

I should probably get started on the morning lab. Hopefully this weekend, or next week I can provide a quick review of my class.