欢迎光临
我们一直在努力

表示代码与逻辑代码分离

建站超值云服务器,限时71元/月

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

companys 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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 表示代码与逻辑代码分离
分享到: 更多 (0)