如何在不使用asp.net中的会话的情况下将所选数据(datalist)从一个页面传输到另一个页面?

时间:2022-05-30 09:52:56

I have used two pages. First one is parent page and second one is popup page. I want to retrieve selected data from popup page to parent page. currently I am using session for it but I want to do it without session.

我用了两页。第一个是父页面,第二个是弹出页面。我想从弹出页面到父页面检索所选数据。目前我正在使用会话,但我想在没有会话的情况下这样做。

Popup page Code is as below:

弹出页面代码如下:

List<vw_ServiceandProduct> lstsapm = new List<vw_ServiceandProduct>();

lstsapm = (from a in db.vw_ServiceandProduct where a.IsActive == true && a.BranchID == Common.BranchID select a).ToList();

Session["lstsapmsession"] = lstsapm;

Parent page Code is as below :

List<vw_ServiceandProduct> lstsapm = Session["lstsapmsession"] as List<vw_ServiceandProduct>;

GridView1.DataSource = lstsapm;
GridView1.DataBind();

2 个解决方案

#1


Since you mentioned memory usage, I'd recommend using cross page postback (cookie & query string can handle only so many data). With this method you essentially persist your data into view state (which is implemented as hidden field) and use http post method to get around - sending the viewstate along.

既然你提到了内存使用,我建议使用跨页回发(cookie和查询字符串只能处理这么多数据)。使用此方法,您基本上可以将数据保持为视图状态(实现为隐藏字段)并使用http post方法绕过 - 发送视图状态。

Example:

public interface ITransferSomething {
  // anything here as long as it is decorated with [Serializable]
  IList<vw_ServiceandProduct> SerializableValue { get; }
  // exposing standard property of System.Web.UI.Page
  bool IsCrossPagePostBack { get; }
}  

Default.aspx

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  <asp:LinkButton PostBackUrl="~/Default2.aspx" Text="Transfer!" runat="server" />
</asp:Content>    

Default.aspx.cs

public partial class _Default : System.Web.UI.Page, ITransferSomething {
  protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
      SerializableValue = new List<vw_ServiceandProduct> {
        new vw_ServiceandProduct { Name = "foo" }
      };
    }
  }

  public IList<vw_ServiceandProduct> SerializableValue {
    get { return (IList<vw_ServiceandProduct>)ViewState["SerializableValue"]; }
    set { ViewState["SerializableValue"] = value; }
  }
} 

Default2.aspx.cs

public partial class Default2 : System.Web.UI.Page {
  protected void Page_Load(object sender, EventArgs e) {
    var transfer = Page.PreviousPage as ITransferSomething;
    if (transfer != null && transfer.IsCrossPagePostBack) {
      SerializableValue = transfer.SerializableValue;
    }
  }

  public IList<vw_ServiceandProduct> SerializableValue {
    get { return (IList<vw_ServiceandProduct>)ViewState["SerializableValue"]; }
    set { ViewState["SerializableValue"] = value; }
  }
}

#2


The method I described in the comment would work like this:

我在评论中描述的方法将如下工作:

List<vw_ServiceandProduct> lstsapm = new List<vw_ServiceandProduct>();
lstsapm = (from a in db.vw_ServiceandProduct where a.IsActive == true && a.BranchID == Common.BranchID select a).ToList();
//Session["lstsapmsession"] = lstsapm;
string key = Guid.NewGuid().ToString("N");
string path = Server.MapPath("~/App_Data/TempFiles/" + key);
DataContractSerializer dcs = new DataContractSerializer(typeof(List<vw_ServiceandProduct>));
using (var outStream = File.OpenWrite(path))
{
    using (XmlDictionaryWriter xdw = XmlDictionaryWriter.CreateTextWriter(outStream, Encoding.UTF8))
    {
        dcs.WriteObject(xdw, lstsapm);
    }
}

// pass key to parent using querystring or cookie


