将gridview中的数据添加到会话变量中

时间:2011-04-24 10:01:47

标签: c# .net asp.net session gridview

我有一个带有条目的gridView,一个书名和图像。

如何在buttonField点击事件中将选择的索引书名和imageUrl分别添加到会话变量,以便我可以在不同的页面上使用此信息。

我会想像

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

以上代码不正确。如何在所选行中实际选择单个项目并在按钮单击事件时将其添加到变量?

Reagards

4 个答案:

答案 0 :(得分:1)

您必须在Gridview的行命令事件中执行此操作。像...

假设您的Button 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");
}

答案 1 :(得分:0)

试试这段代码:

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

将“ColumnIndex”参数替换为“Title”列的编号。

答案 2 :(得分: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"},

                       };
        }

    }

}

答案 3 :(得分: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;
}

//您可以使用此会话按索引获取详细信息//