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

セッション変数を使用して、プロジェクト内の異なる Web ページ間でオブジェクトを送信できます。

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 に答える