0

我在 asp.net 上用 C# 编写 web 项目。从一页导航到另一页时,我想传递对象的实例。

例如我有一堂课

public partial class A: System.Web.UI.Page{
   private Item item = new Item();//I have a class Item

   protected  void btn1_Click(Object sender,EventArgs e)
   {
      Response.Redirect("nextpage.aspx");//Here I want to send item object to the nextpage
   }
}

我有一堂课

public partial class nextpage: System.Web.UI.Page{
    Item myItem;
    protected void Page_Load(object sender, EventArgs e)
    {
       myItem = //item sent from page A
    }       
}

那么,有没有办法将对象的实例从一个页面发送到另一个页面,比如通过 get 查询发送变量?

请不要推荐使用 Session,由于我的算法,这不合适,因为我有很多超链接:

for (int i = 0; i < store1.items.Count(); i++) {
    HyperLink h = new HyperLink();
    h.Text = store1.items[i].Name;
    h.NavigateUrl = "item.aspx";//here I must send items[i] when clicking at this hyperlink
    this.Form.Controls.Add(h);
    this.Form.Controls.Add(new LiteralControl("<br/>"));
}

因此,当用户单击超链接时,他/她必须被重定向到 item.aspx 并将相应的项目发送到该页面。

4

3 回答 3

0

您是否尝试过使用 ASP.Net 缓存?

于 2012-09-14T10:16:42.393 回答
0

您可以使用会话变量在项目中的不同网页之间发送对象。

public partial class A: System.Web.UI.Page{    
    private Item item = new Item();//I have a class Item  
    Session["myItem"]=myItem;   
    protected  void btn1_Click(Object sender,EventArgs e)
        {       Response.Redirect("nextpage.aspx");
        //Here I want to send item object to the nextpage
        }
 }

public partial class nextpage: System.Web.UI.Page{
     Item myItem;
     protected void Page_Load(object sender, EventArgs e)
     {
        myItem =(Cast to It's Type) Session["myItem"];
     }
} 
于 2012-09-14T09:11:05.267 回答
-1

您可以将您的项目设置为查询字符串参数nextpage.aspx

Response.Redirect("nextpage.aspx?MyItem=somevalue")

public partial class nextpage: System.Web.UI.Page{
    Item myItem;
    protected void Page_Load(object sender, EventArgs e)
    {
       string anIdForTheItem = Request.QueryString["MyItem"];

       myItem = myDatabase.Lookup(anIdForTheItem);

       // You can also use Request.Params["MyItem"], but be aware that Params
       // includes both GET parameters (on the query string) and POST paramaters.
    }       
}
于 2012-09-14T09:08:35.757 回答