C# 在web项目中发送对象的实例

C# 在web项目中发送对象的实例,c#,asp.net,object,send,instanceof,C#,Asp.net,Object,Send,Instanceof,我正在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("nextp

我正在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/>"));
}
for(int i=0;i”);
}

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

您可以将项目设置为查询字符串参数,设置为
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.
    }       
}

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

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

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

这正是会话的目的。在否定最明显的答案之前,请准确解释为什么会话不合适!我有超链接,数量是可变的。我正在更新我上面的问题..你在回答之前检查过这个吗?我在myItem=Request.QueryString[“myItem”]上得到编译器错误;请首先检查语法的正确性。@NurlanKenzhebekov您得到的错误是因为
item
item
类型,而QueryString仅用于传递字符串。我现在已经修复了编译错误(尽管我还没有测试它-我还没有接近编译器)。您可以传递查询字符串的一些查找(例如,我在上面的示例中显示的数据库ID),或者将您的项目编码为字符串,以便在绝对不能使用会话的情况下传递查询字符串。如果你想传递对象,我真的建议你使用
Session
不过…我在上面写的不建议使用sessionsorry我没有注意到你的评论…如果不使用Session,那么我能看到的唯一方法就是使用“HttpContext.Current.Items”要传递值..你需要使用服务器。在服务器中传输数据,并且数据只能用于一个请求…你可以在谷歌上找到更多信息。。。