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

强类型方法.

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

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

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

例子如下:

源页面

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



    强类型方法


   
   


       
       
                    Text="完成" />

   

 

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



    弱类型方法


   
   


       
       
       

   


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

请使用浏览器的分享功能分享到微信等