ASP.NET中有几种页面传值方法

来源:互联网 发布:淘宝好评怎么写 编辑:程序博客网 时间:2024/06/06 01:30

目前在ASP.NET中页面传值共有这么几种方式:
1、表单提交,
<form action= "target.aspx" method = "post" name = "form1">
<input name = "param1" value = "1111"/>
<input name = "param2" value = "2222"/> 
</form>
....
form1.submit();
....
此种方在ASP。NET中无效,因为ASP。NET的表单总是提交到自身页面,如果要提交到别一页面,需要特殊处理。
2、<A href="target.aspx?param1=1111&m2=2222">链接地址传送</A>
接收页面: string str = Request["param1"]
3、Session共享
发送页面:Session("param1") = "1111"; 
按收页面 string str = Session("param1").ToString(); 
4、Application共享
发送页面: Application("param1") = "1111"; 
按收页面: string str = Application("param1").ToString(); 
此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用计数器等需要全局变量的地方。
5、Cookie
6、Response.Redirect()方式
Response.Redirect("target.aspx?param1=1111&m2=2222")
接收页面: string str = Request["param1"]
7、Server.Transfer()方式。
Server.Transfer("target.aspx?param1=1111&m2=2222")
接收页面: string str = Request["param1"]
例子:如果在两个页面间需要大量的参数要传传递,如数据查询等页面时,用1 - 6的方法传值及其不便,而第 7 种方法确有一独特的优势!但使用该方法时需要一定的设置,现简单介绍一下该方法的使用方式:
以查询数据页面为例:
在查询页面中设置如下公有属性(QueryPage.aspx):
public class QueryPage : System.Web.UI.Page
{
    protected System.Web.UI.WebControls.TextBox txtStaDate;
    protected System.Web.UI.WebControls.TextBox txtEndDate;
...
    /// <summary>
    /// 开始时间
    /// </summary>
    public string StaDate
    {
       get{ return this.txtStaDate.Text;}
       set{this.txtStaDate.Text = value;}
    }
/// <summary>
/// 结束时间
/// </summary>
public string EndDate
{
    get{ return this.txtEndDate.Text;}
    set{this.txtEndDate.Text = value;}
}
....
private void btnEnter_Click(object sender, System.EventArgs e)
{
    Server.Transfer("ResultPage.aspx");
}
}
在显示查询结果页面(ResultPage.aspx):
public class ResultPage : System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
    {
   //转换一下即可获得前一页面中输入的数据
       QueryPage queryPage = ( QueryPage )Context.Handler;
      Response.Write( "StaDate:" );
       Response.Write( queryPage.StaDate );
       Response.Write( "<br/>EndDate:" );
       Response.Write( queryPage.EndDate );
    }
}


例:如果有许多查询页面共用一个结果页面的设置方法:
在这种方式中关键在于“ QueryPage queryPage = ( QueryPage )Context.Handler; ”的转换,只有转换不依赖于特定的页面时即可实现。
如果让所有的查询页面都继承一个接口,在该接口中定义一个方法,该方法的唯一作用就是让结果页面获得构建结果时所需的参数,就可实现多页面共享一个结果页面操作!
1、先定义一个类,用该类放置所有查询参数:
/// <summary>
/// 结果页面中要用到的值
/// </summary>
public class QueryParams
{
    private string staDate;
    private string endDate;


    /// <summary>
    /// 开始时间
    /// </summary>
    public string StaDate
    {
       get{ return this.staDate;}
       set{this.staDate = value;}
    }
/// <summary>
/// 结束时间
/// </summary>
public string EndDate
{
    get{ return this.endDate;}
    set{this.endDate = value;}
}
}


2、接口定义:
/// <summary>
/// 定义查询接口。
/// </summary>
public interface IQueryParams
{
    /// <summary>
    /// 参数
    /// </summary>
    QueryParams Parameters{get;}
}

