以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Dot NET,C#,ASP,VB 』  (http://bbs.xml.org.cn/list.asp?boardid=43)
----  在页间传递服务器控件值(zzSDK文档)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=43&rootid=&id=11820)


--  作者:admin
--  发布时间:11/9/2004 2:26:00 AM

--  在页间传递服务器控件值(zzSDK文档)


发信人: milletAtaro (小米加芋头), 信区: DotNET
标  题: 在页间传递服务器控件值(zzSDK文档)
发信站: BBS 水木清华站 (Sat Jul 19 13:56:30 2003), 站内

  .NET 框架开发者指南    

在页间传递服务器控件值请参见
@Page 指令 | HttpServerUtility.Transfer | TextBox | IHttpHandler | Handler
语言
C#

Visual Basic

全部显示


当创建 Web 窗体应用程序时,经常需要将信息从一个 Web 窗体页传递到另一个 Web 窗体页。这允许在一个 Web 窗体页上输入信息,然后将该信息提交到另一个页进行处理。

使用内联代码在 Web 窗体页间传递值的设计模式与使用代码隐藏文件的设计模式稍微有所不同。选择哪种设计模式取决于您是喜欢使用内联代码还是代码隐藏文件。将在下面的节中对两种设计模式进行讨论。

使用内联代码的设计模式
当使用代码内联将值传递到另一个 Web 窗体页时,您首先需要为包含所要发送信息的 Web 窗体页指定类名。通过在@Page 指令中包括 ClassName 属性和类的名称为该 Web 窗体页指定类名。然后,在该类中为要共享的每个值创建一个具有 get 访问器的属性。get  
访问器应返回您要传递的值(例如文本框的值)。若要发送这些信息,请使用 Server 对象的 Transfer 方法将应用程序的控制传输到其他 Web 窗体页。

在接收 Web 窗体页上,通过在页的顶部添加一个 @Reference 指令并将 Page 属性设置为发送页来引用发送页中声明的类。然后,接收 Web 窗体页可以通过首先检索处理程序的实例来访问信息,该处理程序首先从 Context 对象的 Handler 属性接收到 HTTP  
请求。然后,处理程序对象将转换为封装所传递信息的类的实例。该转换一旦执行,就可以通过转换后对象的属性访问所传递的值。

创建将值发送到另一个 Web 窗体页的 Web 窗体页  

