日期:2014-05-18  浏览次数:20410 次

asp.net 页面公共属性传值 提问
源页 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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:TextBox ID="txtY" runat="server"></asp:TextBox>
  <asp:Button ID="btnT2" runat="server" OnClick="btnT2_Click" Text="提交(公共属性值)" />
  </div>
  </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
   
  public string Cz
  {
  get
  {
  return txtY.Text;
  }
  }
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void btnT2_Click(object sender, EventArgs e)
  {
  Response.Redirect("Default2.aspx");
  }
}
目标页


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ PreviousPageType VirtualPath="~/Default.aspx" %> 

<!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="labD" runat="server" Width="98px"></asp:Label></div>
  </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  labD.Text = PreviousPage.Cz; // 未将对象引用设置到对象的实例。 
  }
}

求助各位大侠,问什么实现不了啊!// 未将对象引用设置到对象的实例。在目标页 PreviousPage问什么是Null?



------解决方案--------------------
Response.Redirect("Default2.aspx");这种方法是不行的,你应该



<asp:Button ID="btnT2" runat="server" PostBackUrl="Default2.aspx" Text="提交(公共属性值)" />

才能使用PreviousPage