GridView客户端分页不工作

本文关键字:工作 分页 客户端 GridView | 更新日期: 2023-09-27 18:23:44

使用其他程序中的数据(例如,在本代码中,我使用随机数数组之类的其他程序的查询数据)。查询到DataTable,绑定到GridView+添加所有行删除按钮。

在删除按钮上单击-此按钮处于非活动状态(使用button.Enabled = false;UpdatePanel等按钮,单击我有"旧"阵列,效果都很好)。但当我使用分页时,每次都有一个"新"数组。

如何修复?我需要——在第一页我点击按钮——它们变为非活动的,然后我选择第二页,然后返回第一页,看到带有非活动按钮的"旧"数组。

aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!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">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
    <asp:UpdatePanel ID="Up1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleted="GridView1_RowDeleted"
            OnRowDeleting="GridView1_RowDeleting" OnDataBinding="GridView1_DataBinding" AllowPaging="true" PageSize="10" OnPageIndexChanging="GridView1_PageIndexChanging">
            <Columns>
                <asp:BoundField DataField="Name" ItemStyle-Width="200px">
                    <ItemStyle Width="200px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Number" ItemStyle-Width="200px">
                    <ItemStyle Width="200px"></ItemStyle>
                </asp:BoundField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                            <ContentTemplate>
                                <asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" OnClientClick="return DeleteConfirm();" />
                                <asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("Name") %>' />
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>
        </ContentTemplate>
        </asp:UpdatePanel>
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Reset" />
    </div>
    </form>
    <script type="text/javascript">
        function DeleteConfirm() {
            if (confirm("Are you sure you want to delete this customer from excluded customer list ?")) {
                return true;
            }
            return false;
        }
    </script>
</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;
using System.Data;
namespace WebApplication1
{
        public partial class WebForm1 : System.Web.UI.Page
        {
            private  DataTable _Source;
            public WebForm1()
            {
                ResetData();
            }
            private  void ResetData()
            {
               _Source = new DataTable();
               _Source.Columns.Add("Name", typeof(string));
                _Source.Columns.Add("Number", typeof(string));
                Random rn = new Random();
                for (int t = 0; t < 100; t++)
                {
                    _Source.Rows.Add(rn.Next(1, 10).ToString(), rn.Next(1, 10).ToString());
                }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                    GridView1.DataBind();
            }
            protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
            {
            }
            protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                _Source.Rows.RemoveAt(e.RowIndex);
                //GridView1.DataBind();
                Response.Redirect("~/WebForm1.aspx");
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                ResetData();
                GridView1.DataBind();
            }
            protected void GridView1_DataBinding(object sender, EventArgs e)
            {
                GridView1.DataSource = _Source;
            }
            protected void Button2_Click(object sender, EventArgs e)
            {
                var button = sender as Button;
                button.Enabled = false;
                var hidden = button.Parent.FindControl("HiddenField2") as HiddenField;
                var name = hidden.Value;
                DeletForName(name);
            }
            protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                GridView1.PageIndex = e.NewPageIndex;
               // GridView1.DataSource = _Source;
                //GridView1.DataBind();
            }
            private  void DeletForName(string name)
            {
                foreach (DataRow row in _Source.Rows)
                    if (row["Name"].Equals(name))
                    {
                      _Source.Rows.Remove(row);
                        break;
                    }
            }
        }
    }

GridView客户端分页不工作

您基本上需要在请求之间存储数据源。一种选择是使用会话变量:

public partial class WebForm1 : System.Web.UI.Page
    {
        private DataTable _Source;
        public WebForm1()
        {
        }
        private void ResetData()
        {
            _Source = new DataTable();
            _Source.Columns.Add("Name", typeof(string));
            _Source.Columns.Add("Number", typeof(string));
            Random rn = new Random();
            for (int t = 0; t < 100; t++)
            {
                _Source.Rows.Add(rn.Next(1, 10).ToString(), rn.Next(1, 10).ToString());
            }
            Session["data"] = _Source;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ResetData();
                GridView1.DataBind();
            }
            _Source = Session["data"] as DataTable;
        }
        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            ((DataTable)Session["data"]).Rows.RemoveAt(e.RowIndex);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            ResetData();
            GridView1.DataBind();
        }
        protected void GridView1_DataBinding(object sender, EventArgs e)
        {
            GridView1.DataSource = _Source;
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            var button = sender as Button;
            button.Enabled = false;
            var hidden = button.Parent.FindControl("HiddenField2") as HiddenField;
            var name = hidden.Value;
            DeletForName(name);
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }
        private void DeletForName(string name)
        {
            foreach (DataRow row in _Source.Rows)
                if (row["Name"].Equals(name))
                {
                    _Source.Rows.Remove(row);
                    break;
                }
        }
    }

基本上,当页面加载时,如果它不是post-back,它会创建一个新的DataTable并将其存储在Session变量中。如果是post-back,则从Session变量中提取已存在的数据。此外,您只想在需要创建新的DataTable时调用ResetData函数,所以我将其从构造函数中删除。

此外,请确保实际数据源的Name列是唯一的,因为删除函数将删除具有您要查找的名称的第一行,而不一定是您选择要删除的行。

希望这能有所帮助!

编辑

两种可能性。我认为最有可能的情况是,您希望将已删除的数据保留在表中,但只需将其标记为已删除,并在网格视图中禁用其删除按钮。要做到这一点,我的直觉是在表中添加一个布尔列,指示一行是否已被删除。然后,当一行被数据绑定时,检查它是否已被删除,并相应地禁用按钮:

private void DeletForName(string name)
{
    foreach (DataRow row in _Source.Rows)
        if (row["Name"].Equals(name))
        {
            row["Deleted"] = true;
            break;
        }
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((bool)((System.Data.DataRowView)e.Row.DataItem)["Deleted"])
        {
            ((Button)e.Row.FindControl("Button2")).Enabled = false;
        }
    }
}

如果您想从表中删除该项,但仍然禁用已删除行索引处的按钮,则会稍微复杂一些。如果是这样的话,请告诉我,我可以尽力帮助你,但以上似乎更可能是你想要的。