开发者

adding data from gridview to a session variable

I have a gridView with entries, a book title and and image.

How can I add the selecteted index book title and imageUrl respectively to session variables upon the buttonField click event so that I can use this information on a different page.

I would think something like

string title = (开发者_开发问答string)GridView1.Rows[GridView1.SelectedIndex].DataItem["Title"];
        Session["Title"] = title;
        Response.Redirect("RateBook.aspx");

The above code is not correct. How do I actually select and individual item in the selected row and add it to the variable upon the button click event?

Reagards


you have to do this in your Row Command Event of your Gridview. like...

Suppose your Button CommandName is StoreValue but you set whatever

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

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


try this code:

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

replace the "ColumnIndex" parameter with the number of the "Title" column.


Check out the complete example.

<%@ 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"},

                       };
        }

    }

}


try this its working for me

 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;
}

// you can get details by index using this session //

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