【问题标题】:send instance of object in web-project在 web 项目中发送对象实例
【发布时间】:2012-09-07 10:12:37
【问题描述】:

我正在 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 并将相应的项目发送到该页面。

【问题讨论】:

  • 这正是 Sessions 的用途。在驳回最明显的答案之前,请准确解释为什么会议不合适!
  • 我有超链接,数量是可变的。我正在更新我上面的问题..

标签: c# asp.net object send instanceof


【解决方案1】:

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

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

【讨论】:

  • 我上面写的不推荐使用session
  • 对不起,我没有注意到你的评论......如果不使用会话,那么我能看到的唯一方法是使用“HttpContext.Current.Items”来传递值......你需要使用 Server.Transfer 并且数据仅对一个请求可用...您可以在 google 上找到更多信息...
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    您可以将您的项目设置为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.
        }       
    }
    

    【讨论】:

    • 你在回答之前检查过这个吗?我在 myItem = Request.QueryString["MyItem"]; 上收到编译器错误;请先检查语法的正确性。
    • @NurlanKenzhebekov 您得到的错误是因为itemItem 类型,而QueryString 仅对传递字符串有用。我现在已经修复了编译错误(虽然我还没有测试它——我不在编译器附近)。您可以在查询字符串上传递一些查找(例如,我在上面的示例中显示的数据库 ID),或者如果您绝对,则将您的项目编码为传递查询字符串的字符串不能使用会话。如果您想传递对象,我真的建议您使用Session...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多