通过在 Web 窗体页的顶部添加 @Page 指令并将 ClassName 属性设置为有效的类名来为源 Web 窗体页指定类名。  
[Visual Basic]
<%@ Page Language="VB" ClassName="MyClassName" %>
[C#]
<%@ Page Language="C#" ClassName="MyClassName" %>
在该类中为要传递到另一个 Web 窗体页的每个值都定义一个具有 get 访问器的属性。get 访问器应只返回您要传递的值(例如 Web 窗体页上文本框的值)。必须在服务器端脚本中定义这些属性。  
[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>
当要将信息传递到另一个 Web 窗体页时(例如当单击了按钮时),使用 HttpServerUtility.Transfer 方法结束在当前页的执行并将应用程序的控制传输到另一个 Web 窗体页。HttpServerUtility.Transfer 方法采用单个参数,该参数允许您指定要将控制传输到的 Web 窗体页的 URL。  
[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");
}
下面是一个完整的示例,显示如何使用内联代码创建 Web 窗体页以将两个 TextBox 控件的值传递到另一个 Web 窗体页。该示例的名称必须是 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>
创建从另一个 Web 窗体页接收值的 Web 窗体页  

在接收信息的 Web 窗体页上,在页的顶部添加 @Reference 指令并将 Page 属性设置为源 Web 窗体页(包含您要传递信息的 Web 窗体页)。  
<%@ Reference Page="firstpage.aspx" %>
在服务器端脚本中声明一个变量,以存储在发送信息的 Web 窗体页中定义的类的实例。  
[Visual Basic]
<script runat="server">
   Dim fp As FirstPageClass
</script>
[C#]
<script runat="server">
   FirstPageClass fp;
</script>
创建一个自定义 Page_Load 事件处理程序,当 Web 窗体页不回发到其本身时,该处理程序将当前 HTTP 请求的 IHttpHandler 实现的对象分配给上一步中声明的变量。使用 IsPostBack 属性可确定是否将该页回发到其本身。IHttpHandler 实现的对象包含首先接收 HTTP  
请求的处理程序的实例。因为 IHttpHandler 实现的对象与上一步声明的变量属于不同的对象类型,所以必须首先将它转换为封装从第一个 Web 窗体页发送的信息的类,然后才可以将它指派给该变量。使用 Context 对象的 Handler 属性来检索处理程序对象。  
[Visual Basic]
<script runat="server">
   Sub Page_Load()
      If Not IsPostBack Then
         fp =  CType(Context.Handler, FirstPageClass)
      End If
   End Sub
</script>
[C#]
<script runat="server">
   void Page_Load()
   {
      if (!IsPostBack)
      {
         fp = (FirstPageClass)Context.Handler;
      }
   }
</script>
第二步中声明的变量现在包含封装上一个 Web 窗体页中信息的类的实例。使用该变量访问类(该类包含从上一个 Web 窗体页发送的信息)的属性。可以通过编程方式访问这些值以执行计算,或者只是使用脚本分隔符 <%= 和 %> 来显示它们。  
Hello <%=fp.FirstName%>
下面显示一个完整的 Web 窗体页,该页从另一个 Web 窗体页接收两个值。然后,这些值显示在 Web 窗体页上。您必须将该示例叫做 secondpage.aspx。

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

<html>

<head>
  
   <script runat="server">

      Dim fp As FirstPageClass

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

   </script>

</head>  

<body>

   <form runat="server">

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

   </form>

</body>

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


<html>

<head>
  
   <script runat="server">

      FirstPageClass fp;

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

</head>  


<body>

   <form runat="server">

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

   </form>

</body>
</html>
使用代码隐藏文件的设计模式
当使用代码隐藏文件时,代码隐藏文件包含与 Web 窗体页关联的代码的类声明。在发送信息的 Web 窗体页的代码隐藏文件中,首先在类中为要共享的每个值创建一个具有 get 访问器的属性。get 访问器应返回您要传递的值(例如文本框的值)。若要发送这些信息,请使用 Server  
对象的 Transfer 方法将应用程序的控制传输到其他 Web 窗体页。

在接收 Web 窗体页上,通过在页的顶部添加 @Reference 指令并将 Page 属性设置为发送页来引用在发送页中声明的类。然后您可以检索处理程序的实例,该处理程序首先从 Context 对象的 Handler 属性接收 HTTP 请求。

注意 若要使在发送 Web 窗体页中声明的类在接收 Web 窗体页的代码隐藏文件中可用,您必须使用命令行编译器将每个 Web 窗体页的代码隐藏文件手动编译为单个 .dll 文件。该 .dll 文件必须放置在 Web 窗体应用程序的 \Bin 目录中。
然后,处理程序对象将转换为封装所传递信息的类的实例。该转换一旦执行,就可以通过转换后对象的属性访问所传递的值。

从不同的 Web 窗体页发送服务器控件值  

为发送 Web 窗体页创建代码隐藏文件,此文件包含与该页关联的代码的类声明。  
[Visual Basic]
Imports System
' Add other references here.

Public Class FirstPageClass
   Inherits System.Web.UI.Page
    
   ' Add class code here.
    
End Class
[C#]
Imports System
// Add other references here.

public class FirstPageClass : System.Web.UI.Page
{    
   // Add class code here.
}
若要在代码隐藏文件声明的类中访问 Web 窗体页上的服务器控件,请在表示要访问的服务器控件的类中声明受保护的变量。  
[Visual Basic]
Protected FirstNameTextBox As System.Web.UI.WebControls.TextBox
[C#]
protected System.Web.UI.WebControls.TextBox FirstNameTextBox;
在第一步声明的类中,为要传递到另一个 Web 窗体页的每个值都定义一个具有 get 访问器的属性。get 访问器应只返回您要传递的值(例如 Web 窗体页上文本框的值)。  
[Visual Basic]
Public ReadOnly Property FirstName() As String
   Get
      ' FirstNameTextBox is the name of a TextBox control.
      Return FirstNameTextBox.Text
   End Get
End Property
[C#]
public string FirstName
{
   get
   {
      // FirstNameTextBox is the name of a TextBox control.
      return FirstNameTextBox.Text;
   }
}
当要将信息传递到另一个 Web 窗体页时(例如当单击按钮时),使用 HttpServerUtility.Transfer 方法结束在当前页上的执行并将应用程序的控制传输到另一个 Web 窗体页。HttpServerUtility.Transfer 方法采用单个参数,该参数允许您指定要将控制传输到的 Web 窗体页的 URL。  
[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");
}
为发送信息的 Web 窗体页创建界面。确保将 Inherits 属性添加到 @Page 指令,并将其设置为在代码隐藏文件中声明的类。  
[Visual Basic]
<%@ Page Language="VB" Inherits="FirstPageClass" %>

<html>
<head>

</head>

<body>

   <!-- Add User Interface here -->

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

<html>
<head>

</head>

<body>

   <!-- Add User Interface here -->

</body>
下面显示代码隐藏文件的一个完整的示例,该代码隐藏文件与发送信息的 Web 窗体页关联。根据您是使用 Visual Basic 还是 C#,确保该示例分别名为 firstpage.aspx.vb 或 firstpage.aspx.cs。

[Visual Basic]
Imports System

Public Class FirstPageClass :
   Inherits System.Web.UI.Page

   Protected first As System.Web.UI.WebControls.TextBox
   Protected last As System.Web.UI.WebControls.TextBox
   Protected Button1 As System.Web.UI.WebControls.Button

   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

End Class
[C#]
using System;

public class FirstPageClass : System.Web.UI.Page
{
   protected System.Web.UI.WebControls.TextBox first;
   protected System.Web.UI.WebControls.TextBox last;
   protected System.Web.UI.WebControls.Button Button1;

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

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

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

}
下面是一个完整的示例,显示如何用代码隐藏文件创建 Web 窗体页来将两个 TextBox 控件的值传递到另一个 Web 窗体页。确保该示例名为 firstpage.aspx。

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

<html>
<head>

</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
           id="Button1"  
           OnClick="ButtonClicked"  
           Text="Go to second page"
           runat=server />
   </form>

</body>

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

<html>
<head>

</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
           id="Button1"  
           OnClick="ButtonClicked"  
           Text="Go to second page"
           runat=server />
   </form>

</body>

</html>
从不同的 Web 窗体页接收服务器控件值  

为接收 Web 窗体页创建一个代码隐藏文件,该文件包含与该页关联的代码的类声明。  
[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.
}
若要访问代码隐藏文件中 Web 窗体页上的服务器控件,请在表示要访问的服务器控件的类中声明受保护的变量。  
[Visual Basic]
Protected FirstNameTextBox As System.Web.UI.WebControls.TextBox
[C#]
protected System.Web.UI.WebControls.TextBox FirstNameTextBox;
在类中声明变量以存储发送信息的 Web 窗体页中定义的类的实例。如果要使该变量在接收 Web 窗体页中可用,则将其设为公共的。  
注意 在将每个 Web 窗体页的代码隐藏文件编译为单个 .dll 文件之前,不能访问发送信息的 Web 窗体页中定义的类。该 .dll 文件必须放置在 Web 应用程序的 \Bin 目录中。该过程在稍后的步骤中有所介绍。
[Visual Basic]
Public fp As FirstPageClass
[C#]
public FirstPageClass fp;
创建一个自定义 Page_Load 事件处理程序,(当 Web 窗体页不回发到其本身时)该处理程序将当前 HTTP 请求的 IHttpHandler 实现的对象分配给上一步中声明的变量。使用 IsPostBack 属性可确定是否将该页回发到其本身。IHttpHandler 实现的对象包含首先接收 HTTP  
请求的处理程序的实例。因为 IHttpHandler 实现的对象与上一步声明的变量属于不同的对象类型,所以必须首先将它转换为封装从第一个 Web 窗体页发送的信息的类,然后才可以将它指派给该变量。使用 Context 对象的 Handler 属性来检索处理程序对象。  
[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;
   }
}
第三步中声明的变量现在包含封装上一个 Web 窗体页中信息的类的实例。使用该变量访问从上一个 Web 窗体页发送的信息。可以通过编程方式访问这些值以执行计算,或者只是使用脚本分隔符 <%= 和 %> 来显示它们。  
Hello <%=fp.FirstName%>
为发送信息的 Web 窗体页创建界面。在 @Page 指令中,确保添加设置为代码隐藏文件中声明的类的 Inherits 属性。  
[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>
在接收信息的 Web 窗体页的顶部添加 @Reference 指令,并将 Page 属性设置为源 Web 窗体页(包含要传递信息的 Web 窗体页)。  
<%@ Reference Page="firstpage.aspx" %>
发送和接收 Web 窗体页在这时都已完成。但是,在应用程序可以正常运行之前,必须将每个页的代码隐藏文件编译为单个 .dll 文件。该 .dll 文件必须放置在 Web 应用程序的 \Bin 目录中。这允许在接收 Web 窗体页中可以访问发送 Web  
窗体页中声明的类。若要编译代码隐藏文件,请在命令行输入以下命令:  
[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
下面显示代码隐藏文件的一个完整的示例,该代码隐藏文件与接收信息的 Web 窗体页关联。根据您是使用 Visual Basic 还是 C#,确保该示例分别名为 secondpage.aspx.vb 或 secondpage.aspx.cs。

[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;
      }  
   }

}
下面是一个完整的示例,显示如何用代码隐藏文件创建 Web 窗体页,以接收从另一个 Web 窗体页传递的值。确保该示例名为 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>
请参见
@Page 指令 | HttpServerUtility.Transfer | TextBox | IHttpHandler | Handler



--------------------------------------------------------------------------------

将文档反馈发送给 Microsoft

? 2002 Microsoft Corporation。保留所有权利。
  

--

※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.31.222]
上一篇
返回上一页
回到目录
回到页首
下一篇



W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
7,681.641ms