簡體   English   中英

如何在不使用asp.net的會話的情況下將選定的數據(數據列表)從一頁轉移到另一頁?

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

我用了兩頁。 第一個是父頁面,第二個是彈出頁面。 我想從彈出頁面到父頁面檢索選定的數據。 目前,我正在使用會話,但我想不使用會話。

彈出頁面代碼如下:

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

既然您提到了內存使用情況,我建議使用跨頁回發 (cookie和查詢字符串只能處理這么多數據)。 使用此方法,您基本上將數據保持為視圖狀態(實現為隱藏字段),並使用http post方法來解決問題-一起發送viewstate。

例:

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

我在評論中描述的方法將像這樣工作:

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