手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Asp.Net编程>列表

Creating a Server Component with VB - Redesigned - Part 2

来源:互联网 作者:西部数码 时间:2008-04-10
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
The intStyleID is the second method parameter that is sent from the calling ASP file code. It''''s an optional method parameter with a default value of zero, if no value is sent to the method (or deliberately set to zero). We''''ll use the default value as a way of telling whether the developer using the method wants to use a customized HTML format that is stored in the database.

But first, the code assigns the default values to the HTML style variables if intStyleID is set to zero.


  ''''///// SET NOTE STYLE PROPERTIES ///////////////////////////////
    If intStyleID = 0 Then
    
        ''''///// SET NOTE STYLE TO DEFAULT VALUES
        ''''///// Note Title Default Style Values
        strTitleDataStart = "<TD WIDTH=""74"" NOWRAP VALIGN=""top"">"
        strTitleFontStart = "<FONT FACE=""Verdana"" SIZE=""1"" COLOR=""GRAY"">"
        strTitleFontEnd = "</FONT>"
        strTitleDataEnd = "</TD>"
        
        ''''///// Note Text Default Style Values
        strTextDataStart = "<TD WIDTH=""124"" VALIGN=""top"" BGCOLOR=""#EFEFEF"">"
        strTextFontStart = "<FONT FACE=""Verdana"" SIZE=""1"" COLOR=""NAVY"">"
        strTextFontEnd = "</FONT>"
        strTextDataEnd = "</TD>"

The code also conditionally sets the HTML style variables from a database record if intStyleID is set to a value other than zero.

    Else
    
        ''''///// SET NOTE STYLE FROM VALUES IN DATABASE
        sql = "SELECT * FROM NoteStyleTable WHERE intStyleID = " & intStyleID
        objCmd.CommandText = sql
        objCmd.CommandType = adCmdText
        Set objCmd.ActiveConnection = objConn
        Rs.Open objCmd
    
        ''''///// Note title database style values
        strTitleDataStart = "" & Rs.Fields("strTitleDataStart")
        strTitleFontStart = "" & Rs.Fields("strTitleFontStart")
        strTitleFontEnd = "" & Rs.Fields("strTitleFontEnd")
        strTitleDataEnd = "" & Rs.Fields("strTitleDataEnd")
            
        ''''///// Note text database style values
        strTextDataStart = "" & Rs.Fields("strTextDataStart")
        strTextFontStart = "" & Rs.Fields("strTextFontStart")
        strTextFontEnd = "" & Rs.Fields("strTextFontEnd")
        strTextDataEnd = "" & Rs.Fields("strTextDataEnd")

        ''''///// Close this Recordset        
        Rs.Close

    End If

The database code should be more than familiar to you now. All the fields from the NoteStyleTable are being retrieved from the record that is indicated by the intStyleID value. The GetRows() method is not used since we''''re not looping through a set of records. Notice that this code lacks the error-handling code demonstrated in the note title and text database code. It''''s left out here for clarity sake, but you''''d want to include error handling since a nonexistent intStyleID value could be sent.

Here''''s the string that is returned from this example code when the default style values are used.

The above code returns the following:


<TD WIDTH="74" NOWRAP VALIGN="top">
<FONT FACE="Verdana" SIZE="1" COLOR="GRAY">Test</FONT>
</TD>
<TD WIDTH="124" VALIGN="top" BGCOLOR="#EFEFEF">
<FONT FACE="Verdana" SIZE="1" COLOR="NAVY">Another Test</FONT>
</TD>

Another option is to have multiple default settings that are checked with ElseIf statements within the If/Then code. By doing this, a developer can set the intStyleID method parameter to negative values (-1, -2, -3, etc.) for various other default settings and positive values (1, 2, 3, etc.) for customizable settings that are stored in the database.

Constructing the Return String

The code fragment in this section uses concatenation to build up the string that fuses the HTML style variable values and the database note information. In the next code section, string buffering will be demonstrated as an optional, and optimal, replacement for VB''''s native string concatenation.

The code here assigns the VB construct vbCrLf to the ends of each HTML tag so that the HTML source code will be easier to read. You can remove these line breaks to save a few bits of HTML source code space at the expense of clarity. They are optional.

The basic structure of this code appears below. Notice that the first table we construct is the note titles table. This is where our vRecordArray will be used. Since multiple note titles are retrieved from the database, we''''ll loop through them while constructing an <A> HTML tag with a query string set to each title''''s NoteID. We''''ll also omit the <A> tags for the title that was selected by the user.


CONSTRUCT THE NOTE TITLES TABLE

Include the beginning table data and font tags
Loop through the vRecordArray array
Include an <A> tag if not the selected NoteID 
Include the note title text from the array 
Include an </A> tag if not the selected NoteID 
End the title line with a line break
End looping
Include the ending font and table data tags

CONSTRUCT NOTE TEXT BODY 
Include the beginning table data and font tags
Include the note text body
Include the ending font table data tags

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!