Friday, February 02, 2007

Javascript ASP.NET

How to use JavaScript in ASP.NET ??

Ans. Its Pretty Simple ............

To use JavaScript in an ASP.NET page, you first need to take two steps:
1.Define the script block that contains the JavaScript
function.
2.Insert the script block programmatically.

for this use either the

Page.RegisterStartupScript()
Page.RegisterClientScriptBlock()


Show MessageBox when page is started !!
To do this, Declare a string variable and assaign the
javascript code as below...

String firstscript = "<script language='javascript'>"+ "alert('Hi! JavaScript world');</script>;";

Now, load this script when ever needed like this ,for ex, write the below code in page_load function

Page.RegisterStartupScript("AlertScript", firstscript);


thats all folks .....

How to Open a Popup window when button clicked?

To popup a window when a button clicked, similar to above

Create one string variable

String popupscript= "<script language='javascript'>"+ "window.open('http://dotnethangout.blogspot.com','DotnetHangout','location=1,status=1,scrollbars=1,
width=100,height=100');</script>;";

now register this script to the page element as same as above

Page.RegisterStartupScript("PopUpWindow", popupscript);


write this in Button click event...



1 comments:

Anonymous said...

Also have a look at this link

http://www.dotnetrobert.com/dotnet/UsingJavaScriptinASPNet/tabid/113/Default.aspx