c# - 我有一个带有分页事件的 gridview。每次我点击一个新页面时,它都会让我从上传文件控件重新加载文件,然后它会显示新页面

标签 c# asp.net

我希望我的分页是无缝的,我不希望用户必须重新加载文件才能获得新页面。我哪里出错了?

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (FileUpload1.PostedFile.FileName == string.Empty)
            {

                return;
            }
            else
            {
                string[] FileExt = FileUpload1.FileName.Split('.');
                string FileEx = FileExt[FileExt.Length - 1];
                if (FileEx.ToLower() == "csv")
                {
                    FileUpload1.SaveAs(Server.MapPath(" " + FileUpload1.FileName));
                }
                else
                {

                    return;
                }
            }
            CSVReader reader = new CSVReader(FileUpload1.PostedFile.InputStream);
            string[] headers = reader.GetCSVLine();
            DataTable dt = new DataTable();

            foreach (string strHeader in headers)
                dt.Columns.Add(strHeader);
            string[] data;
            while ((data = reader.GetCSVLine()) != null)
                dt.Rows.Add(data);
            foreach (DataRow row in dt.Rows)
            {
                string rowIdNumber = row["Sequence"].ToString();
                string dateAndTime = row["Date and Time"].ToString();
                string personName = row["Description #2"].ToString();
                string doorType = row["Card number"].ToString();

                DateTime dateEntered = Convert.ToDateTime(dateAndTime);
                DoorType doorTypeEnum;

                bool personExists = false;
                foreach (object name in listboxOfNames.Items)
                {
                    if (name.ToString() == personName)
                    {
                        personExists = true;
                        break;
                    }
                }
                if (!personExists)
                {
                    listboxOfNames.Items.Add(personName);

                }
                switch (doorType)
                {
                    case "A02 - Rear Entrance":
                        doorTypeEnum = DoorType.RearEntranceDoor;
                        break;
                    case "B12 - Exterior Main Floor Man Trap":
                        doorTypeEnum = DoorType.ExteriorMainFloorDoor;
                        break;
                    case "B12 - Interior Main Floor Man Trap":
                        doorTypeEnum = DoorType.InteriorMainFloorDoor;
                        break;
                    case "C13 - Rear Break Room Door":
                        doorTypeEnum = DoorType.RearBreakRoomDoor;
                        break;
                    case "B02 - Exterior Basement Man Trap":
                        doorTypeEnum = DoorType.ExteriorBasementDoor;
                        break;
                    case "B02 - Interior Basement Man Trap":
                        doorTypeEnum = DoorType.InteriorBasementDoor;
                        break;
                    case "D01 - Managed Services Main door":
                        doorTypeEnum = DoorType.ManagedServicesDoor;
                        break;
                    case "D01 - Managed Services Big Door":
                        doorTypeEnum = DoorType.ManagedServicesBigDoor;
                        break;
                    default:
                        doorTypeEnum = DoorType.None;
                        break;
                }
                peopleEntering.Add(new PersonEntered(personName, dateEntered, doorTypeEnum));
            }


            for (int i = 0; i < peopleEntering.Count; i++)
            {
                DateTime startDate = new DateTime();
                DateTime endDate = new DateTime();
                string personName = peopleEntering[i].PersonName;
                if (peopleEntering[i].DoorEntered == DoorType.RearEntranceDoor)
                {
                    startDate = peopleEntering[i].DateOfEntry;
                    for (int j = i + 1; j < peopleEntering.Count; j++)
                    {
                        if (peopleEntering[j].DoorEntered == DoorType.ExteriorBasementDoor && peopleEntering[j].PersonName == personName)
                        {
                            endDate = peopleEntering[j].DateOfEntry;
                        }
                    }

                }
                workSpans.Add(new WorkSpan(personName, startDate, endDate));

            }

            csvReaderGv.DataSource = dt;
            csvReaderGv.DataBind();
            listboxOfNames.Visible = true;

        }

    }

    protected void csvReaderGv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (Page.IsPostBack)
        {
            csvReaderGv.PageIndex = e.NewPageIndex;
            csvReaderGv.DataBind();
        }

    }

我做错了什么?我的小脑袋不知道为什么每次我对 gridview 进行分页时都必须重新加载文件。请添加示例或指导我正确的论坛或教程。

最佳答案

我们一般使用GridView控件来显示数据,也可以进行分页、排序、编辑、删除记录。在每一个中,我们在页面上都有回发。 为了避免在排序和分页期间回发,可以在 GridView 控件中使用以下代码。

如果您已经创建了一个 GridView 并使用数据源控件绑定(bind)了 GridView,您可以通过将 GridView 的 EnableSortingAndPagingCallbacks 属性设置为 True 来避免在排序和分页期间回传.

注意:当您将“EnableSortingAndPagingCallbacks”属性设置为 true 以避免回发时,您不能在 GridView 中使用模板字段。

示例:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"AllowSorting = "true"

 DataSourceID="SqlDataSource1" EnableSortingAndPagingCallbacks = "true">

 <Columns>

  <asp:BoundField DataField="FIRST_NAME" HeaderText="First Name"

                    SortExpression="FIRST_NAME" />

  <asp:BoundField DataField="LAST_NAME" HeaderText="Last Name"

                    SortExpression="LAST_NAME" />

  <asp:BoundField DataField="ADDRESS" HeaderText="Address"

                    SortExpression="ADDRESS" />

 </Columns>

</asp:GridView>

点这里

How to prevent Postback on GridView paging in UpdatePanel

关于c# - 我有一个带有分页事件的 gridview。每次我点击一个新页面时,它都会让我从上传文件控件重新加载文件,然后它会显示新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18362646/

相关文章:

c# - winforms 面板重叠有问题

c# - 将 MSSQL GetUTCDate() 转换为 PHP/Unix/MySQL Ticks

c# - 纸牌游戏,纸牌建筑

asp.net - Paypal PayFlow Pro Asp.Net 配置

c# - C#-如何在ASP.NET应用程序中处理子线程

asp.net - 如何在不使用 CreateUser 的情况下在 ASP.NET 成员资格表中创建用户

c# - 如何从datalist内的datakey字段获取值

c# - C#中的换行符

C#调用native代码比native调用native快

c# - WebAPI Controller 继承和属性路由