3、查询页面继承IQueryParams接口(QueryPage.aspx):
/// <summary>
///查询页面,继承接口
/// </summary>
public class QueryPage : System.Web.UI.Page, IQueryParams
{
    protected System.Web.UI.WebControls.TextBox txtStaDate;
    protected System.Web.UI.WebControls.TextBox txtEndDate;


private QueryParams queryParams;
...
/// <summary>
/// 结果页面用到的参数
/// </summary>
public QueryParams Parameters
{
    get
    {
       return queryParams;
    }
}
....
private void btnEnter_Click(object sender, System.EventArgs e)
{
    //赋值
    queryParams = new QueryParams();
    queryParams.StaDate = this.txtStaDate.Text;
    queryParams.EndDate = this.txtEndDate.Text


    Server.Transfer("ResultPage.aspx");
}
}
4、别外的页面也如此设置
5、接收页面(ResultPage.aspx):
public class ResultPage : System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
{
QueryParams queryParams = new QueryParams();
IQueryParams queryInterface;
//实现该接口的页面
if( Context.Handler is IQueryParams)
{
    queryInterface = ( IQueryParams )Context.Handler;
    queryParams = queryInterface.Parameters;
}

Response.Write( "StaDate:" );
Response.Write( queryParams.StaDate );
Response.Write( "<br/>EndDate:" );
Response.Write( queryParams.EndDate );
}
}

具体介绍:
一、使用Querystring
Querystring是一种非常简单的传值方式,其缺点就是会把要传送的值显示在浏览器的地址栏中,并且在此方法中不能够传递对象。如果你想传递一个安全性不是那么太重要或者是一个简单的数值时,用此方法最好不过了。下面通过一个小例子来完成传值工作,步骤如下:
1、创建一个web form
2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2 
3、为button按钮创建click事件
代码如下:
private void Button1_Click
(object sender, System.EventArgs e)
{
string url;
url="webform2.aspx?name=" + 
   TextBox1.Text + "&email=" + 
   TextBox2.Text;
Response.Redirect(url);
}
4、新建一个目标页面命名为webform2
5、在webform2中放置两个Label1,Label2
在webform2的Page_Load中添加如下代码:
private void Page_Load
(object sender, System.EventArgs e)
{
Label1.Text=Request.QueryString["name"];
Label2.Text=Request.QueryString["email"];
}
运行,即可看到传递后的结果了。


二、使用Session变量


使用Session变量传值是一种最常见的方式了,此中方式不仅可以把值传递到下一个页面,还可以交叉传递到多个页面,直至把Session变量的值removed后,变量才会消失。举个例子看看:
1、创建一个webform
2、在新建的webform中放置一个button1,在放置两个TextBox1,TextBox2 
3、为button按钮创建click事件
代码如下:
private void Button1_Click
(object sender, System.EventArgs e)
{
    Session["name"]=TextBox1.Text;
    Session["email"]=TextBox2.Text;
    Response.Redirect("webform2.aspx");
}
4、新建一个目标页面命名为webform2
5、在webform2中放置两个Label1,Label2
在webform2的Page_Load中添加如下代码:
private void Page_Load
(object sender, System.EventArgs e)
{
Label1.Text=Session["name"].ToString();
Label2.Text=Session["email"].ToString();
Session.Remove("name");
Session.Remove("email");
}
运行,即可看到传递后的结果了。


三、使用Server.Transfer
这个方法相比上面介绍的方法稍微复杂一点,但在页面间值传递中却是特别有用的,使用该方法你可以在另一个页面以对象属性的方式来存取显露的值,当然了,使用这种方法,你需要额外写一些代码以创建一些属性以便可以在另一个页面访问它,但是,这个方式带来的好处也是显而易见的。总体来说,使用这种方法是简洁的同时又是面向对象的。使用这种方法的整个过程如下:
1,在页面里添加必要的控件
2,创建返回值的Get属性过程
3,创建可以返回表单的按钮和链接按钮
4,在按钮单击事件处理程序中调用Server.Transfer方法转移到指定的页面
5,在第二个页面中,我们就可以使用Context.Handler属性来获得前一个页面实例对象的引用,通过它,就可以使用存取前一个页面的控件的值了
以下代码综合实现上述步骤过程的代码:
  源页面WebForm1.aspx.cs中的部分代码:
    把以下的代码添加到页面中
public string Name
{
     get
     {
         return TextBox1.Text;
     }
}


public string EMail
{
     get
     {
         return TextBox2.Text;
     }
}
  然后调用Server.Transfer方法
private void Button1_Click(object sender, System.EventArgs e)
{
     Server.Transfer("WebForm2.aspx");
}
   目标页面代码:


在WebForm2.aspx中务必在第一句话添加


<%@ Reference Page="~/WebForm1.aspx" %>或


<%@ PreviousPageType VirtualPath="~/WebForm1.aspx" %>


然后在WebForm2.aspx.cs中添加如下。




