GridView的PageIndexChanging事件未触发

时间:2019-03-22 08:51:27

标签: c# asp.net events gridview page-index-changed

我正在构建一个ASP.NET Web应用程序。我有一个.ASPX页面,在其中创建了一个静态GridView,单击按钮后将其填充(使用SQL Server存储过程)。我需要实现分页,但是它不起作用:当我单击第2、3页或任何其他内容时,GridView似乎消失了。

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UDC.aspx.cs" Inherits="DynamicStoreWebApplication.UDC" %>
<link href="Controls.css" rel="stylesheet" type="text/css" />

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>DynamicStore Web - Unità di carico</title>
</head>
<body>
    <form id="form1" runat="server">
        <div runat="server">
        <asp:PlaceHolder ID="PlaceHolderHeader" runat="server"></asp:PlaceHolder>
        <div style="float: left; width: 10%; background: #fff2e6;">
            <asp:PlaceHolder ID="PlaceHolderMenu" runat="server"></asp:PlaceHolder>
        </div>
        <div style="padding: 25px; padding-left: 200px; width: 90%;">
            <asp:Label ID="DateFromLbl" runat="server" Text="Dal giorno: " CssClass="label"></asp:Label>
            <asp:TextBox ID="DateFrom" runat="server" Width="130px" TextMode="Date" CssClass="dropdown" Font-Names="Tahoma">2000-01-01</asp:TextBox>
            <asp:Label ID="DateToLbl" runat="server" Text="Al giorno: " CssClass="label"></asp:Label>
            <asp:TextBox ID="DateTo" runat="server" Width="130px" TextMode="Date" CssClass="dropdown" Font-Names="Tahoma"></asp:TextBox>
            <asp:Label ID="Ricerca" runat="server" Text="Ricerca: " CssClass="label"></asp:Label>
            <asp:TextBox ID="Search" runat="server" Width="95px" TextMode="SingleLine"></asp:TextBox>
            <asp:Label ID="Risultati" runat="server" Text="Ultimi risultati: " CssClass="label"></asp:Label>
            <asp:CheckBox ID="TopResults" runat="server" EnableViewState="true" ViewStateMode="Enabled"/>
            <asp:Button ID="btnSubmit" runat="server" Text="Visualizza UDC" CssClass="control-button" OnClick="btnSubmit_Click"/>
            <br />
            <br />
            <div style = "overflow-x:auto; width:100%">
                <asp:GridView ID="GridView1" runat="server" CssClass="mydatagrid" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header" 
                    RowStyle-CssClass="rows" EnableSortingAndPagingCallbacks="True" AutoGenerateEditButton="False" ShowHeaderWhenEmpty="True" 
                    EnableViewState="true" AllowPaging="True" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging">
                </asp:GridView>
            </div>
        </div>
        <asp:PlaceHolder ID="PlaceHolderFooter" runat="server"></asp:PlaceHolder>
        </div>
    </form>
</body>
</html>

ASPX.CS (后面的代码)

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

namespace DynamicStoreWebApplication
{
    public partial class UDC : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UserControl uc1 = (UserControl)Page.LoadControl("~/Header.ascx");
            PlaceHolderHeader.Controls.Add(uc1);
            UserControl uc2 = (UserControl)Page.LoadControl("~/Menu.ascx");
            PlaceHolderMenu.Controls.Add(uc2);
            UserControl uc3 = (UserControl)Page.LoadControl("~/Footer.ascx");
            PlaceHolderFooter.Controls.Add(uc3);
            DateTo.Text = DateTime.Today.ToString("yyyy-MM-dd");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void BindGridView()
        {
            string conn = "";
            conn = ConfigurationManager.ConnectionStrings["Connection"].ToString();

            SqlConnection objsqlconn = new SqlConnection(conn);

            try
            {
                objsqlconn.Open();
                SqlCommand objcmd = new SqlCommand("DY_FindUdcList", objsqlconn);
                objcmd.CommandType = CommandType.StoredProcedure;


                SqlParameter RCp = objcmd.Parameters.Add("@RC", SqlDbType.Int);
                RCp.Direction = ParameterDirection.ReturnValue;

                SqlParameter DateFromp = objcmd.Parameters.Add("@DateFrom", SqlDbType.DateTime);
                DateFromp.Value = DateFrom.Text;

                SqlParameter DateTop = objcmd.Parameters.Add("@DateTo", SqlDbType.DateTime);
                DateTop.Value = DateTo.Text;

                SqlParameter UdcCelp = objcmd.Parameters.Add("@UdcCel", SqlDbType.Int);
                UdcCelp.Value = -1;

                SqlParameter TopResultsp = objcmd.Parameters.Add("@TopResults", SqlDbType.Bit);

                if (TopResults.Checked)
                {
                    TopResultsp.Value = 1;
                }
                else
                {
                    TopResultsp.Value = 0;
                }

                SqlParameter Searchp = objcmd.Parameters.Add("@Search", SqlDbType.VarChar);
                Searchp.Value = Search.Text;

                SqlParameter Operp = objcmd.Parameters.Add("@Oper", SqlDbType.VarChar);
                Operp.Value = "";

                SqlParameter Termp = objcmd.Parameters.Add("@Term", SqlDbType.VarChar);
                Termp.Value = "";

                SqlParameter Errorep = objcmd.Parameters.Add("@Errore", SqlDbType.VarChar);
                Errorep.Value = "";
                Errorep.Direction = ParameterDirection.Output;

                SqlDataAdapter adapter = new SqlDataAdapter(objcmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);

                GridView1.DataSource = dt;
                GridView1.DataBind();

                objcmd.Dispose();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
            finally
            {
                objsqlconn.Close();
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridView();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码应该可以工作,但是请从gridview声明中删除EnableSortingAndPagingCallbacks="True"并尝试。我认为您也不允许排序。