Learning While Aging

Run server control event handler before executing javascript

Sometimes, we need to run the event handler of a server control before executing some javascript code. For instance, when user clicks a button called “Save and Close Window”, the application saves data into database, then closes the window automatically. How can we accomplish this? We can’t add an “onclick” javascript to the button’s attributes, because if we do, then the application will run the javascript code and thus close the window before it even reaches the event handler. The easiest way to implement this is to use “Response.Write()” method. Here is the sample code:

Private Sub btnSaveClose_Click(byval sender as System.Object,
                     byval e as System.EventArgs) Handles btnSaveClose.Click
   ‘Put here the code to save data into database

   ‘The following code will generate the javascript to close the window

   Response.Write(“<script language=’javascript’>”)
End Sub

That’s it! Quite simple, right?

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x
Close Bitnami banner