Related Articles
In this column, the author lays out some guiding principles that you should follow when working with the ASP.NET MVC framework. Scott Allen MSDN Magazine July 2009 ... Read more!
Listen in on a chat between a developer and security pro that delves into some of the major Security Development Lifecycle (SDL) requirements we impose on product teams here at Microsoft Michael Howard MSDN Magazine May 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!
In this month's installment we build modal and modeless dialog boxes in jQuery and explain how to post data from them to the Web server. Dino Esposito MSDN Magazine May 2009 ... Read more!
This article reviews the Prism project developed by the Microsoft patterns & practices group and demonstrates how to apply it to composite Web applications using Silverlight. Shawn Wildermuth MSDN Magazine July 2009 ... Read more!
Also by this Author
Robert Hess MSDN Magazine July 2000 ... Read more!
Robert Hess MSDN Magazine April 2000 ... Read more!
Robert Hess MSDN Magazine March 2001 ... Read more!
Robert Hess MSDN Magazine May 2001 ... Read more!
Robert Hess MSDN Magazine June 2000 ... Read more!
Popular Articles
Paul DiLascia MSDN Magazine August 2002 ... Read more!
Ray Djajadinata MSDN Magazine May 2007 ... Read more!
Jeff Prosise explains when it's better to use UpdatePanel and when it's better to use asynchronous calls to WebMethods or page methods instead. Jeff Prosise MSDN Magazine June 2007 ... Read more!
When incorporating the ASP.NET DataGrid control into your Web apps, common operations such as paging, sorting, editing, and deleting data require more effort than you might like to expend. But all that is about to change. The GridView control--the successor to the DataGrid-- extends the DataGrid's functionality it in a number of ways. First, it fully supports data source components and can automatically handle data operations, such as paging, sorting, and editing, as long as its bound data source object supports these capabilities. In addition, ...
Read more!
A Sidebar gadget is a powerful little too that's surprisingly easy to create. Get in on the fun with Donavon West. Donavon West MSDN Magazine August 2007 ... Read more!
|
This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.
| .gif) | | Client-side Cookies, Unchecking Checkboxes, Microsoft.com Toolbar, WebBrowser Control, and More | | Robert Hess | Q I have a series of pages on my Web site, with different want ads on each page. Users click on the ads they want to see. In order for me to identify users and put them in touch with the person who placed the ad, I request that users register with a user ID, password, and e-mail address, which they keep for future visits. Before registered users look at the ads, I would like them to enter their user ID and password. Then, when they click on an ad, a form is sent to me with the user ID, password, and the ad number or ID. I don't want to force users to enter their user ID and password every time they click on an ad. I thought the best way around that would be with a cookie I keep in RAM. That way, once the user has entered the data and I've put it in memory, I could pick it up when needed. Unfortunately, my ISP does not allow CGI programs, so I must use a client-side operation. How can I store state information for my users?
A The best solution to your problem is to use client-side cookies for storing temporary information about the user. You can persist this information on the user's machine and it will only be accessible from Web pages on your own domain. The MSDN® Online article "Persistent Client Side Data Using Cookies" shows how you can access client-side cookie data from server-side ASP code. It is far easier to use cookie values from the server, but if you need to do this all on the client side, then you can get or set the cookie value via client-side code. See the cookie property page in the MSDN Library. One drawback to this approach is that the browser may not support cookies because many users turn this feature off. Instructions for detecting whether or not the browser supports cookies can be found in the article "Client Capabilities."
Q I have an HTML file that checks a number of checkboxes (see Figure 1). If you uncomment the line that creates the ActiveX® Dictionary object after you have viewed the page in Microsoft® Internet Explorer 5.5, then click Refresh, the page stops working. In other words, nothing is checked. In Internet Explorer 5.0 this works fine, however. Is this a bug? If so, is there a workaround?
A Actually, it doesn't appear to be a problem with the Dictionary object at all. I commented it back out, and the boxes still don't get checked. However, if you put the following code immediately after the for loop:
alert ("Test");
you'll notice that the checkboxes are checked, then promptly get unchecked when you dismiss the alert dialog. What's happening is that during the rendering of the page you are changing the state of the checkboxes, but the internal code that initializes the checkboxes to their default values of unchecked hasn't run yet. When this code does run, it promptly goes ahead with the default setting, which is unchecked. The solution is to delay your for loop until after the page has been loaded. This can easily be done by changing your inline script code into an init function, and calling it via the onload handler for the body (see Figure 2).
Q I'd like to create the cool toolbar with dropdown menus that's shown on the Microsoft home page (http://www.microsoft.com). I borrowed what I could from the site and I've customized it to an extent, but I can't replace the 25th Anniversary GIF and the Microsoft logo with my own picture (see Figure 3). Can you tell me which files reference those images (toolbar.js, global.js, or local.js), or is there a program that can locate these types of calls for me?
.gif) Figure 3 Microsoft.com Home Page
A Dropdown menus are not something that HTML, Cascading Style Sheets (CSS), or Dynamic HTML specifically support, which means that adding something like that to your Web page requires a fair amount of code and careful event handling in order to make DIVs, links, and graphics behave as if they were menus. Any time you try to borrow complex code from another Web site, you are going to spend some time figuring out exactly what the code does before you can modify it to suit your needs. Your first step should be to pull over all of the HTML, JScript®, and CSS files that the page uses to implement its menus. Then you need to trim the main page to the bare minimum, removing all the form tags, image tags, and so on. When you do this with code from the Microsoft home page, the result is the code shown in Figure 4. If you remove the call to setAds, you'll see that the 25th Anniversary graphic disappears. Since the code file in Figure 4 has virtually nothing left in it except calls to the .js files, it is pretty clear that the various script files being imported are defining the menus, placing them on the page, and inserting the various graphical elements. The easiest way to figure out where these are coming from is to right-click on one of the images on the page, select Properties, get the name of the image that the browser is trying to display. Then you can search through the .js files to find out where this image is being called. Make sure you carefully read through all of the JScript code, otherwise you will continue to run into little problems with making this (or any other borrowed code) work properly for you. My own recommendation is that instead of borrowing someone else's code, you write your own. This is the best way to make sure that the menus behave the way you want them to. But then again, I'm a geek.
Q I'm trying to use the Internet Explorer object within a Microsoft Office application, using Visual Basic® for Applications (VBA), but I don't know how to use the Internet Explorer events.
A To catch the events fired from the Microsoft WebBrowser control in a VBA-based app that you are building within a Microsoft Office application (such as Word), all you have to do is define a subroutine that uses the name of the object you created, an underscore, and the name of the event you want to use as your trigger. For example, the following code will be called during the BeforeNavigate2 event, and will open a message box displaying the URL that the browser is getting ready to navigate to:
Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, _
URL As Variant, _
Flags As Variant, _
TargetFrameName As Variant, _
PostData As Variant, _
Headers As Variant, _
Cancel As Boolean)
MsgBox (URL)
End Sub
Q Is it possible to use variables inside an include statement in ASP? For example, can I use the following code?
<%
dim strfile
strfile = "test.htm"
%>
<-- #include file=<%=strfile%> -->
As you can see, I'm trying to include files dynamically. Can this be done?
A ASP processing first handles all #include directives before starting the script interpreter. As a result, the code you present won't work. However, there are other options. If all you want to do is run some server-side code to determine which page you should send to the client, then the following code will do the trick:
<%
dim strfile
strfile = "test.htm"
Response.Redirect strfile
%>
If you only have a few files that you may want to include, then you could use something like the following code, which would include all of the files in your main page, but only show the contents of one of them:
<% toInclude=2 %>
<% if 1=toInclude then %>
<!-- #include file ="File1.html" -->
<% elseif 2=toInclude then %>
<!-- #include file ="File2.html" -->
<% else %>
<!-- #include file ="File3.html" -->
<%end if%>
Q When I try to update a memo field in Microsoft Access with a special character, I get a database error. Is there any special way to update memo fields using ASP? Also, while displaying the memo field value, the text is not aligning properly according to carriage returns. What is causing this problem?
A When updating text fields in a database, it is important to take into account any special characters associated with formatting the query request for the database. In the ASP code that I use for writing text strings to a database, I always make sure to route them through my DeQuoter function (see Figure 5), which will remove all single quotes contained within the string. Because I'm using single quotes as string delimiters in my SQL commands, I have to convert any single quote actually used within the string into two single quotes, thus making it a quoted quote. As for the problem of retaining line breaks, it isn't really the database that is getting rid of the line breaks; to a certain extent they were never there to begin with. If you were to echo the text out to a div.innerHTML instead of submitting it as a form, you'd see that the carriage returns don't show up. This is because HTML doesn't recognize carriage returns as line breaks. However, if you were to echo the text out as div.innerText, the line breaks would show up where you expect them, which indicates that as far as the data goes, they are still there. So instead of storing text with carriage returns in the database, you should convert them to <BR> tags instead, or better yet, convert the carriage returns to <BR> tags once the data is retrieved from the database. I've prepared a sample ASP file that illustrates all of these ideas (see Figure 6). When using a memo type field, remember that since these fields can be really long, they will be set as a read-once field. This means that once you do a
memo = RS_item("MemoField")
the component discards the data from its memory stream, so you won't be able to do it again! You need to request this type of field once, then hold onto that variable for as long as you might need that information. Otherwise, you'll have to get a fresh copy of the recordset.
Q I'd like to use Word on an ASP page to create a document, send the document back to the server, store the document in a database, and return the document when requested for editing in Word. Is this possible? I've tried using an <OBJECT> element for Word, but that doesn't seem to work.
A One of the problems with this type of document management over HTTP is that the HTTP protocol was not designed to be a bidirectional file transfer mechanism. So while you can read a document on the client via HTTP, writing it back to the server is not so simple. Even when reading a file via HTTP, you will notice that the file isn't simply sent down and copied onto the user's hard drive. Instead, it is saved to a general-purpose cache, and if the user wants to physically store that file on their system for later use, they have to manually redirect or move the file into that location. The most straightforward way to support uploading a file to a Web server is to use the file upload capability that many browsers now support. This is invoked by adding an <input type="file" ...> element to a <form>. Upon submission of the form, a specially formatted data stream (known as multipart) is created, in which all data files that are being passed back to the server are concatenated into a single data stream, with separators between each file that identify the name of the file being transferred. Many Web servers (such as Microsoft Site Server) provide server-side components to make it easy to identify and extract files from these data streams. You can also write Perl code that can walk the streams and split out the files, but you can't use ASP code, since the files are in binary format and the stream support in ASP can't operate at that level. Even if the support for reading and writing files was as easy as opening the file in Word and allowing the user to write it back out again, you would quickly find that you need a much more complex system to support document management. Most likely you need to support the notion of checking out a file for version control. This keeps the file locked from modifications by other users until the original user checks their file back in. Otherwise, you could have multiple users opening the original file and making modifications on it, and then when they all saved their changes back to the server, the files would all overwrite one another, with the last one in overwriting all previous changes. Many usability and synchronization issues need to be thought through to make a fully functional document management system. There are several on the market, including Microsoft Visual SourceSafeâ¢; you just have to find the one that best suits your needs.
Q I have some VML code in my Web page that only works on Internet Explorer 5.0 or greater, so I'd like to do some browser detection and only send the page to the proper browsers. But when I do the query, my Internet Explorer version appears as 5.50.4134.0600 in the About box on Windows® 2000. When I query navigator.appVersion I get:
4.0 (compatible; MSIE 5.5; Windows NT 5.0)
Shouldn't this be MSIE 5.0 ? I can do a quick test with
if( ! (/MSIE 5/).test( navigator.appVersion ) ) complain();
but of course when Internet Explorer 6 comes out, I'll be in trouble. Is this a bug? Is there a better way to do this? The MSDN Library page "AppVersion Property" says navigator.appVersion is supposed to be 5.0 for Internet Explorer 5.5.
A There are two browser version numbers in the appVersion string. In the example that you cite, one is 4.0 and the other is 5.5. I think the number you are confused about is the 4.0. The 4.0 you see in the returned string is not the version of Internet Explorer. It's the version of Mozilla (Netscape Navigator) that the browser is most compatible with, which is why it says "compatible" in the attached comment string. This all came about back in the early days of Web browsers, when Netscape Navigator was the first to support the <table> element. Since this element dramatically changed the way a Web page would be rendered, many Web sites would look at the version string, and if they saw that the browser was the appropriate version of Netscape Navigator, they would send down the table version of the page. But if they saw anything else, they would send down pages that didn't use tables. Later, when other browsers (such as Internet Explorer) added support for the <table> element, it was all for naught since many of the most popular Web sites would continue to send down the version of the page that didn't use tables. The only workaround was to use a technique called cloaking to the userAgent/appVersion string. In other words, the version number being reported represented the version of Netscape Navigator that the browser was claiming to be compatible with. The browser put its real version number in the comment attached to the version string, so that the actual browser version could be detected. You must search the string for "MSIE", then extract any number that follows this string if you want to know the version of Internet Explorer being used.
Q I'm returning some information from a query window to a new results window. The results data and headings are in an HTML- formatted table. My problem is that the results window pops up at full size, which completely obscures the calling window. I need to make the results window smaller than the calling query window, so that the query window is partially visible. (I am running Internet Explorer 5.0 on Windows NT® 4.0 with Service Pack 4.)
A Before I answer your question, I'm compelled to tell you that I think that spawning new windows is sort of like raking your fingernails down a chalkboard. But if you really want to do it, you can always pass the height and width parameters through the window.open call. But since I expect you already knew that, and for whatever reason aren't able to use window.open, here is the way I do this when I have a new window (or even an existing one for that matter) that I need to resize when the page loads. Be sure to put the script code as high up as possible in the page code to make sure that the resize happens first.
<html>
<head>
<script>window.resizeTo(535,screen.height-50);</script>
</head>
<body>
â¢â¢â¢
</body>
</html>
| Robert Hess is currently the host of "The MSDN Show" and is a regular contributor to various areas of MSDN. Send e-mail to webqa@microsoft.com | From the December 2000 issue of MSDN Magazine
|
|