Tip

Using JavaScript to save roundtrips

This tip was submitted to the VS.NET Info Center by member Clive Chinery. Please let other users know how useful it is by rating it below. Do you have a tip or code of your own you'd like to share? Submit it here.


As part of a recent project, it was necessary to set a check box to true, when the user typed a search string into the text box. This would have required at least one roundtrip to the server, which would have had a server impact on the application responsiveness, unless some client code was used. The code example below shows how this was done using JavaScript. Note the JavaScript and the ASP.NET code to invoke the function via "onkeyup." The "onkeyup" is CASE sensitive.

Code:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DEMO.aspx.vb"
Inherits="Probe.DEMO"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title>DEMO</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <script language="javascript">
    function txtFredChange() { var theform = document.Form1;
  theform.chkFred.checked =1;    }    
    &tl;/script>  
  </HEAD>
  <body>
    <form id="Form1" method="post" runat="server">
      <asp:TextBox id="txtFred" onkeyup="txtFredChange()"
runat="server"></asp:TextBox>
      <asp:CheckBox id="chkFred" runat="server" Text="Select using
this"></asp:CheckBox>
    </form>
  </body>
</HTML>

This was first published in April 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.