【问题标题】:adding data from gridview to a session variable将数据从 gridview 添加到会话变量
【发布时间】:2011-04-24 10:01:46
【问题描述】:

我有一个带有条目、书名和图片的 gridView。

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

我会想像

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

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

问候

【问题讨论】:

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


    【解决方案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");
    }
    

    【讨论】:

      【解决方案2】:

      试试这个代码:

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

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

      【讨论】:

        【解决方案3】:

        查看完整示例。

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

        【讨论】:

          【解决方案4】:

          试试这个对我有用

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-12-07
            • 2019-09-02
            • 2013-12-29
            • 1970-01-01
            • 1970-01-01
            • 2019-06-16
            • 2017-07-11
            相关资源
            最近更新 更多