Using JavaScript to save roundtrips

Client code to set a check box to true when the user types a search string into a text box without having to make a roundtrip to the server.

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 last published in April 2003

Dig Deeper on .NET tutorials, guides and quizzes

Start the conversation

Send me notifications when other members comment.

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

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close