Saturday, August 7, 2010

Running specific JavaScript after partial postback (update panel)

It is very easy to register a new javascript after postback, but if u are using a update panel and want to register a new javascript after a partial postback then you must use the different way then u use in first case.
Here is the code to solve that issue.
i have got this code from the site http://fooberry.com/2009/05/25/running-specific-javascript-after-partial-postback/
here we will create a reusable control so u can use it anywhere u want in ur project.
you can download the code from here.
Download
add this files into ur project.
and now see the code below to use it.
filename.aspx
add the following line in file before head
<%@ Register src="DoAfterPostback.ascx" tagname="DoAfterPostback" tagprefix="uc1" %>
now add the javascript code

    
and web controls as follows:

                
                    
                    
                    
                        Text    ="Button"
                        OnClick ="ButtonClicked" />

                    
                        Text    ="Label" />
                
            
  

filename.aspx.cs
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 + "');";
    }
}



Now run your project and enter value in textbox and click on button to test it.
If u have any problem in implementing this then feel free to contact me.
Amit Panchal
info@amitech.co
http://www.amitech.co

No comments:

Amitech

Hell0 Friends,
i know you are stuck with some serious problems and that's why you are here. So friends i m putting all the solved problems(with solution) that i have faced in my life (technical problems) on this blog.
In case you can not find the proper solutions, feel free to mail me at info@amitech.co
Amit Panchal