如何在绑定后临时存储DataTable

时间:2013-02-27 05:41:02

标签: c# asp.net data-binding gridview datatable

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

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

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分: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();

} 

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

答案 4 :(得分:0)

以下是使用ViewState的完整工作示例,但您可以针对其他caching方法进行更改。

enter image description here

Default.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>

Default.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);
            }
        }
    }
}