简体   繁体   English

如何在不使用asp.net的会话的情况下将选定的数据(数据列表)从一页转移到另一页?

[英]How to transfer selected data(datalist) from one page to another page without using session in asp.net?

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

Since you mentioned memory usage, I'd recommend using cross page postback (cookie & query string can handle only so many data). 既然您提到了内存使用情况,我建议使用跨页回发 (cookie和查询字符串只能处理这么多数据)。 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. 使用此方法,您基本上将数据保持为视图状态(实现为隐藏字段),并使用http post方法来解决问题-一起发送viewstate。

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 Default.aspx

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

Default.aspx.cs 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 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; }
  }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM