You can add java-script to server controls in multi ways:-
you can call Clientsidefunctions[javascript] from ClientSideEvents or ServerSideEvents
1. On body tag as a javascript single statement:-
<body onload="javascript:document.forms[0]['Button1'].value=Date();">
2.Server's control's attribute property :-
Button1.Attributes.Add("onclick","javascript:alert('ALERT ALERT!!!')");
-using this property you can add javascript to the controls attributes from server side ie using c# code written in g.d. or on page load,for ex:-
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(object sender, EventArgs e) {
Button1.Attributes.Add("onclick",
"javascript:alert('ALERT ALERT!!!')");
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:Button id="Button1" runat="server" Font-Bold="True"
Font-Names="Verdana" Font-Size="Larger"
Text="Click Me!"></asp:Button>
</form>
</body>
</html>
3.Working directly with control:-
<asp:ImageButton id="ImageButton1"onmouseover="this.src='button2.jpg'" onclick="ImageButton1_Click" onmouseout="this.src='button1.jpg'" runat="server" ImageUrl="button1.jpg"></asp:ImageButton>
-if u r using control directly then "this" keyword is used to specify that control,you can use the this keyword followed by the property you are trying to change.
4.Keeping javascript under script tag:-u can write functions under head section and call them from anywhere or also can write single or multi line executable codes inside body section but runat=server is not allowed here with script tag.
<head>
<script type="text/javscript">
---
---
</script>
</head>
or inside body :-
<body>
<script language="javscript">
---
---
</script>
</body>
5.Using RajisterStartupScript method[only in asp.net website with location http]:- When working with the RegisterStartupScript method, the constructor asks for two parameters—the first being the key of the script and the second being the script itself (represented as a string).For ex use this code on pageload
Page.RegisterStartupScript("MyScript",
"<script language=javascript>" +
"function AlertHello() { alert('Hello ASP.NET'); }</script>");
Button1.Attributes["onclick"] = "AlertHello()";
Button2.Attributes["onclick"] = "AlertHello()";
->when this method is used then after execution it will produce <script> tag just before the closing </form> tag on browser in html.
6.Using RajisterClientScriptBlock method[only in asp.net website with location http]:- When working with the RajisterClientScriptBlock method, the constructor asks for two parameters—the first being the key of the script and the second being the script itself (represented as a string).For ex use this code on pageload
Page.RajisterClientScriptBlock("MyScript",
"<script language=javascript>" +
"function AlertHello() { alert('Hello ASP.NET'); }</script>");
Button1.Attributes["onclick"] = "AlertHello()";
Button2.Attributes["onclick"] = "AlertHello()";
->when this method is used then after execution it will produce <script> tag just after the starting <form> tag on browser in html.
7.Keeping javascript in a seperate file(.js):-Keeping JavaScript functions in a separate file (a .js file) is highly recommended. Once they are in a separate file and part of a project, the file can be imported into a page using different methods:-
* Page.RegisterClientScriptBlock("MyScript",
"<script language=javascript src='MyJavaScriptFile.js'>");
* <head>
<script type="text/javascript" src="myjavascriptfile.js"></script>
</head>
*<body>
<script type="text/javascript" src="myjavascriptfile.js"></script>
</body>
Note:- when RegisterClientScritBlock or RegisterStartupScript is used to load .js files then no need to close the script tag ie </script> need not to be written.
8.You can call java-script functions from asp.net web server controls:-
<script type="text/javascript">
function s()
{
document.getElementById("Button2").value="Nishant";
}
</script>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="NEXT" onmouseover="s()" />
Note:- DotNet doesnt supports all javascript keywords in intellisense box.