// Parent page Code:
string key = ""; // from cookie or querystring
List<vw_ServiceandProduct> lstsapm = null; //Session["lstsapmsession"] as List<vw_ServiceandProduct>;
string path = Server.MapPath("~/App_Data/TempFiles/" + key);
DataContractSerializer dcs = new DataContractSerializer(typeof(List<vw_ServiceandProduct>));
using (var inStream = File.OpenRead(path))
{
    using (XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(inStream, new XmlDictionaryReaderQuotas()))
    {
        lstsapm = dcs.ReadObject(xdr) as List<vw_ServiceandProduct>;
    }
}
if (lstsapm != null)
{
    GridView1.DataSource = lstsapm;
    GridView1.DataBind();
}

#1


Since you mentioned memory usage, I'd recommend using cross page postback (cookie & query string can handle only so many data). With this method you essentially persist your data into view state (which is implemented as hidden field) and use http post method to get around - sending the viewstate along.

既然你提到了内存使用,我建议使用跨页回发(cookie和查询字符串只能处理这么多数据)。使用此方法,您基本上可以将数据保持为视图状态(实现为隐藏字段)并使用http post方法绕过 - 发送视图状态。

Example:

public interface ITransferSomething {
  // anything here as long as it is decorated with [Serializable]
  IList<vw_ServiceandProduct> SerializableValue { get; }
  // exposing standard property of System.Web.UI.Page
  bool IsCrossPagePostBack { get; }
}  

Default.aspx

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  <asp:LinkButton PostBackUrl="~/Default2.aspx" Text="Transfer!" runat="server" />
</asp:Content>    

Default.aspx.cs

public partial class _Default : System.Web.UI.Page, ITransferSomething {
  protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
      SerializableValue = new List<vw_ServiceandProduct> {
        new vw_ServiceandProduct { Name = "foo" }
      };
    }
  }

  public IList<vw_ServiceandProduct> SerializableValue {
    get { return (IList<vw_ServiceandProduct>)ViewState["SerializableValue"]; }
    set { ViewState["SerializableValue"] = value; }
  }
} 

Default2.aspx.cs

public partial class Default2 : System.Web.UI.Page {
  protected void Page_Load(object sender, EventArgs e) {
    var transfer = Page.PreviousPage as ITransferSomething;
    if (transfer != null && transfer.IsCrossPagePostBack) {
      SerializableValue = transfer.SerializableValue;
    }
  }

  public IList<vw_ServiceandProduct> SerializableValue {
    get { return (IList<vw_ServiceandProduct>)ViewState["SerializableValue"]; }
    set { ViewState["SerializableValue"] = value; }
  }
}

#2


The method I described in the comment would work like this:

我在评论中描述的方法将如下工作:

List<vw_ServiceandProduct> lstsapm = new List<vw_ServiceandProduct>();
lstsapm = (from a in db.vw_ServiceandProduct where a.IsActive == true && a.BranchID == Common.BranchID select a).ToList();
//Session["lstsapmsession"] = lstsapm;
string key = Guid.NewGuid().ToString("N");
string path = Server.MapPath("~/App_Data/TempFiles/" + key);
DataContractSerializer dcs = new DataContractSerializer(typeof(List<vw_ServiceandProduct>));
using (var outStream = File.OpenWrite(path))
{
    using (XmlDictionaryWriter xdw = XmlDictionaryWriter.CreateTextWriter(outStream, Encoding.UTF8))
    {
        dcs.WriteObject(xdw, lstsapm);
    }
}

// pass key to parent using querystring or cookie


// Parent page Code:
string key = ""; // from cookie or querystring
List<vw_ServiceandProduct> lstsapm = null; //Session["lstsapmsession"] as List<vw_ServiceandProduct>;
string path = Server.MapPath("~/App_Data/TempFiles/" + key);
DataContractSerializer dcs = new DataContractSerializer(typeof(List<vw_ServiceandProduct>));
using (var inStream = File.OpenRead(path))
{
    using (XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(inStream, new XmlDictionaryReaderQuotas()))
    {
        lstsapm = dcs.ReadObject(xdr) as List<vw_ServiceandProduct>;
    }
}
if (lstsapm != null)
{
    GridView1.DataSource = lstsapm;
    GridView1.DataBind();
}