Monday, July 27, 2009

Execute JavaScript function from ASP.NET codebehind

by declaring a JavaScript function in your code as shown below you can easly do it:

JavaScript


< head runat="server" >

< title >Call JavaScript From CodeBehind< /title >

< script type="text/javascript" >



function alertMe() {

alert('Hello');

}

< /script >

< /head >



now write below code in Page_Load to call it from code behind:

VB.NET


If (Not ClientScript.IsStartupScriptRegistered("alert")) Then

Page.ClientScript.RegisterStartupScript _

(Me.GetType(), "alert", "alertMe();", True)

End If

C#


if (!ClientScript.IsStartupScriptRegistered("alert"))

{

Page.ClientScript.RegisterStartupScript

(this.GetType(), "alert", "alertMe();", true);

}








The Page.ClientScript.RegisterStartupScript() allows you to emit client-side script blocks from code behind.

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates