ASP.NET2.0_执行页面发送的强类型方法与弱类型方法

强类型方法.

     1,先在源页面代码块中,声明一个公共属性;

     2,再在宿页面呈现块中,添加一个<% PreviousPageType %>指令,在其中指定VirtualPath属性

     3,最后在宿页面代码块中,用PreviousPage直接调用源页面代码块的公共属性即可.

例子如下:

源页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Typed.aspx.cs" Inherits="Typed" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>强类型方法</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" AssociatedControlID="TextBox1" Text="姓名:"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" PostBackUrl="~/TestTyped.aspx"
            Text="完成" /></div>
    </form>
</body>
</html>

 

 

public partial class Typed : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string UserName
    {
        get
        {
           return this.TextBox1.Text.ToString();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

 

宿页面

    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            Response.Write(string.Format("欢迎{0}使用执行跨页面发送数据的强类型方法!", PreviousPage.UserName));
        }
    }

 

弱类型方法:

     1,在宿页面代码块中,用PreviousPage的FindControl方法得到相应的控件,再转换;

     2.最后调用此转换后的控件属性即可.

例子如下:

源页面

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Untyped.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>弱类型方法</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="姓名:" AssociatedControlID="TextBox1"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="完成"  PostBackUrl="~/TestUntyped.aspx" OnClick="Button1_Click"/></div>
    </form>
</body>
</html>

 

 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            TextBox txtName=(TextBox)PreviousPage.FindControl("TextBox1");
            Response.Write(string.Format("欢迎{0}使用执行跨页面发送数据的弱类型方法!",txtName.Text.ToString()));
        }
    }

 

 

 

宿页面

    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            Response.Write(string.Format("欢迎{0}使用执行跨页面发送数据的强类型方法!", PreviousPage.UserName));
        }
    }

posted @ 2008-07-23 14:09  Arishuang  阅读(432)  评论(0编辑  收藏  举报