• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

表示代码与逻辑代码分离

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网 <%@ Page Inherits="MyCodeBehind" Src="c2.vb" %>

There is a nice section in the quickstart docs on this topic also. Click here to read up on it!

Here is the code

This example uses the following
MS-SQL Server 7.0 database
Stored Procedure
Component1a.aspx (HTML File)
c2.vb
Component1a.aspx (The Page that is the UI)
<%@ Page Inherits="MyCodeBehind" Src="c2.vb" Debug="True" trace="True" %>

<script language="VB" runat="server">

Sub Page_Load(Sender As Object, E As EventArgs)

response.write("SMILE!!! I love learning new things everyday")

End Sub
</script>


<html>
<head>
<title>Component Page 1</title>
</head>
<body>
<table border=0 cellpadding=3 cellspacing=3>
<tr bgcolor="#CCCCCC">
<td>
<font face="Arial, Helv" size="-1">
Please fill out this form to create a new user profile for your
Company‘’s Component.
<br>
Once this information is gathered you will not need to enter it again and you will be able to update anytime.
<p>
Use the button at the bottom of this page to continue when you are finished.
<br>
</font>
</td>
</tr>
</table>

<font size="+1"><b><font color="#ff0000">*=Required Fields</b><br>

<form method="Post" name="form1" runat="server">
<table> <tr>
<td align=right>
<asp:Label id="Label1" Text="Company Name" Font-Name="Verdana" Font-Size="10pt" Width="200px" BorderStyle="solid" BorderColor="#cccccc" runat="server"/>
</td>
<td>
<asp:TextBox id="CompanyName" size="30" runat="server" />
<asp:RequiredFieldValidator ControlToValidate="CompanyName" Display="Dynamic" errormessage="You must enter your name!" runat=server/>
</td>
</tr>
<tr>
<td align=right>
<asp:Label id="Label2" Text="Company URL" Font-Name="Verdana" Font-Size="10pt" Width="200px" BorderStyle="solid" BorderColor="#cccccc" runat="server"/>
</td>
<td>
<asp:TextBox id="CompanyURL" size="30" runat="server" />
</td>
</tr>

</font>
<tr>
<td align=right>
<asp:Label id="Label3" Text="Contact Email" Font-Name="Verdana" Font-Size="10pt" Width="200px" BorderStyle="solid" BorderColor="#cccccc" runat="server"/>
</td>
<td>
<asp:TextBox id="EmailAddress" size="30" runat="server" maintainstate="false" />
<asp:RegularExpressionValidator ControlToValidate="EmailAddress" ValidationExpression="[\w-]+@[\w-]+\.(com|net|org|edu|mil)" Display="Dynamic" Font-Name="verdana" Font-Size="9pt" ErrorMessage="Must use a valid email address." runat="server"> </asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ControlToValidate="EmailAddress" Display="dynamic" Font-Name="verdana" Font-Size="9pt" ErrorMessage="‘’Email‘’ must not be left blank." runat=server> </asp:RequiredFieldValidator> </td>
</tr>
</table>

<table border=0 bgcolor="#CCCCCC" cellpadding=3 cellspacing=3 width="490">
<tr>
<td width="100%" colspan="2">
<asp:Button id="Button1" Text="Create Profile" OnClick="Button1_Click" Runat="server"/>
</td>
</tr>
</table>
</form>

</body>
</html>




c2.vb File(This File Contains the business logic that is inherited just like a compiled DLL
Option Strict Off

Imports System
Imports System.DateTime
Imports System.Globalization
Imports System.Data
Imports System.Data.SQL
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Public Class MyCodeBehind : Inherits Page
‘’Declare a public variable to hold the Identity Value
Public strID as String

Public Sub Button1_Click(Sender As Object, E As EventArgs)

If Page.IsValid Then

Dim MyConnection As SQLConnection

MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=aspfree")

‘’Puts the local date in variable to be written to the database
dim d as dateTime
d = now()
d = d.Date()

‘’Using the request.form, this retrieves the variables out of the forms collection

Dim MyCommand = New SQLCommand("sp_ComponentCompanyInfo", MyConnection)

MyCommand.CommandType = CommandType.StoredProcedure

MyCommand.Parameters.Add(New SQLParameter("@CompanyName", SQLDataType.VarChar, 50))
MyCommand.Parameters("@CompanyName").Value = request.Form("CompanyName")

MyCommand.Parameters.Add(New SQLParameter("@CompanyURL", SQLDataType.VarChar, 50))
MyCommand.Parameters("@CompanyURL").Value = request.Form("CompanyURL")

MyCommand.Parameters.Add(New SQLParameter("@EmailAddress", SQLDataType.VarChar, 50))
MyCommand.Parameters("@Emailaddress").Value = request.Form("EmailAddress")

MyCommand.Parameters.Add(New SQLParameter("@DateSubmitted", SQLDataType.DateTime, 8))
MyCommand.Parameters("@DateSubmitted").Value = d //stores the date variable!

MyCommand.ActiveConnection.Open()


Dim workParam as SQLParameter

‘’This line brings back the value using the output method
workParam = myCommand.Parameters.Add(new SQLParameter("@CompanyID", SQLDataType.Int, 4))
workParam.Direction = ParameterDirection.Output

Try
myCommand.Execute()
‘’This line populates the strID variable with the Identity value
strID = (myCommand.Parameters("@CompanyID").Value.ToString())

Catch myException2 as Exception
Response.Write(myException2.ToString())
finally

End try

MyCommand.ActiveConnection.Close()
dim strURL as string = "component1a.aspx?CategoryID=" & strID
page.navigate(strURL)
End If

End Sub

End Class






Stored Procedure that does the business logic

CREATE PROCEDURE sp_ComponentCompanyInfo

@CompanyName varchar(100),
@CompanyURL varchar(100),
@EmailAddress varchar(100),
@DateSubmitted datetime,
@CompanyID int OUTPUT

AS

INSERT tblComponentCompanyInfo(CompanyName, CompanyURL, EmailAddress,
DateSubmitted)
VALUES (@CompanyName, @CompanyURL, @EmailAddress, @DateSubmitted)

Select @CompanyID = @@Identity
RETURN



DDL that Creates the table
CREATE TABLE [dbo].[tblComponentCompanyInfo] (
[CompanyID] [int] IDENTITY (1, 1) NOT NULL ,
[CompanyName] [varchar] (100) NULL ,
[CompanyURL] [varchar] (100) NULL ,
[EmailAddress] [varchar] (100) NULL ,
[DateSubmitted] [datetime] NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[tblComponentCompanyInfo] WITH NOCHECK ADD
CONSTRAINT [PK_tblComponentCompanyInfo] PRIMARY KEY NONCLUSTERED
(
[CompanyID]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网