1

我有两个 GridView,我在其中从数据库中填充 PageStart 上的数据。当我刷新页面(在回发时)时,我看不到数据表内容。所以我想在每次页面加载时再次对 GridView 进行数据绑定。为了绑定数据,我需要将数据临时存储在某个地方。哪一种是临时存储数据的最佳方法?

在我的第一个网格中大约有 10 行,在第二个 GridView 中我有大约 200 行。而且我没有使用分页

4

5 回答 5

1

再次使用 Cache 对象与 Session 将取决于您是希望每个会话临时存储数据还是要存储相同数据的所有会话。如果您希望仅为应用程序的特定会话维护相同的数据,则可以使用会话。缓存可用于应用程序中的所有用户会话。

于 2013-02-27T08:53:11.163 回答
1

这是一个完整的使用示例,ViewState但您可以将其更改为其他缓存方法。

在此处输入图像描述

默认.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView runat="server" ID="gvProd" AutoGenerateColumns="false" OnRowDataBound="gvProd_RowDataBound" OnRowCommand="gvProd_RowCommand">
            <Columns>
                <asp:TemplateField HeaderText="Product">
                    <ItemTemplate>
                        <asp:Literal runat="server" ID="litNm"></asp:Literal>
                        <asp:DropDownList runat="server" ID="ddlQty"></asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Add To Cart">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="lbnAdd" Text="Add To Cart" CommandName="AddToCart"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <hr />
        <asp:GridView runat="server" ID="gvCart" AutoGenerateColumns="false" OnRowDataBound="gvCart_RowDataBound" OnRowCommand="gvCart_RowCommand">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Literal runat="server" ID="litNm"></asp:Literal>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="txtQty"></asp:TextBox>
                        <asp:Button runat="server" ID="btnUpdate" Text="Update Qty" CommandName="UpdateCart" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>

默认.aspx.cs

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

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        [Serializable]
        public class Product
        {
            public int PID { get; set; }
            public string Name { get; set; }

            public Product(int i) { this.PID = i; this.Name = "product " + i.ToString(); }
        }

        [Serializable]
        public class CartItem
        {
            public Product Prod { get; set; }
            public int Qty { get; set; }

            public CartItem(Product p, int q) { this.Prod = p; this.Qty = q; }
        }

        public List<CartItem> myCart = new List<CartItem>();
        public List<CartItem> MyCart
        {
            get
            {
                if (ViewState["cart"] == null)
                {
                    ViewState["cart"] = new List<CartItem>();
                }

                return ViewState["cart"] as List<CartItem>;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindProdGrid();
        }

        protected void BindProdGrid()
        {
            gvProd.DataSource = GetProducts();
            gvProd.DataBind();
        }

        protected List<Product> GetProducts()
        {
            var ret = new List<Product>();

            ret.Add(new Product(1));
            ret.Add(new Product(2));

            return ret;
        }

        protected void gvProd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "AddToCart")
            {
                var row = (e.CommandSource as LinkButton).NamingContainer as GridViewRow;
                var ddl = row.FindControl("ddlQty") as DropDownList;

                var qty = Convert.ToInt32(ddl.SelectedValue);
                var pid = Convert.ToInt32(e.CommandArgument);

                AddToCart(pid, qty, increase: true);
                BindCartGrid(this.MyCart);
            }
        }

        protected void AddToCart(int pid, int qty, bool increase = false)
        {
            var cartItem = this.MyCart.Find(o => o.Prod.PID == pid);
            if (cartItem == null)
                this.MyCart.Add(new CartItem(new Product(pid), qty));
            else
                if (increase)
                    cartItem.Qty += qty;
                else
                    cartItem.Qty = qty;
        }

        protected void gvProd_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var item = e.Row.DataItem as Product;

                var litNm = e.Row.FindControl("litNm") as Literal;
                litNm.Text = item.Name;

                var ddlQty = e.Row.FindControl("ddlQty") as DropDownList;
                ddlQty.Items.Add(new ListItem("1", "1"));
                ddlQty.Items.Add(new ListItem("10", "10"));

                var lbnAdd = e.Row.FindControl("lbnAdd") as LinkButton;
                lbnAdd.CommandArgument = item.PID.ToString();
            }
        }

        protected void BindCartGrid(List<CartItem> items)
        {
            gvCart.DataSource = items;
            gvCart.DataBind();
        }

        protected void gvCart_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var item = e.Row.DataItem as CartItem;

                var litNm = e.Row.FindControl("litNm") as Literal;
                litNm.Text = item.Prod.Name + " (pid:" + item.Prod.PID.ToString() + ")";

                var txtQty = e.Row.FindControl("txtQty") as TextBox;
                txtQty.Text = item.Qty.ToString();
                txtQty.Attributes["data-pid"] = item.Prod.PID.ToString();
            }
        }

        protected void gvCart_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "UpdateCart")
            {
                var row = (e.CommandSource as Button).NamingContainer as GridViewRow;
                var txtQty = row.FindControl("txtQty") as TextBox;

                var qty = Convert.ToInt32(txtQty.Text);
                var pid = Convert.ToInt32(txtQty.Attributes["data-pid"]);

                AddToCart(pid, qty, increase: false);
                BindCartGrid(this.MyCart);
            }
        }
    }
}
于 2013-03-01T07:32:17.663 回答
0

要解决回发之间的数据持久性,您有多种选择。我真的反对 ViewState,除非在非常特殊的情况下你只有很少的数据(这就是微软在 WebForms 中失败的地方——它的默认行为是 DemoWare)。

我建议将您的数据保存在Cache对象中,并在回发时从该对象中读取它。但这实际上取决于您的特定用例。有不同的技术。

于 2013-02-27T05:43:31.000 回答
0

存储数据的最佳位置是会话。Viewstate 会将所有数据带到客户端,这是不受欢迎的网络/带宽开销。

您的 PageStart 应如下所示:

public void PageStart()
{
if(Session["dt"] == null || !(Session["dt"] is datatable)){


datatable dt;
///your dt populating code
Session["dt"] = dt;
}
yourGridView.datasource = (datatable)Session["dt"];
yourGridView.databind();

} 
于 2013-02-27T06:57:04.473 回答
0

这就是您需要做的所有事情。
放置你的方法或函数,用这样的数据填充gridview。

private void FillGrid()
{
    DataTable dt = new DataTable();
    dt = //Fill you datatable
    Gridview1.DataSource = dt;
    Gridview1.DataBind();
}

这是您在页面加载事件中必须做的事情。

protected void Page_Load(object sender, EventArgs e)  
{  
    if (!IsPostBack)  
    {  
        this.FillGrid();  
    }  
} 
于 2013-02-27T07:31:50.730 回答