How to invoke a Java script after completion of server event of asp.net control ?

}

Dear All,

Sometime you fall in this situation where you need to invoke a java script just after your server event of asp.net control. Lets  understand with an example suppose you have a  asp.net server control button on your web page now after clicking this button page will post back and everything is added in database and then after you need to show  Java script alert like Record is Saved successfully.

so to achieve this task you need to do following things

1.) Write a Javascript function for showing custom alert message  just like below

<script type="text/javascript">
        function onSuccessful() {

              alert(‘Data Saved Successfully’);

}

function onError() {

alert (‘Data not saved successfully’);

}

2) In server side button event you need to do following thing

protected void btnFaceBookLogin_Click(object sender, EventArgs e)
     {
         try
         {
             if (CheckLogin())
             {

ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "RegisterSuccess", "onSuccessful();", true);

}

else {

ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "Registerfail", "onError();", true);

}

} catch (Exception ex) {

error.log(ex);

}

}

So when you compile it and run it after button click’s server side event javascript will fire according to checklogin  status.

Here the important note is to register the script with unique name.

I hope this will help you also.

Enjoy cheers 🙂

Rajat Jaiswal

Advertisements

One thought on “How to invoke a Java script after completion of server event of asp.net control ?

  1. Hi there! This article couldn’t be written any better!
    Looking at this article reminds me of my previous roommate!
    He always kept talking about this. I am going to send this post to him.
    Fairly certain he will have a very good read. Thank you for sharing!

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