How to Register JavaScript on Server Side with and without Atlas update panel

  • Share/Save/Bookmark

This article would help you in executing the JavaScript code after Server code execution finished in different scenario’s Like,

  1. Register JavaScript Code on the server side when your page is not having the update panel.
  2. Register JavaScript Code on the server side when you use update panel in your page.


Register JavaScript Code on the server side when your page is not having the update panel

You may be having a scenario where you want to open a new aspx page when user clicked on some button, if it’s mere client side operation then you can call onClientClick function, but some times it’s required to execute some server side code and then open the new page. This means you have first execute your server side code and then have to execute java script code. In this scenario by emitting our Client side code to page response object we can achieve the result.

you can emit the client side code to page response using the following code in server side.

Page.RegisterStartupScript("someUniqueKey","your JavaScript Code Here");

Example:

var script = "<script language=javascript>alert('ServerSideCodeFinished');</script>"
Page.RegisterStartupScript("someUniqueKey",script );

Even you can call your JavaScript functions which are having the entire functionality by sending some parameter to the function. But when you use update panel in your page, this code do not work. As update panel only updates the content of the content template it’s doesn’t bother about executing this code. so then how can we execute JavaScript code after server side code when using Update Panel?

Register JavaScript Code on the server side when you use update panel in your page

Try using the above code even in this case ( having update panel in the page), you will find nothing is firing. so change your code to

System.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanelID, UpdatePanelID.GetType(), "uniquekey",<script type='text/javascript'>jsFunctionName(" + par1 + "," + par2 + ")</script>, false);

to get your JavaScript code run. Above i am registering the JavaScript code to Response object of the update panel. so when the Content template reloaded of the given update panel id, this code get registered.

Categories : C#, Javascript

Comments

  1. [...] label. Instead of message if you want to run some client side script then read my previous article Register JavaScript Code on the server side when you use update panel in your page. This article will explain you to execute your client side script after server side code get [...]

Leave a Reply

About Techieon

Techieon is all about sharing Development experience of experienced talented developers of different technologies working in real time. Here at Techieon we talk about latest updates of different technologies, provide solutions to the developers problems, write tutorials on frequently used tools of development and provide tips to developers day to day activities.

This is a Widget Section

This section is widgetized. If you would like to add content to this section, you may do so by using the Widgets panel from within your WordPress Admin Dashboard. This Widget Section is called "Feature Bottom Middle"

Want to Write for Techieon?

Techieon have few opening positions for talented developers who want to share their development experience with Techieon users.If you are interested in writing at Techieon please Contact us or e-mail me for more information.