繁体中文
设为首页
加入收藏
当前位置:ASP技术首页 >> ASP技巧 >> 表示代码与逻辑代码分离

表示代码与逻辑代码分离

2004-10-01 08:26:10  作者:  来源:互联网  浏览次数:10  文字大小:【】【】【
简介:<%@ 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...
关键字:代码 逻辑 分离

<%@ 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" %>

Component Page 1

Please fill out this form to create a new user profile for your

Company's Component.


Once this information is gathered you will not need to enter it again and you will be able to update anytime.

Use the button at the bottom of this page to continue when you are finished.


*=Required Fields

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

责任编辑:admin
相关文章