Creating a Server Component with VB - Redesigned - Part 2[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 sonicdater 的 blog

creating a server component with vb - redesigned - part 2
by doug dean
rating: 4.1 out of 5
rate this article


print this article

email this article to a colleague

introduction

this is part 2 of an article that demonstrates how to design a maintainable server-side dll that can be called from an asp page and rendered in any browser. this part shows how to apply the ideas discussed in part 1 (see http://www.15seconds.com/issue/010730.htm). it will cover different aspects of retrieving data from a database and how best to work with that data within a visual basic (vb) dll so that the method string that is returned contains a structure ready to be rendered within a browser.

returning a compact browser-ready string from a component can be very convenient for the end developer, although not without issue. debates regarding the sanctity of the separation between presentation and business tiers are not merely academic. i have found that speed can be as important as maintenance, especially with internet applications running on servers. just as in the normalization process related to database table design, sometimes allowing a little practical redundancy goes a long way in relieving processing burdens and speeding up transactions. in the case of populating an html table with values from a database in an asp/vb environment, many different options are open to the programmer, as mentioned in the first part of this article.

the code demonstrated here will construct fully structured html table data records encapsulated between <td></td> tags within the vb code itself. the values used to construct this return string, containing ready-to-render html table data, will come from three sources. the data contained within the vb-constructed html table tags will be stored within a database table. values that deal with controlling the immediate retrieval of this data, such as which database record to display, will be sent to the vb code via method parameters that originate from the asp file that calls the method. presentation data, the html table td and font tag values, will be retrieved from a separate database table, although default values for these variables will also be written into the vb code itself.

because of the "code application" nature of this second article, there's much more example code than in the first article. i've tried to simplify the entirety of the example code as much as possible without omitting any of the processes that this article is attempting to exemplify. the difficulty with a program of this complexity is preserving the overall conceptual nature of a significant amount of code while presenting it in smaller, topical sections. most of the following example code sections can be placed within vb and run "as-is," although some example code fragments are also included and are labeled as such. once the functioning of a section of example code is explained, any further code added to the same example code will be highlighted in bold so that you can more easily see the newly introduced code and how it functions within the context of previously discussed code.

  • download source code and other documentation for this article.

  • what the example application does

    the processes the example application will demonstrate are:

    • how to utilize vb method parameter variables sent from a calling asp file.
    • vb code that utilizes database transactions.
    • vb code that utilizes the getrows() method for retrieving database data.
    • how to access data in a variant variable populated by the getrows() method to construct an html-ready formatted string.
    • using a database "style table" that can provide a quick way to modify html presentation code.
    • the use of string buffering to avoid relying on vb's processing-intense concatenation operator.
    • a way to utilize error code to inform the calling asp file when a database record is not successfully retrieved by the vb code.
    this is a tall order for a simple example program that's best kept as uncomplicated as possible. the application i picked demonstrates all these processes, although the database transaction code is included without a practical transactional application.

    this example application is very limited and lacks vb methods for adding, editing, and deleting the text used in the examples from the database. but then again, this is just enough code to demonstrate the processes needed for you to add these features yourself -- if you feel inclined to do so.

    the example vb project, named noteproject, populates an html table record with two table record data areas. the left table record data area contains a list of hyperlinked titles that are stored in a database. in our example code, we'll list some of the topics found in this article. when a user selects any of these title links, database-stored text related to the title will fill the table record area to the right of the titles. so that users won't get confused, we'll provide a way to alter the color of the currently selected title and remove its hyperlink.

    the vb code produces html that displays the following table record data:

    we'll get the titles from the database using the getrows() method, which conveniently stores our record fields in a multidimensional array. in order to identify which title the user selects, a query string will be attached to the end of each hyperlinked title within the html code. this query string will use the titles' noteid database field value, which is unique for each title.

    here's an example of how the title html <a> tags are structured within the table td tags sent back from our vb code to an asp file, which we'll name noteexample.asp. notice that the user must have selected the "buffering" title since <a> tags don't enclose it.

    
    <td width="74" nowrap valign="top">
    <font face="verdana" size="1" color="gray">
    <a href="noteexample.asp?id=1">intro</a><br>
    <a href="noteexample.asp?id=2">style tables</a><br>
    <a href="noteexample.asp?id=3">getrows()</a><br>
    buffering<br>
    <a href="noteexample.asp?id=5">transactions</a><br>
    </font>
    </td>
    
    
    the vb code will send back the <td></td> tags along with the tags' content. we'll also provide <font></font> tags, even though the <a> tag will override the font title colors. we can use this to our advantage and set the font color property value to a different color than the page's body link value so that the selected title will display a different color than the linked titles. remember, our vb code will omit the <a></a> tags for the title that is selected by the user, thereby using the font tag color parameter rather than the body link color parameter.

    the url for each title will send the user back to the current asp file being displayed -- basically having the asp file call itself when one of the titles is selected. the vb code will also assign a query string named "id" to the end of each titles' url within the <a> tag. the value of the id query string will match each title's unique database record noteid value. every time our example noteexample.asp file is called with an id query string attached to the end of its url, we'll send the id query strings value to the vb code as a method parameter value so that the vb code will know which title was selected. so whether a url with an id query string is called from within the asp file, or from another internet link, the vb code will display the text of the title whose noteid equals the id query string value.

    below is the line of asp code that will precede the call to the vb method and assign the "id" query string variable value to a local asp file variable. basically, this line of code sets the local asp variable named aspnoteid to the noteid value of the title that the user selected. we'll then use the aspnoteid variable as a vb method parameter to let the vb method code know which notes' title text to display.

    
    aspnoteid = request.querystring("id")
    
    
    if the noteexample.asp file is called without a query string named id, then the following line of asp code will set the aspnoteid to the default of 1. sending the vb method code a parameter value that is less than 1, or greater than the number of titles in the database table, would cause an error.
    
    if aspnoteid < 1 then aspnoteid = 1
    
    
    the right side of our html table re

    本文关键:Creating a Server Component with VB - Redesigned
     

    本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

    go top