2013-04-20 76 views
0

我在頁面1上有一個列表框和按鈕,當我點擊按鈕時,頁面2將在一個新選項卡中打開。在第2頁中,我將照片上傳到文件夾並設置會話[「FileName」]值。我想關閉頁面2時,上傳的圖像的名稱顯示在列表框中。如何在asp.net中將會話的值從第2頁傳遞給第1頁?

注意:session [「FileName」] =上傳圖像的名稱。

有沒有人有想法?請幫幫我。

謝謝。

我上傳類:

public void ProcessRequest(HttpContext context) 
{  
    if (context.Request.Files.Count > 0) 
    { 
     // get the applications path 

     string uploadPath = context.Server.MapPath(context.Request.ApplicationPath + "/Temp"); 
     for (int j = 0; j <= context.Request.Files.Count - 1; j++) 
     { 
      // loop through all the uploaded files 
      // get the current file 
      HttpPostedFile uploadFile = context.Request.Files[j]; 

      // if there was a file uploded 
      if (uploadFile.ContentLength > 0) 
      { 
       context.Session["FileName"] = context.Session["FileName"].ToString() + uploadFile.FileName+","; 
       uploadFile.SaveAs(Path.Combine(uploadPath, uploadFile.FileName)); 
      } 
     } 
    } 
    // Used as a fix for a bug in mac flash player that makes the 
    // onComplete event not fire 
    HttpContext.Current.Response.Write(" "); 
} 
+0

簡單的更新第1頁,第2頁接近後 - 還是不要打開第二頁,並從一個頁面做的一切。 – Aristos 2013-04-20 09:40:06

+1

如果這是可能的,我希望它會通過Javascript完成。我不認爲它是一個asp.net特定的問題。也許這可能會指向你在正確的方向http://stackoverflow.com/questions/5139657/closing-child-pop-up-and-refreshing-parent-page – 2013-04-20 09:42:02

+0

謝謝TwentyGotoTen。這非常有幫助。 – 2013-04-20 10:18:03

回答

0

會議是在ASP.NET服務器對象。這意味着,當您在一個頁面上創建會話時,只要會話對象未被刪除或過期,就可以在任何其他頁面上使用它。

假設你這樣做是對page1.aspx.cs

Session["FileName"] = "file1"; 

那麼你就可以訪問它page2.aspx.cs這樣的:

if(Session["FileName"]!=null) 
    Label1.Text = (string)Session["FileName"] 

所以這樣你可以訪問會話變量只在.aspx頁面或控件派生類上。

如果您要訪問一個類庫項目比Session變量,這樣做:

HttpContext.Current.Session["FileName"] 

也,它看起來像你已經創建了一個自定義HttpModule

被通知您的HTTPModule不能處理在會話狀態被初始化之前發生的任何管道事件。

Read this瞭解更多關於如何以及何時訪問會話變量中的HttpModule

+0

親愛的Manish Mishra。感謝幫助,但我有兩頁。第2頁將打開一個新選項卡,並在第2頁,我將上傳照片,當我關閉第2頁時,我希望將會話值顯示在第1頁的列表框中。請幫助。 – 2013-04-20 10:27:25

+0

@MortezaKarimi在這種情況下,你將不得不刷新你的page1。只有這樣它纔會知道所創建的新會話變量。而不是打開一個新的選項卡,打開一個彈出或modeldialog。這樣會容易得多 – 2013-04-20 10:30:49

相關問題