ASP.NET Gridview分页问题

时间:2014-02-12 15:27:06

标签: asp.net vb.net gridview data-binding datasource

我遇到gridview控件的数据绑定方法问题我可以使用一些帮助。一些背景...我有一个搜索界面,有各种文本框(名字,姓氏,年份,性别等)用户将输入一些数据,然后按搜索按钮。那时我动态生成数据源的SQL并将其绑定到gridview控件。

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    '  Check Program Year
    If ddYear.SelectedValue = 0 Then
        lblStatus.Text = "Please select a program year!"
        Exit Sub
    Else
        lblStatus.Text = ""
    End If

    ds1.SelectCommand = "SELECT [StudentId], [ProgramYear], [LastName], [Middle], [FirstName], [HighSchoolCode], [Sex], [DateOfBirth] FROM [Student] WHERE [ProgramYear] = " & ddYear.SelectedValue & ""

    Dim useCase As Integer = 0

    If tbFname.Text <> "" Then useCase = 1
    If tbLname.Text <> "" Then useCase = 2
    If tbFname.Text <> "" And tbLname.Text <> "" Then useCase = 3
    If tbFname.Text <> "" And tbM.Text <> "" Then useCase = 4
    If tbID.Text <> "" Then useCase = 5
    If tbHSCode.Text <> "" Then useCase = 6
    If tbLname.Text <> "" And ddGender.SelectedValue <> "0" Then useCase = 7
    If tbHSCode.Text <> "" And ddGender.SelectedValue <> "0" Then useCase = 8


    Select Case useCase

        Case 1 'First Name
            ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%')"
        Case 2 'Last Name
            ds1.SelectCommand += " and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%')"
        Case 3 'First and Last Name
            ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%') and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%')"
        Case 4 'First and Middle
            ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%') and ([Middle] = '" & tbM.Text & "')"
        Case 5 'Student ID
            ds1.SelectCommand += " and ([StudentId] = '" & tbID.Text & "')"
        Case 6 'HS Code
            ds1.SelectCommand += " and ([HighSchoolCode] = '" & tbHSCode.Text & "')"
        Case 7 'Last Name and Sex
            ds1.SelectCommand += " and ([Sex] = '" & ddGender.SelectedValue & "' and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%'))"
        Case 8 'HS Code and Sex
            ds1.SelectCommand += " and ([HighSchoolCode] = '" & tbHSCode.Text & "' and [Sex] = '" & ddGender.SelectedValue & "')"


        Case Else
            ds1.SelectCommand += " order by [LastName] desc"
    End Select


    ds1.DataBind()
    GridView1.DataBind()


End Sub

这很好用但是当我启用分页并应用OnPageIndexChanging方法时,我遇到了麻烦。

Protected Sub gridview1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)

    gridview1.PageIndex = e.NewPageIndex
    GridView1.DataSource = ds1
    gridview1.databind()

End Sub

当我再次按下提交按钮时,我点击网格视图的分页页脚来更改页面,并且gridview不会应用新的绑定。我不确定为什么会发生任何建议表示赞赏。

Gridview:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="StudentId" DataSourceID="ds1" AllowPaging="True" PageSize="15">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%# Container.DataItemIndex + 1 %>.
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Student Id" InsertVisible="False" SortExpression="StudentId">
            <ItemTemplate>
                <a href="searchDetail.aspx?StudentID=<%# eval("StudentId") %>" >
                <asp:Label ID="xyz" runat="server" Text='<%# Bind("StudentId") %>' /></a> 
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
        <asp:BoundField DataField="Middle" HeaderText="Middle" SortExpression="Middle" />
        <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
        <asp:BoundField DataField="Sex" HeaderText="Sex" SortExpression="Sex" />
        <asp:BoundField DataField="HighSchoolCode" HeaderText="HS Code" SortExpression="HighSchoolCode" />
        <asp:BoundField DataField="ProgramYear" HeaderText="Program Year" SortExpression="ProgramYear" />
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>

DS1

   <asp:SqlDataSource ID="ds1" runat="server" ConnectionString="<%$ ConnectionStrings:NMERITEXString %>"></asp:SqlDataSource>

2 个答案:

答案 0 :(得分:0)

您需要在gridview1_PageIndexChanging中执行的操作是设置gridview1.PageIndex并执行与btnSubmit_Click中相同的操作。

为避免重复代码,请将btnSubmit_Click的内容移至单独的子代,我们将其称为LoadGrid

Private Sub LoadGrid()
    '  Check Program Year
    If ddYear.SelectedValue = 0 Then
        lblStatus.Text = "Please select a program year!"
        Exit Sub
    Else
        lblStatus.Text = ""
    End If

    ds1.SelectCommand = "SELECT [StudentId], [ProgramYear], [LastName], [Middle], [FirstName], [HighSchoolCode], [Sex], [DateOfBirth] FROM [Student] WHERE [ProgramYear] = " & ddYear.SelectedValue & ""

    Dim useCase As Integer = 0

    If tbFname.Text <> "" Then useCase = 1
    If tbLname.Text <> "" Then useCase = 2
    If tbFname.Text <> "" And tbLname.Text <> "" Then useCase = 3
    If tbFname.Text <> "" And tbM.Text <> "" Then useCase = 4
    If tbID.Text <> "" Then useCase = 5
    If tbHSCode.Text <> "" Then useCase = 6
    If tbLname.Text <> "" And ddGender.SelectedValue <> "0" Then useCase = 7
    If tbHSCode.Text <> "" And ddGender.SelectedValue <> "0" Then useCase = 8


    Select Case useCase

        Case 1 'First Name
            ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%')"
        Case 2 'Last Name
            ds1.SelectCommand += " and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%')"
        Case 3 'First and Last Name
            ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%') and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%')"
        Case 4 'First and Middle
            ds1.SelectCommand += " and ([FirstName] LIKE '%' + '" & tbFname.Text & "' + '%') and ([Middle] = '" & tbM.Text & "')"
        Case 5 'Student ID
            ds1.SelectCommand += " and ([StudentId] = '" & tbID.Text & "')"
        Case 6 'HS Code
            ds1.SelectCommand += " and ([HighSchoolCode] = '" & tbHSCode.Text & "')"
        Case 7 'Last Name and Sex
            ds1.SelectCommand += " and ([Sex] = '" & ddGender.SelectedValue & "' and ([LastName] LIKE '%' + '" & tbLname.Text & "' + '%'))"
        Case 8 'HS Code and Sex
            ds1.SelectCommand += " and ([HighSchoolCode] = '" & tbHSCode.Text & "' and [Sex] = '" & ddGender.SelectedValue & "')"


        Case Else
            ds1.SelectCommand += " order by [LastName] desc"
    End Select


    ds1.DataBind()

    GridView1.DataSource = ds1;
    GridView1.DataBind()


End Sub

然后在LoadGrid()

内拨打btnSubmit_Click
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    'set to first page
    gridview1.PageIndex = 0

    LoadGrid()
End Sub

并在LoadGrid()中致电gridview1_PageIndexChanging。不要忘记添加Handles gridview1.PageIndexChanging

Protected Sub gridview1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gridview1.PageIndexChanging

    gridview1.PageIndex = e.NewPageIndex
    LoadGrid()

End Sub

答案 1 :(得分:0)

好的,我现在正在工作......

我必须将loadgrid()子添加到页面加载,如下所示。谢谢你的帮助!

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    If Page.IsPostBack Then
        LoadGrid()
    End If

End Sub