2012-10-12 35 views
-1

我想在登錄頁面設置會話,並希望將一個頁面girdview數據傳遞到另一個頁面。傳遞gridview特定的行數據到asp.net中的另一個頁面?

slno title  author publication takenby 
    1 book1 author1 pub1   sendrequest (button) 

上圖顯示gridview。當我點擊請求書按鈕(第一個)。它會重定向到sendrequest.aspx頁面。並有把該行的數據(即)

slno=1 
title=book1 
Author=author1 
publication=publ 

我想下面的代碼 user.aspx.cs

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
      { 
       string user = (string)(Session["User"]); 
       if (e.CommandName.Equals("SendRequestCmd")) 
       { 
        if (user == null) 
        { 
         Session["slno"] = null; 
         Session["bookname"] = null; 
         var clickedRow = ((Button)e.CommandSource).NamingContainer as GridViewRow; 
         // now access the cells like this 
         SlNo = clickedRow.Cells[0].Text; 
         Session["slno"] = SlNo.ToString(); 
         BookName = clickedRow.Cells[1].Text; 
         Session["bookname"] = BookName.ToString(); 
        } 
       } 
      } 

sendquest.aspx.cs

protected void Page_Load(object sender, EventArgs e) 
     { 
      string slno = "", bookname = ""; 
      slno = (string)(Session["slno"]); 
      bookname = (string)(Session["bookname"]); 
      if (slno == null || bookname == null) 
      { 
       Response.Write("serial no: " + slno + "\nbookname: " + bookname); 
      } 
     } 
     protected void Logout() 
     { 
      Session.Clear(); 
     } 

,但我得到的錯誤在

slno=(string)(Session["slno"]); 
bookname=(string)(Session["bookname"]); 

爲什麼?有人糾正它? 別的說更好的方法?

+0

您收到的會話對象的錯誤是什麼? – martinwnet

+0

而不是在分隔會話中傳遞每個值只需創建一個類型(Class)並將每個值作爲屬性並設置對象的每個屬性,並在會話中傳遞該對象並將其轉換爲接收結束,便於檢查會話對於單個會話爲空4-5個會話值 – rahularyansharma

回答

0

沒有發佈您的錯誤,很難看出問題所在。你可以檢查你的應用程序中啓用了會話。

另一種方法(可能更好)是將id/slno(通過QueryString)傳遞給其他頁面,然後在該階段執行查找以獲取數據。

0

我必定爲預訂

slno title author publication性能和

public class Booking 
{ 
    public Booking() 
    { 
     // 
     // TODO: Add constructor logic here 
     // 
    } 
    public int slno { get; set; } 
    public string Title { get; set; } 
    public string author { get; set; } 
    public string publication { get; set; } 

} 

組預訂類型的對象的所有屬性創建類型(類),並通過該對象在會話

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     string user = (string)(Session["User"]); 
     if (e.CommandName.Equals("SendRequestCmd")) 
     { 
      if (user == null) 
      { 
       Booking b = new Booking(); 
       b.slno = clickedRow.Cells[0].Text; 
       b.Title = BookName.ToString(); 

       Session["Booing"] = b; 

      } 
     } 
    } 

並且在接收端只是將該會話值重新插入到該對象中 並僅通過使用dot來訪問每個屬性對象名稱。

Booking b=(Booking)Session["Booking"] 
     int slno=b.slno; 
    string Title=b.Title; 

而不發佈錯誤無法給出建議或糾正,我的回答是,你是如何從一個頁面傳遞使用會話多個值到另一個。

謝謝

相關問題