ASP.NET 用于在数据网格网格中显示最后一页的 C# 按钮在单击时消失

本文关键字:网格 按钮 单击 消失 一页 显示 用于 NET 数据 数据网 ASP | 更新日期: 2023-09-27 18:34:52

如果放在 If (!this.isPostBack( 中,最后一页按钮代码可以工作,所以我做了很多研究来尝试了解我问题的原因,但无法弄清楚我的具体问题,在 stackoverflow 上也查看了所有关于此的线程,但没有运气。我错过了什么?

此外,我无法让 Excel 导出工作,我尝试了 3 种不同的方法,这些方法都导致了 OutOfMemoryException,最新的方法仍在该方法中,但注释掉以供进一步研究。

https://i.stack.imgur.com/rzp9l.jpg

ASPX 代码

<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="TestWebFormView.aspx.cs" Inherits="NameSpace.TestWebFormView"
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<%--Form--%>
<form id="form1" runat="server" visible="true">
<asp:Hidden
        <div>
        <%--GridView--%>
           <asp:GridView ID="GridView" AllowPaging="true" PageSize="5" OnPageIndexChanging="GridView_PageIndexChanging" runat="server" AutoGenerateColumns="true" DataKeyNames="Column1, Column2, Column3, Column4, Column5, Column6, Column7, Column8, Column9, Column10, Column11, Column12, Column13, Column14"></asp:GridView>     
        <%--Last Page Button(Not done, Datagrid vanishes on click, Last Page & Export button remain in browser view)--%>
            <asp:Button ID="btnLastPage" runat="server" Text="Jump to last page" OnClick="btnLastPage_Click" />
        <%--Export Button (Not done, OutOfMemoryException, need to somehow divide the workload - Stored Proc in the SQL back end behind the components maybe)--%`>`
            <asp:Button ID="btnExport" runat="server" Text="Exp to Excel" OnClick="btnExport_Click"/>
         </div>
</form>
</body>
</html>

代码隐藏

using System;
using DBComponentsLibrary;
using DBComponentsLibrary.NameDataSetTableAdapters;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
using System.Drawing;
namespace Name
{
    public partial class TestWebFormView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                loadGridData();
        }
        private void loadGridData()
        {
            NameTableAdapter TA = new NameTableAdapter();
            NameDataSet rds = new NameDataSet();
            NameDataSet.NameDataTable Rdt = new NameDataSet.NameDataTable();
            TA.Fill(Rdt);
            GridView.DataSource = Rdt;
            GridView.DataBind();
            GridView.Rows[0].Cells[0].Visible = true;
}
        protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView.PageIndex = e.NewPageIndex;
            Page.DataBind();
        }
        protected void btnLastPage_Click(object sender, EventArgs e)
        {
            int myNewIndex = GridView.PageCount - 1;
            if (myNewIndex != null && myNewIndex != -1 && myNewIndex < GridView.PageCount)
            {
                GridView.PageIndex = myNewIndex;
            }
        }
        protected void btnExport_Click(object sender, EventArgs e)
        {
        }
        //    //Response.Buffer = true;
        //    //Response.Charset = "";
        //    //Response.BinaryWrite(Bytes);
        //    Response.ClearHeaders();
        //    Response.Clear();
        //    Response.AddHeader("content-disposition", "attachment;filename=*.xls");
        //    Response.ContentType = "application/vnd.ms-excel";
        //    Response.End();
        //    using (StringWriter sw = new StringWriter())
        //    {
        //        HtmlTextWriter hw = new HtmlTextWriter(sw);
        //        //To Export all pages
        //        //GridView.AllowPaging = true;

        //        this.DataBind();
        //        //this.BindGrid();
        //        GridView.HeaderRow.BackColor = Color.White;
        //        foreach (TableCell cell in GridView.HeaderRow.Cells)
        //        {
        //            cell.BackColor = GridView.HeaderStyle.BackColor;
        //        }
        //        foreach (GridViewRow row in GridView.Rows)
        //        {
        //            row.BackColor = Color.White;
        //            foreach (TableCell cell in row.Cells)
        //            {
        //                if (row.RowIndex % 2 == 0)
        //                {
        //                    cell.BackColor = GridView.AlternatingRowStyle.BackColor;
        //                }
        //                else
        //                {
        //                    cell.BackColor = GridView.RowStyle.BackColor;
        //                }
        //                cell.CssClass = "textmode";
        //            }
        //        }
        //        GridView.RenderControl(hw);
        //        //style to format numbers to string
        //        string style = @"<style> .textmode { } </style>";
        //        Response.Write(style);
        //        Response.Output.Write(sw.ToString());
        //        Response.Flush();
        //        Response.End();
        //    }
        //}
    }
    } 

编辑 2016-02-17 : 编辑和清理代码。仍然有一些我正在尝试修复的错误。但发展正在向前发展。如果我卡在具体的事情上,我会发一篇新帖子。 再次感谢!

ASP.NET 用于在数据网格网格中显示最后一页的 C# 按钮在单击时消失

问题是您将页码设置为可以用 In32 类型表示的最高可能数字。

protected void btnLastPage_Click(object sender, EventArgs e)
    {
        GridView.PageIndex = Int32.MaxValue;
        DataBind();
    }

那应该是

protected void btnLastPage_Click(object sender, EventArgs e)
    {
        GridView.PageIndex = GridView.PageCount - 1;
        DataBind();
    }