2011-04-24 53 views
0

我有一個帶有條目,圖書標題和圖像的gridView。將gridview中的數據添加到會話變量中

如何在buttonField單擊事件中將選定的索引書標題和imageUrl分別添加到會話變量,以便我可以在不同的頁面上使用此信息。

我認爲像

string title = (string)GridView1.Rows[GridView1.SelectedIndex].DataItem["Title"]; 
     Session["Title"] = title; 
     Response.Redirect("RateBook.aspx"); 

上面的代碼是不正確的。在按鈕點擊事件時,如何真正選擇所選行中的單個項目並將其添加到變量中?

Reagards

回答

1

你必須這樣做在你的GridView你行命令事件。像...

假設您的按鈕的CommandName是StoreValue但你設置任何

if(e.CommandName == "StoreValue") 
{ 
    GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer); 
    string title = row.Cells[ColumnIndex].Text; 

    Session["Title"] = title; 
    Response.Redirect("RateBook.aspx"); 
} 
0

試試這個代碼:

string title = GridView1.Rows[GridView1.SelectedIndex].Cells[ColumnIndex].text; 
Session["Title"] = title; 
Response.Redirect("RateBook.aspx"); 

用的數代替 「ColumnIndex」 參數「標題「欄。

0

查看完整的示例。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:GridView ID="grdTest" runat="server" AutoGenerateColumns="false"> 
      <Columns> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:CheckBox ID="chkSelect" runat="server" /> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:BoundField DataField="Title" HeaderText="Title" /> 
       <asp:BoundField DataField="Author" HeaderText="Author" /> 
      </Columns> 
     </asp:GridView> 
     <asp:Button ID="btnSelect" Text="Select" runat="server" 
      onclick="btnSelect_Click" /> 
    </div> 
    </form> 
</body> 
</html> 




using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class Default3 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      grdTest.DataSource = Book.Books; 
      grdTest.DataBind(); 
     } 


    } 
    protected void btnSelect_Click(object sender, EventArgs e) 
    { 
     bool result = false; 
     foreach (GridViewRow row in grdTest.Rows) 
     { 
      result = ((CheckBox)row.FindControl("chkSelect")).Checked; 
      if (result) 
      { 
       Session["Title"] = row.Cells[1].Text; 
       Session["Author"] = row.Cells[2].Text; 
      } 
     } 
    } 
} 

public class Book 
{ 
    public string Title { get; set; } 
    public string Author { get; set; } 
    public static List<Book> Books 
    { 
     get 
     { 
      return new List<Book>() 
         { 
          new Book{Title = "Title1",Author = "Author1"}, 
          new Book{Title = "Title2",Author = "Author2"}, 
          new Book{Title = "Title3",Author = "Author3"}, 

         }; 
     } 

    } 

} 
0

試試這個它的工作對我來說

protected void OnSelectedIndexChanged(object sender, EventArgs e) 
      {   
       foreach (GridViewRow row in GridVIew1.Rows) 
       { 
        if (row.RowIndex == GridVIew1.SelectedIndex) 
        { 
         row.BackColor = ColorTranslator.FromHtml("#A1DCF2"); 
         row.ToolTip = string.Empty; 
        } 
        else 
        { 
         row.BackColor = ColorTranslator.FromHtml("#FFFFFF"); 
         row.ToolTip = "Click to select this row."; 
        } 
       } 
       //suppose your index is in cell[0]// 
       Session["Index"] = GridVIew1.SelectedRow.Cells[0].Text; 
} 

//你可以使用這個會話//

通過索引獲取詳細信息