Using Client Side Script in ASP.Net Pages

Using client side script in a asp.net page can be very effective, in this example I use a small amount of code to enable and disable a button depending on the contents of a text box. Because Im using

ITPro Today

October 20, 2003

1 Min Read
ITPro Today logo

Using client side script in a asp.net page can be very effective, in this example I use a small amount of code to enable and disable a button depending on the contents of a text box. Because Im using client side script though there is no need for a post back, this gives a much quicker and more professional feel to the page.1. Specify the script'Used for the embedded script Private ChecktxtIDIncludeScriptKey As String = "CheckandDisabletxtID" Private EmbeddedScriptFormat As String = _ ""First you specify the script you then need to register the script2. Register the javascript, if it is not already registered Protected Sub RegisterCommonScript() Dim location As String = "" 'Embedded Client Script If (Not Page.IsClientScriptBlockRegistered(ChecktxtIDIncludeScriptKey)) Then Page.RegisterClientScriptBlock(ChecktxtIDIncludeScriptKey, EmbeddedScriptFormat) End If End Sub3. Call the script registration from the OnPreRender method'actions to carry out before the page is rendered Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs) MyBase.OnPreRender(e) RegisterCommonScript()End subTIP. You need to specify the .id for any controls that the script interacts with as you acces them by there id e.g document.getElementById('btnNew'.4. Finnaly you add an attribute to youre control like thism_txtID.Attributes.Add("onKeyUp", "ChecktxtID();")in order for the script to be called when required.Thats it. As you can probably see this can be a very powerful tool when creating ASP.Net pages.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like