各位老菜,如何在两个WebForm中传递参数?

lracky 2003-08-20 01:52:59
如题!
...全文
75 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lookingfor 2003-08-20
  • 打赏
  • 举报
回复
这里的人都说这么清楚了你还不给分啊
gd4134 2003-08-20
  • 打赏
  • 举报
回复
用对象Session
例Session["变量名"]=aaa
waterass 2003-08-20
  • 打赏
  • 举报
回复
To receive Server control values from a different Web Forms page

Create a code-behind file, for the receiving Web Forms page, that contains a class declaration for the code associated with the page.
[Visual Basic]
Imports System
' Add other references here.

Public Class SecondPageClass
Inherits System.Web.UI.Page

' Add class code here.

End Class
[C#]
Imports System
// Add other references here.

public class SecondPageClass : System.Web.UI.Page
{
// Add class code here.
}
To access the server controls on the Web Forms page in the code-behind file, declare protected variables in the class to represent the server controls you want to access.
[Visual Basic]
Protected FirstNameTextBox As System.Web.UI.WebControls.TextBox
[C#]
protected System.Web.UI.WebControls.TextBox FirstNameTextBox;
Declare a variable in the class to store an instance of the class defined in the Web Forms page sending the information. If you want the variable available in the receiving Web Forms page, make it public.
Note The class defined in the Web Forms page sending the information cannot be accessed until you compile the code-behind files of each Web Forms page into a single .dll file. The .dll file must be placed in the \Bin directory of the Web application. This process is described in a later step.
[Visual Basic]
Public fp As FirstPageClass
[C#]
public FirstPageClass fp;
Create a custom Page_Load event handler that assigns the IHttpHandler implemented object for the current HTTP request to the variable declared in the previous step (when the Web Forms page does not post back to itself). Use the IsPostBack property to determine whether the page is posting back to itself. The IHttpHandler implemented object contains an instance of the handler that first received the HTTP request. Because the IHttpHandler implemented object is not the same type of object as the variable declared in the previous step, it must be converted to the class encapsulating the information sent from the first Web Forms page before it can be assigned to the variable. Retrieve the handler object by using the Handler property of the HttpContext object.
[Visual Basic]
Sub Page_Load()
If Not IsPostBack Then
fp = CType(Context.Handler, FirstPageClass)
End If
End Sub
[C#]
void Page_Load()
{
if (!IsPostBack)
{
fp = (FirstPageClass)Context.Handler;
}
}
The variable declared in the third step now contains an instance of the class encapsulating the information from the previous Web Forms page. Use the variable to access the information sent from the previous Web Forms page. You can programmatically access these values to perform a calculation, or simply display them using the script delimiters <%= and %>.
Hello <%=fp.FirstName%>
Create the interface for the Web Forms page sending the information. In the @ Page directive, make sure to add an Inherits attribute that is set to the class declared in the code-behind file.
[Visual Basic]
<%@ Page Language="VB" Inherits="SecondPageClass" %>

<html>
<head>

</head>

<body>

<!-- Add User Interface here -->

</body>
[C#]
<%@ Page Language="C#" Inherits="SecondPageClass" %>

<html>
<head>

</head>

<body>

<!-- Add User Interface here -->

</body>
Add an @ Reference directive at the top of the Web Forms page, receiving the information, with the Page attribute set to the source Web Forms page (the Web Forms page that contains the information you want to pass).
<%@ Reference Page="firstpage.aspx" %>
The sending and receiving Web Forms pages are both complete at this point. However, before the application can run properly, the code-behind files of each page must be compiled together into a single .dll file. This .dll file must be placed in the \Bin directory of the Web application. This allows the class declared in the sending Web Forms page to be accessible in the receiving Web Forms page. To compile the code-behind files, enter the following command in the command line:
[Visual Basic]
vbc /out:Bin\appname.dll /r:System.dll /r:System.Web.dll /t:library firstpage.aspx.vb secondpage.aspx.vb
[C#]
csc /out:Bin\appname.dll /r:System.dll /r:System.Web.dll /t:library firstpage.aspx.cs secondpage.aspx.cs
The following shows a complete example of the code-behind file associated with the Web Forms page receiving the information. Depending on whether you use Visual Basic or C#, make sure this sample is called Secondpage.aspx.vb or Secondpage.aspx.cs, respectively.

[Visual Basic]
Imports System

Public Class SecondPageClass
Inherits System.Web.UI.Page

Protected DisplayLabel As System.Web.UI.WebControls.Label
Public fp As FirstPageClass

Sub Page_Load()
If Not IsPostBack Then
fp = CType(Context.Handler, FirstPageClass)
End If
End Sub

End Class
[C#]
using System;

public class SecondPageClass : System.Web.UI.Page
{

protected System.Web.UI.WebControls.Label DisplayLabel;
public FirstPageClass fp;

void Page_Load()
{
if (!IsPostBack)
{
fp = (FirstPageClass) Context.Handler;
}
}

}
The following shows a complete example of how to create a Web Forms page with a code-behind file to receive the values passed from a different Web Forms page. Make sure this sample is called Secondpage.aspx

[Visual Basic]
<%@ Page Language="VB" Inherits="SecondPageClass" %>
<%@ Reference Page="firstpage.aspx" %>

<html>

<head>

</head>

<body>

<form runat="server">

Hello <%=fp.FirstName%> <%=fp.LastName%>

</form>

</body>

</html>
[C#]
<%@ Page Language="C#" Inherits="SecondPageClass" %>
<%@ Reference Page="firstpage.aspx" %>

<html>

<head>

</head>

<body>

<form runat="server">

Hello <%=fp.FirstName%> <%=fp.LastName%>

</form>

</body>

</html>
waterass 2003-08-20
  • 打赏
  • 举报
回复
可以用QuerryString,或者用Session,或者用表单提交.
还有一种,详见下方,是MSDN上讲的,我也不知道有什么优点。
.NET Framework Developer's Guide

Passing Server Control Values Between Pages
When creating a Web Forms application, it is often necessary to pass information from one Web Forms page to another. This allows information to be entered on one Web Forms page and then submitted to another page for processing.

The design pattern for passing values between Web Forms pages using code inline is slightly different from that for code-behind files. Which design pattern you choose depends on your preference for using inline code or code-behind files. Both design patterns are discussed in the following sections.

Design Pattern Using Inline Code
When passing values to another Web Forms page using code inline, you first need to specify a class name for the Web Forms page that contains the information you want to send. Specify a class name for the Web Forms page by including the ClassName attribute together with the name of the class in the @ Page directive. Next, create a property with a get accessor, in the class, for each value you want to share. The get accessor should return the value you want to pass, such as the value of a text box. To send the information, transfer control of the application to a different Web Forms page by using the Transfer method of the Server object.

On the receiving Web Forms page, reference the class declared in the sending page by adding an @ Reference directive at the top of the page with the Page attribute set to the sending page. The receiving Web Forms page can then access the information by first retrieving an instance of the handler that first received the HTTP request from the Handler property of the HttpContext object. The handler object is then converted to an instance of the class encapsulating the passed information. Once the conversion is performed, the passed values can be accessed through the properties of the converted object.

To create a Web Forms page that sends values to another Web Forms page

Specify a class name for the source Web Forms page by adding an @ Page directive at the top of the Web Forms page, with the ClassName attribute set to a valid class name.
[Visual Basic]
<%@ Page Language="VB" ClassName="MyClassName" %>
[C#]
<%@ Page Language="C#" ClassName="MyClassName" %>
Define a property with a get accessor, in the class, for each value you want to pass to another Web Forms page. The get accessor should simply return the value you want to pass, such as the value of a text box on the Web Forms page. The properties must be defined in server-side script.
[Visual Basic]
<script runat="server">
Public ReadOnly Property FirstName() As String
Get
' FirstNameTextBox is the name of a TextBox control.
Return FirstNameTextBox.Text
End Get
End Property
</script>
[C#]
<script runat="server">
public string FirstName
{
get
{
// FirstNameTextBox is the name of a TextBox control.
return FirstNameTextBox.Text;
}
}
</script>
When you want to pass the information to another Web Forms page, such as when a button is clicked, use the HttpServerUtility.Transfer method to end execution on the current page and transfer control of the application to another Web Forms page. The HttpServerUtility.Transfer method takes a single parameter that allows you to specify the URL of the Web Forms page you want to transfer control to.
[Visual Basic]
Sub SubmitButtonClick(sender As Object, e As EventArgs)
Server.Transfer("secondpage.aspx")
End Sub

[C#]
void SubmitButtonClick(object sender, EventArgs e)
{
Server.Transfer("secondpage.aspx");
}
The following shows a complete example of how to create a Web Forms page, using inline code, to pass the values of two TextBox controls to another Web Forms page. This sample must be called Firstpage.aspx.

[Visual Basic]
<%@ Page Language="VB" ClassName="FirstPageClass" %>

<html>
<head>

<script runat="server">
Public ReadOnly Property FirstName() As String
Get
' first is the name of a TextBox control.
Return first.Text
End Get
End Property

Public ReadOnly Property LastName() As String
Get
' last is the name of a TextBox control.
Return last.Text
End Get
End Property

Sub ButtonClicked(sender As Object, e As EventArgs)
Server.Transfer("secondpage.aspx")
End Sub

</script>

</head>

<body>

<form runat="server">
First Name:
<asp:TextBox id="first"
runat="server"/>
<br>
Last Name:
<asp:TextBox id="last"
runat="server"/>
<br>
<asp:Button
OnClick="ButtonClicked"
Text="Go to second page"
runat=server />
</form>

</body>

</html>
[C#]
<%@ Page Language="C#" ClassName="FirstPageClass" %>

<html>
<head>

<script runat="server">

public string FirstName
{
get
{
return first.Text;
}
}

public string LastName
{
get
{
return last.Text;
}
}

void ButtonClicked(object sender, EventArgs e)
{
Server.Transfer("secondpage.aspx");
}

</script>

</head>

<body>

<form runat="server">
First Name:
<asp:TextBox id="first"
runat="server"/>
<br>
Last Name:
<asp:TextBox id="last"
runat="server"/>
<br>
<asp:Button
OnClick="ButtonClicked"
Text="Go to second page"
runat=server />
</form>

</body>

</html>
vikey 2003-08-20
  • 打赏
  • 举报
回复
form可以传
<form action="webform2.aspx">
……
<input ……>
……
</form>

webform2.aspx
a = request.form("")
孟子E章 2003-08-20
  • 打赏
  • 举报
回复
http://www.erp800.com/net_lover/ShowDetail.asp?id=49ML4AO8-5PB3-4KNY-NJZD-LJOIOXV4M1X4
孟子E章 2003-08-20
  • 打赏
  • 举报
回复
webform2.aspx?test=aaaaa

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