private void Page_Load(object sender, System.EventArgs e)
{
     //create instance of source web form
     WebForm1 wf1;
     //get reference to current handler instance
     wf1=(WebForm1)Context.Handler;
     Label1.Text=wf1.Name;
     Label2.Text=wf1.EMail;
}


如果在调试的过程中遇到错误.就到C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files中把新建的网站名的文件夹删掉.(要先关掉Visual Studio开发环境再删)
5、新建一个目标页面命名为webform2
6、在webform2中放置两个Label1,Label2
在webform2的Page_Load中添加如下代码:
private void Page_Load
(object sender, System.EventArgs e)
{
//创建原始窗体的实例
WebForm1 wf1;
//获得实例化的句柄
wf1=(WebForm1)Context.Handler;
Label1.Text=wf1.Name;
Label2.Text=wf1.EMail;


}
运行,即可看到传递后的结果了。
四.使用@PreviousPageType指令
这个指令是.net 2.0中的一个新指令,用于处理ASP.NET 2.0提供的跨页面传送新功能.用于批定跨页面的传送过程起始于哪个页面.包含两个属性:
TypeName:设置回送时的派生类名
VirtualPath:设置回送时所传送页面的地址.
如下示例:
源页面WebForm1.aspx中有一个TextBox,ID为txtName.在WebForm1.aspx.cs中设置一个属性:
public TextBox Name
{
  get{return this.txtName;}//返回一个控件对象
}
在目标页面的设计文件中(WebForm2.aspx)的最上方加上:
 <%@ PreviousPageType VirtualPath="~/Page1.aspx"%>,
然后就能引用WebForm1.aspx中定义的属性了.
在WebForm2.aspx.cs中可以有如下引用形式(假设WebForm2.aspx中有一个ID为lblName的Label):
lblName.Text="Hello"+PreviousPage.Name.Text+"<br />";


五.利用某些控件的PostBackUrl属性
示例:仍然是源页面WebForm1.aspx和目标页面WebForm2.aspx.
WebForm1.aspx中的部分代码:
<asp:Button ID="btnPostBack" Runat="server" Text="PBButton"></asp:Button>
<asp:TextBox ID="txtName" Runat="server" ></asp:TextBox>
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
WebForm2.aspx.cs中的部分代码:
protected void Page_Load(object Sender,System.EventArgs e)

{
TextBox txtName;
  Calendar calendar1;
txtName=(TextBox)PreviousPage.FindControl("txtName");
 calendar1=(Calendar)PreviousPage.FindControl("Calendar1");
 Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();
}
使用这种方法存在一个问题:如果在没有单击那个按钮之前,也就是未处理WebForm1.aspx之前,有人请求了WebForm2.aspx,该怎么办?这就需要在WebForm2.aspx中的代码处理之前加一个判断.使用IsCrossPagePostBack属性,这与IsPostBack属性很相似,它允许检查请求是否来自WebForm1.aspx.如下:
protected void Page_Load(object Sender,System.EventArgs e)
{
  if(PreviousPage.IsCrossPagePostBack)
    {
        TextBox txtName;
    Calendar calendar1;
       txtName=(TextBox)PreviousPage.FindControl("txtName");
       calendar1=(Calendar)PreviousPage.FindControl("Calendar1");
       Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();
    }
    else
    {
        Response.Redirect("WebForm1.aspx");
   }
}

六、 使用Cookie对象变量
  这个也是大家常使用的方法,与Session一样,是对每一个用户而言的,但是有个本质的区别,即Cookie是存放在客户端的,而session是存放在服务器端的。而且Cookie的使用要配合ASP.NET内置对象Request来使用。
a.aspx的C#代码
private void Button1_Click(object sender, System.EventArgs e)
{
    HttpCookie cookie_name = new HttpCookie("name");
    cookie_name.Value = Label1.Text;
    Reponse.AppendCookie(cookie_name);
    Server.Transfer("b.aspx");
}
b.aspx中C#代码
private void Page_Load(object sender, EventArgs e)
{
    string name;
    name = Request.Cookie["name"].Value.ToString();
}

七、使用Application 对象变量
  Application对象的作用范围是整个全局,也就是说对所有用户都有效。其常用的方法用Lock和UnLock。
a.aspx的C#代码
private void Button1_Click(object sender, System.EventArgs e)
{
    Application["name"] = Label1.Text;
    Server.Transfer("b.aspx");
}


b.aspx中C#代码
private void Page_Load(object sender, EventArgs e)
{
    string name;
    Application.Lock();
    name = Application["name"].ToString();
    Application.UnLock();
}



原创粉丝点击