1. Adding simple JavaScript to a Server Control
<body onload="javascript:document.forms[0]['Button1'].value=Date();">
<body onload="javascript:document.forms[0]['Button1'].value=Date();">
Button1.Attributes.Add("onclick", "javascript:alert('ALERT ALERT!!!')")
Body1.Attributes("onload") = "document.forms[0]['TextBox2'].focus();"
2. Using Larger JavaScript Functions
<strong>RegisterStartupScript</strong> and the <strong>RegisterClientScriptBlock</strong> methods
Page.RegisterStartupScript("MyScript", "<script language="javascript">function AlertHello() { alert('Hello ASP.NET'); }</script>")
Button1.Attributes("onclick") = "AlertHello()"
Button2.Attributes("onclick") = "AlertHello()"
The main <strong>difference</strong>: RegisterStartupScript method places the JavaScript at the bottom of the ASP.NET page right before the closing </form>element. The RegisterClientScriptBlock method places the JavaScript directly after the opening
<form><p>element in the page.
3. Keeping JavaScript in a Separate File (.js)
Page.RegisterClientScriptBlock("MyScript", "<script language="javascript" src="'MyJavaScriptFile.js'">")
</p></form>
Best for Java function:Directly write in .aspx file
<script type="text/javascript">
function EmailValidation<%= CustomValidator1.ClientID %>(source, arguments)
{
var obj1=document.getElementById("<%= txtBrokerID.ClientID %>");
var obj2=document.getElementById("<%= txtEmail.ClientID %>");
if (obj1.value.length>0)
{
if (obj2.value.length>0)
arguments.IsValid=true;
else
arguments.IsValid=false;
}
}
</script>
Reference:
ASP.NET Client-Side Script FAQ
No comments:
Post a Comment