帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP编程
表示代码与逻辑代码分离
作者:jspfuns 发布时间:2005-03-12 来源:豆腐技术站
<%@ 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

  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·在ASP中使用数据库
·使用ASP脚本技术
·通过启动脚本来感受ASP的力量
·学习使用ASP对象和组件
·解析asp的脚本语言
·初看ASP-针对初学者
·ASP开发10条经验总结
·ASP之对象总结
·ASP与数据库应用(给初学者)
·关于学习ASP和编程的28个观点
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统