Click to Rate and Give Feedback
Related Articles

Good navigation makes for happy users, and happy users are good for your business. See what makes users happy this month.

Dr. Charles B. Kreitzberg and Ambrose Little

MSDN Magazine March 2009

...

Read more!

As a Web platform, Silverlight should be fast. Don’t keep your users waiting by not heeding these performance tips.

Jeff Prosise

MSDN Magazine March 2009

...

Read more!

See how to get the most out of isolated storage in Silverlight to keep your applications safe.

Justin Van Patten

MSDN Magazine March 2009

...

Read more!

Thanks to selectors and function chaining, jQuery allows you to write compact, cross-browser code.

Dino Esposito

MSDN Magazine March 2009

...

Read more!

With the help of Silverlight Deep Zoom and a remarkable control named MultiScaleImage, you can create scenes with many levels of zoom. Jeff Prosise illustrates with what else but the Mandlebrot set.

Jeff Prosise

MSDN Magazine July 2009

...

Read more!

Popular Articles

One-time passwords offer solutions to dictionary attacks, phishing, interception, and lots of other security breaches. Here's how it all works.

Dan Griffin

MSDN Magazine May 2008

...

Read more!

WPF is one of the most important new technologies in the .NET Framework 3.0. This month John Papa introduces its data binding capabilities.

John Papa

MSDN Magazine December 2007

...

Read more!

See how routed events and routed commands in Windows Presentation Foundation form the basis for communication between the parts of your UI.

Brian Noyes

MSDN Magazine September 2008

...

Read more!

Kenny Kerr sings the praises of the new Visual C++ 2008 Feature Pack, which brings modern conveniences to Visual C++.

Kenny Kerr

MSDN Magazine May 2008

...

Read more!

This article introduces 10 development tools that can increase your productivity, give you a better understanding of .NET, and maybe even change the way that you develop applications. The tools covered include NUnit to write unit tests, Reflector to examine assemblies, FxCop to police your code, Regulator to build regular expressions, NDoc to create code documentation and five more.

James Avery

MSDN Magazine July 2004

...

Read more!

TextAreas, AutoComplete Dropdown Box, JScript Garbage Collection, Caching, and More
Edited by Nancy Michell
Download the code for this article: Web0105.exe (35KB)
Browse the code for this article at Code Center: AutoComplete Listbox

Q I'm trying to size a TextArea element. Is there a property you can set to control the width of a TextArea to be the size of its container, in this case a <TD>? Otherwise, is there a way to do this with DHTML?

A Setting the width of the TextArea to 100 percent will keep it the same size as the table cell it is in. The following code shows how it's done:
<TABLE width="100%">
    <TR>
        <TD><TEXTAREA style="width: 100%">I am in a table</TEXTAREA>
        </TD>
        <TD width="130px">I am just to the right of the TEXTAREA</TD>
    </TR>
</TABLE>
<TEXTAREA style="width: 100%">I am not!</TEXTAREA>
Q In a Web page, is there any way to implement an edit box with a dropdown list that will AutoComplete the text entered with items in the dropdown?

A Sounds like you've got some DHTML programming on your hands. My suggestion is turn it into a behavior so others can reuse it. The basic strategy is to have a little keyboard buffer containing what the user has typed and then walk through the items in the listbox looking for a near match. The keyboard buffer is automatically cleared after a short interval, so that the user can start over with a new string. By default, the timeout is .75 seconds (250 milliseconds × 3 timeout countdowns)â€"that is, if the user hesitates longer than .75 seconds, the buffer is cleared and whatever the user types in next starts a new string. The timeout is accomplished using the setInterval function of the window object, a standard way in JScript® to yield script execution.

Figure 2 The Listbox in Action
Figure 2 The Listbox in Action

      Figure 1 shows one way to build a listbox so that when the user types some letters, the listbox jumps to the closest match. Figure 2 shows the code in action.

Q Can someone point me to some good documentation for garbage collection in JScript? I need to find out how JScript cleans up.

A JScript uses a mark-and-sweep garbage collector with a variety of heuristics used to determine when to run garbage collection. The JScript garbage collector works like this:
  1. When the script engine is shut down, garbage is collected.
  2. When 256 variants, or more than 64KB of strings, or more than 4096 array slots have been allocated, the garbage collector sets a flag that says collect soon.
  3. Whenever a new statement is executed or the script debugger starts, that flag is checked, and if it is set, a collection is done.
      There is an undocumented JScript function called CollectGarbage that forces a garbage collection. This is for testing purposes onlyâ€"do not ship code that calls this function. It is a poor programming practice to write code in JScript that depends on garbage collections being done at particular times. If you need predictable garbage collection, use a language that supports it (like Visual Basic® or VBScript). Note that all of this is the implementation detail of the engine and should not be relied upon because it may change in the future. Note also that the version of JScript supported by Microsoft® .NET will use the .NET Framework garbage collector, a multigenerational mark-and-sweep collector.
      And remember, if you want a deterministic-lifetime app, use a deterministic-lifetime language like C++, Visual Basic 6.0, or VBScript; not an indeterministic-lifetime language like JScript, Scheme, or Java. If you're writing a program that depends on being able to have a deterministic object lifetime, JScript is not the right tool for the job. Trying to make it a deterministic-lifetime language will just create headaches down the road.

Q I have three questions about writing HTML and optimizing Web development. What is the syntax for the meta tag that makes the browser send images to the cache? What meta tag makes the page always expire? I have some pages with a lot of client-script code and I want this code to be cached, too. If my scripts are in the form
<SCRIPT src="file1.js"></SCRIPT>
will file1.js be cached? If not, how can I avoid subsequent downloads of that file?

A The meta tag that forces the page to expire immediately is
<META HTTP-EQUIV="Expires" CONTENT="0">
There are no specific tags that control the caching of images or any other item in particular. By setting the Expires tag, the browser will check for a new version of all the files every time the page is loaded.
      Standard browser behavior is to cache everything unless told to do otherwise on either the server or the client. In the event that you want to prevent caching, use this:
<meta http-equiv="pragma" content="no-cache">
Q Call me weird, but I've got an ASP/ADO project that uses JScript as the language the ASP is written in. I need something similar to the VBScript on error resume next. I can test for a recordset.EOF, but I need more than that. I need to gracefully trap errors when the server running SQL Server™ goes off-line (ADO returns 'Specified SQL server not found'). I would also like to know if there is a graceful way to trap all ADO errors and tell the client something nice like, "There is a problem, but we know about it. Please don't panic."

A You need a try/catch block. Here is a rough example.
rs = new ActiveXObject("ADODB.Recordset");
strSQL = "myStoredProc"
try {
rs.Open(strSQL, Session("sDSN"),3);
    }
catch(e)
    {
    if (e instanceof Error)
        {
        Response.Write(e.description);
        Response.End();
    }
            }
Got a question? Send questions and comments to webqa@microsoft.com.
Thanks to the following Microsoft developers for their technical expertise: Doug Anderson, Matthew Barbour, Raymond Chen, John Jansen, Scott Jordan, Praveen Kosuri, Eric Lippert, Mauricio Lorenzon, Michael Pizzo, Mike Pope, Eric Schurman, Chris Sires, Tushar K. Sukale, Adam Vandenberg.

From the May 2001 issue of MSDN Magazine

Page view tracker