07 August 2010

Running specific JavaScript after partial postback (update panel)

Written by Amit Panchal, Posted in Blog

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 runyour 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 comment.

Social Bookmarks

About the Author

Amit Panchal

Co-Founder,

Amitech Business Solutions

All Events

Textify

Comments (0)

Leave a comment

You are commenting as guest. Optional login below.

Cancel Submitting comment...