The common way to accomplish this is to use RegisterStartupScript and the RegisterClientScriptBlock methods of the Page class. Page.RegisterStartupScript - This method adds _javascript_ to the web form right before the ending </form> tag. This wouls be best used when we want to initiate a _javascript_ function when the page is loaded. It takes two parameters � the first being the key of the script and the -- the second being the script itself (represented as a string). private void Page_Load(object sender, System.EventArgs e) { ----- ----- Page.RegisterStartupScript("UserScript", "<script language=_javascript_>" + "function HelloWorld() { alert('Hello World'); }</script>"); Button1.Attributes["onclick"] = "HelloWorld()"; ----- ----- } Page.RegisterClientScriptBlock - This method adds _javascript_ to the web form, right after the <form runat="server"> declaration. It also takes two parameters� the first being the key of the script and the second being the script itself (represented as a string). The main difference between the two methods is that the RegisterStartupScript method places the _javascript_ at the bottom of the ASP.NET page right before the closing </form> element while RegisterClientScriptBlock method places the _javascript_ directly after the opening <form> element in the page. |