Nothing happens when I click on page on GridView w

2019-09-04 23:08发布

问题:

I'm new to C# and .NET, but stuck on this for a couple days now, I am using MVC 3 (.NET 4.0) Web Application.

I looked through tutorials to make a GridView based on a SqlDataSource with the code below:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            OnPageIndexChanging = "GridView1_PageIndexChanging"
            AutoGenerateColumns="False" DataKeyNames="LastName,FirstName"
            EnablePersistedSelection="True" SelectedRowStyle-BackColor="Yellow" 
            DataSourceID="SqlDataSource1" AllowSorting="True" >
            <Columns>
                <asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True" SortExpression="LastName" />
                <asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True" SortExpression="FirstName" />
                ...
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
            SelectCommand="SELECT * FROM User WHERE [IsDeleted]=0"></asp:SqlDataSource> 

However, when I click on the different pages, nothing happens(it looked like it reloaded the same page), but when I open in a new tab in Chrome, it just opens a blank page, when I hover over the link "2" to go to page 2, this is what shows:

javascript:__doPostBack('ctl00$MainContent$GridView1','Page$2') 

When I click on the title to do a sort, nothing happens just as the pages, and the link says this:

javascript:__doPostBack('ctl00$MainContent$GridView1','Sort$WindowsUsername')

I've also tried putting on top of the aspx file this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="..." %>
<script runat="server">

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
</script>

And same problem. This function isn't even being called when I click on other pages

Am I missing something somewhere? Any help appreciated!

回答1:

You must specify the event for OnPageIndexChanging in your .aspx, which you don't have. It should be:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        OnPageIndexChanging = "GridView1_PageIndexChanging" 
        AutoGenerateColumns="False" DataKeyNames="LastName,FirstName"
        EnablePersistedSelection="True" SelectedRowStyle-BackColor="Yellow" 
        DataSourceID="SqlDataSource1" AllowSorting="True">