⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ba42.htm

📁 VB教程
💻 HTM
字号:
<HTML>
<HEAD>
<TITLE>二、实现代码</TITLE>
 
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
 
</head>
<p align="center"><script src="../../1.js"></script></a>
<BODY topMargin=4 vLink=#0000ff>
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
  <TBODY>
  <TR>
    <TD height="52"> 
      <DIV align=center>
      <CENTER>
          <table border=0 cellpadding=0 cellspacing=0 width=679 align="center">
            <tbody> 
            <tr> 
              <td width=200 height="59"> 
                 
    </TD></TR><!--msnavigation--></TBODY></TABLE>
<table border=0 cellpadding=0 cellspacing=0 width="100%">
  <tbody> 
  <tr><!--msnavigation--><td valign=top height="123"> 
      <div align=center> 
        <table border=1 bordercolor=#b9d9ff cellpadding=0 cellspacing=0 
      class=table width=755>
          <center>
            <tr> 
               
              <td width="100%" height="3">
                <div align="center"> <strong><b>二、实现代码</b> 
                  </strong></span></div>
              </td>
            </tr>
            <tr> 
              <td 
            width="100%" class="unnamed1" height="124"> 
                <div align="left">
                  <p align="center">&nbsp;</p>
                  <p align="right"><font face="宋体" size="-1">作 者 : 仙人掌工作室 编译 </font></p>
                  <p align="left"><font face="宋体" size="-1">   接下来就可以为这个IIS应用程序输入代码了。首先,在WebClass界面中,我们需要为表单提交动作定义一个自定义事件,这只需要双击WebClass设计器右面窗格的Form1标记即可。就象通常在ASP应用程序中所作的那样,在该事件中要完成的任务是用户输入数据的合法性验证,以及验证通过后将这些数据保存到数据库。具体实现请参见下面代码清单中的GuestRegister_Form1过程。 
                    </font></p>
                  <p><font face="宋体" size="-1">   IIS应用程序应该指定用户首次访问时所显示的内容。为此,我们在WebClass_Start事件中将WebClass的NextItem属性设置为GuestRegister模板,然后在GuestRegister模板的Respond事件中用模板的WriteTemplate方法将页面发送到浏览器。 
                    </font></p>
                  <p><font face="宋体" size="-1">   在将页面发送到浏览器时,系统会遇到页面中的wc@定制标记,此时它会自动调用GuestRegister的ProcessTag事件,并把标记名字作为参数传递给事件处理过程GuestRegister_ProcessTag。在这个过程中我们应当返回插入文档内定制标记所在位置的HTML代码。在本实现中我们使用了一个Select 
                    Case语句,它按照标记的不同设置该事件的TagContents参数。系统会自动将TagContents的内容写入发送到浏览器的页面。 
                    </font></p>
                  <p><font face="宋体" size="-1">   在GuestRegister_ProcessTag中我们将这些HTML输入元素的缺省值设置为对应的全局变量值。当需要在GuestRegister_Form1过程中验证用户输入数据的合法性时,则重新从HTML输入元素取回这些全局变量的值。因此,当用户输入数据错误时程序将重新显示它们以避免重复输入。 
                    </font></p>
                  <pre><font face="宋体" size="-1">
     Option Explicit

     Option Compare Text

     ' 与表单输入元素对应的全局变量声明

     Dim chrUserName As String

     Dim chrAddress As String

     Dim chrZipCode As String

     Dim chrEmail As String

     Dim chrPhone As String

     Dim txtComments As String

     Dim strError As String

     Sub InsertGuestReg()

     ' 将用户输入数据插入数据库

     Dim sql As String

     Dim dbConnection As New ADODB.Connection

     

     ' 打开数据库连接

     dbConnection.Open &quot;GuestRegister&quot;

     ' 构造SQL语句

     sql = &quot;insert into guestregister(&quot; &amp; _

           &quot;chrUserName,&quot; &amp; _

           &quot;chrAddress,&quot; &amp; _

           &quot;chrZipCode,&quot; &amp; _

           &quot;chrEmail,&quot; &amp; _

           &quot;chrPhone,&quot; &amp; _

           &quot;txtComments) &quot; &amp; _

           &quot;values('&quot; &amp; _

           chrUserName &amp; &quot;', '&quot; &amp; _

           chrAddress &amp; &quot;', '&quot; &amp; _

           chrZipCode &amp; &quot;', '&quot; &amp; _

           chrEmail &amp; &quot;', '&quot; &amp; _

           chrPhone &amp; &quot;', '&quot; &amp; _

           txtComments &amp; &quot;')&quot;

     

     ' 插入记录

     dbConnection.Execute sql

     ' 关闭数据库连接

     dbConnection.Close

     End Sub

     

     Private Sub GuestRegister_Form1()

     ' 用户输入数据验证

     strError = &quot;&quot;

     

     ' 验证用户输入数据的合法性

     chrUserName = Request(&quot;chrUserName&quot;)

     If chrUserName = &quot;&quot; Then strError = strError &amp; _

        &quot;&lt; i&gt;用户名字不能为空.&lt; /i&gt;&quot;

     chrAddress = Request(&quot;chrAddress&quot;)

     If chrAddress = &quot;&quot; Then strError = strError &amp; _

        &quot;&lt; BR&gt;&lt; i&gt;地址不能为空.&lt; /i&gt;&quot;

     chrZipCode = Request(&quot;chrZipCode&quot;)

     If chrZipCode = &quot;&quot; Then strError = strError &amp; _

        &quot;&lt; BR&gt;&lt; i&gt;邮编不能为空.&lt; /i&gt;&quot;

     chrEmail = Request(&quot;chrEmail&quot;)

     If chrEmail = &quot;&quot; Then

       strError = strError &amp; _

        &quot;&lt; BR&gt;&lt; i&gt;Email地址不能为空.&lt; /i&gt;&quot;

     Else

       If InStr(1, chrEmail, &quot;@&quot;) = False Then

          strError = strError &amp; &quot;&lt; BR&gt;&lt; i&gt; &quot; &amp; _

                    &quot; Email地址格式错误.&lt; /i&gt;&quot;

       End If

     End If

     

     chrPhone = Request(&quot;chrPhone&quot;)

     txtComments = Request(&quot;txtComments&quot;)

     

     ' 如果出现错误,则重新显示注册表单

     ' 否则,保存数据并提示

     If strError &lt; &gt; &quot;&quot; Then

       Set NextItem = GuestRegister

     Else

       InsertGuestReg

       Response.Write &quot;谢谢!&quot;

     End If

     End Sub

     

     Private Sub GuestRegister_ProcessTag(ByVal TagName As String, _

             TagContents As String, SendTags As Boolean)

     '生成注册表单中的输入元素

     Select Case LCase(TagName)

       ' 错误信息

       Case &quot;wc@error&quot;

         '  若有错误信息要显示

         '  则将它显示在&lt; wc@error&gt;所在位置

         If strError &lt; &gt; &quot;&quot; Then

           TagContents = &quot;&lt; HR&gt;&lt; font color=&quot;&quot;red&quot;&quot;&gt;&quot;

           TagContents = TagContents &amp; _

                        &quot;你没有正确填写表单. &quot; &amp; _

                        &quot;请再试一次.&lt; BR&gt;&lt; BR&gt;&quot;

           TagContents = TagContents &amp; strError

           TagContents = TagContents &amp; &quot;&lt; /font&gt;&lt; BR&gt;&lt; HR&gt;&quot;

         End If

     

       ' 客户名字

       Case &quot;wc@username&quot;

         TagContents = &quot;&lt; input type=&quot;&quot;text&quot;&quot; value=&quot;&quot;&quot; &amp; _

                   chrUserName &amp; &quot;&quot;&quot; name=&quot;&quot;chrUserName&quot;&quot;&gt;&quot;

       ' 地址

       Case &quot;wc@address&quot;

         TagContents = &quot;&lt; input type=&quot;&quot;text&quot;&quot; value=&quot;&quot;&quot; &amp; _

           chrAddress &amp; &quot;&quot;&quot; name=&quot;&quot;chrAddress&quot;&quot;&gt;&quot;

       ' 邮政编码

       Case &quot;wc@zipcode&quot;

         TagContents = &quot;&lt; input type=&quot;&quot;text&quot;&quot; value=&quot;&quot;&quot; &amp; _

           chrZipCode &amp; &quot;&quot;&quot; name=&quot;&quot;chrZipCode&quot;&quot;&gt;&quot;

       ' Email地址

       Case &quot;wc@email&quot;

         TagContents = &quot;&lt; input type=&quot;&quot;text&quot;&quot; value=&quot;&quot;&quot; &amp; _

           chrEmail &amp; &quot;&quot;&quot; name=&quot;&quot;chrEmail&quot;&quot;&gt;&quot;

       ' 电话

       Case &quot;wc@phone&quot;

         TagContents = &quot;&lt; input type=&quot;&quot;text&quot;&quot; value=&quot;&quot;&quot; &amp; _

           chrPhone &amp; &quot;&quot;&quot; name=&quot;&quot;chrPhone&quot;&quot;&gt;&quot;

       ' 其它说明.

       Case &quot;wc@comments&quot;

         TagContents = &quot;&lt; textarea name=&quot;&quot;txtComments&quot;&quot;&gt;&quot; &amp; _

           txtComments &amp; &quot;&lt; /textarea&gt;&quot;

     

     End Select

     End Sub

     Private Sub GuestRegister_Respond()

     '  输出页面

     GuestRegister.WriteTemplate

     End Sub

     

     Private Sub WebClass_Start()

     ' 启动时将GuestRegister设置为第一个页面

     Set NextItem = GuestRegister

     End Sub
</font></pre>
                  <p><font face="宋体" size="-1">   GuestRegister_Form1过程中我们验证了除Phone和Comments之外的所有输入数据。若有任何错误存在,则向用户显示错误信息。如果用户输入的所有数据都通过合法性验证,程序调用InsertGuestReg过程将这些数据保存到数据库。下表显示了我们所使用的Access数据库表GuestRegister的结构,数据库的系统DSN为GuestRegister: 
                    </font></p>
                  <p><font face="宋体" size="-1"> </font></p>
                  <table border="1" class="font">
                    <tr> 
                      <td align="middle" bgcolor="#3399cc" colspan="4">GuestRegister表结构 
                      </td>
                    </tr>
                    <tr> 
                      <td align="middle" bgcolor="#66aaff" width="150">字段名称</td>
                      <td align="middle" bgcolor="#66aaff">类型</td>
                      <td align="middle" bgcolor="#66aaff">大小</td>
                      <td align="middle" bgcolor="#66aaff" width="100">说明</td>
                    </tr>
                    <tr> 
                      <td bgcolor="#ccffff" noWrap>idGuestReg</td>
                      <td bgcolor="#ccffff" noWrap>自动编号</td>
                      <td bgcolor="#ccffff" noWrap>长整形</td>
                      <td bgcolor="#ccffff" noWrap>主键</td>
                    </tr>
                    <tr> 
                      <td bgcolor="#ccffff" noWrap>chrUserName</td>
                      <td bgcolor="#ccffff" noWrap>文本</td>
                      <td bgcolor="#ccffff" noWrap>50</td>
                      <td bgcolor="#ccffff" noWrap> </td>
                    </tr>
                    <tr> 
                      <td bgcolor="#ccffff" noWrap>chrAddress</td>
                      <td bgcolor="#ccffff" noWrap>文本</td>
                      <td bgcolor="#ccffff" noWrap>50</td>
                      <td bgcolor="#ccffff" noWrap> </td>
                    </tr>
                    <tr> 
                      <td bgcolor="#ccffff" noWrap>chrZipCode</td>
                      <td bgcolor="#ccffff" noWrap>文本</td>
                      <td bgcolor="#ccffff" noWrap>50</td>
                      <td bgcolor="#ccffff" noWrap> </td>
                    </tr>
                    <tr> 
                      <td bgcolor="#ccffff" noWrap>chrEmail</td>
                      <td bgcolor="#ccffff" noWrap>文本</td>
                      <td bgcolor="#ccffff" noWrap>50</td>
                      <td bgcolor="#ccffff" noWrap> </td>
                    </tr>
                    <tr> 
                      <td bgcolor="#ccffff" noWrap>chrPhone</td>
                      <td bgcolor="#ccffff" noWrap>文本</td>
                      <td bgcolor="#ccffff" noWrap>50</td>
                      <td bgcolor="#ccffff" noWrap> </td>
                    </tr>
                    <tr> 
                      <td bgcolor="#ccffff" noWrap>txtComments</td>
                      <td bgcolor="#ccffff" noWrap>文本</td>
                      <td bgcolor="#ccffff" noWrap>100</td>
                      <td bgcolor="#ccffff" noWrap> </td>
                    </tr>
                  </table>
                  <p><font face="宋体" size="-1">   一旦新的用户记录插入成功,则程序向用户发送提示信息“谢谢”。下图显示了表单运行时的外观。 
                    </font></p>
                  <p><font face="宋体" size="-1">   【图2 VBIISApplications_2.gif】 
                    <br>
                    <img src="../pic/ll/VBIISApplications_2.gif" tppabs="http://www.pcbyte.net/program/VBIISApplications_2.gif" width="306" height="338"> 
                    </font></p>
                  <p><font face="宋体" size="-1"> </font></p>
                  <p><font face="宋体" size="-1">   三、小结 </font></p>
                  <p><font face="宋体" size="-1">   这就是客户注册器的全部实现过程了。从这个简单的示例中,我们可以看到Web用户界面与传统Visual 
                    Basic编程的结合。在IIS应用程序中,我们可以使用所有传统的编程方法——如面向对象,数据抽象等等,来描述应用逻辑;而在用户界面方面,则用Web页面取代传统的VB窗体。 
                    </font></p>
                  <p align="center"><font face="宋体" size="-1"><a href="ba8.htm" tppabs="http://www.pcbyte.net/program/185.htm">上一页</a></font></p>
                  <p><font face="宋体" size="-1">转载自Chinabyte网络学院</font>
                </div>
              </td>
            </tr>
          </center>
          <tr> 
            <td width="100%" class="unnamed1"> 
              <p align=right><a href="ba41.htm">(上一页)</a>---<a href="ba43.htm">(下一页)</a></p>
            </td>
          </tr>
          <tr> 
            <td width="100%" class="unnamed1"> 
               
    </div>
      </td>
  </tr>
  <!--msnavigation--></tbody>
</table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -