Monday, July 01, 2013

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