Using Client Side Script in ASP.Net Pages

Using client side script in a 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 script 2. 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 Sub 3. 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 sub TIP. 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 this m_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.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.