1

我有一个 GridView,它在单击“生成”按钮时显示特定的数据列表或报告。

GridView 记录

当我移动到 GridView 的下一页或单击下一页时,GridView 绑定变为空并且不显示 GridView 列表。

页面索引已更改

这是我的代码.aspx

<asp:GridView ID="GridView1"
    runat="server"
    CellPadding="4"
    HeaderStyle-ForeColor="White"
    ForeColor="#333333"
    GridLines="None"
    CssClass="mGrid"
    PagerStyle-CssClass="pgr"
    AlternatingRowStyle-CssClass="alt"
    RowStyle-CssClass="row"
    AllowSorting="True"
    AllowPaging="True"
    PageSize="25" Visible="false" OnRowDataBound="GridView1_RowDataBound"
    EnableViewState="false">
    <EmptyDataTemplate>
        No record found.
    </EmptyDataTemplate>
</asp:GridView>

这是我的代码Page_Load

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            userid = IIf(SessionHandler.UserID = "", User.Identity.Name.ToUpper, SessionHandler.UserID)

            V3Substance.Attributes.Add("onfocus", "javascript:if (this.value=='Substance Cas Number') { this.value = ''; }")
            V3Substance.Attributes.Add("onblur", "javascript:if (this.value=='') { this.value = 'Substance Cas Number'; }")

            If Not Page.IsPostBack Then
                V1Division.DataSource = GetDivision()
                V1Division.DataBind()

                V1BR.Items.Clear()
                V1BR.DataSource = GetBusinessRule(V1Division.Text)
                V1BR.DataBind()

                MultiView1.ActiveViewIndex = 0
                Panel1.DefaultButton = "V1Generate"
            End If
        End Sub

这是我的代码GridView1_PageIndexChanging

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    Dim dt As DataTable = TryCast(GridView1.DataSource, DataTable)
    GridView1.DataSource = SortTable(dt)

    GridView1.PageIndex = e.NewPageIndex
    GridView1.DataBind()
End Sub

这是我的代码BindGridView

 Private Sub BindGridview(ByVal query As String, ByVal countquery As String)
            ViewState.Add("Query", query)
            ViewState.Add("CountQuery", countquery)

            Dim count As Integer = 0

            If Description.Text = "1" Then
                ValidateFamily(query)
            Else
                DataSource = dbHelper.GetRecordDT(query)
            End If

            count = DataSource.Rows.Count

            'GridView1.DataSource = Nothing
            'GridView1.DataBind()
            'GridView1.PageIndex = 0

            GridView1.DataSource = DataSource
            GridView1.Visible = True
            GridView1.DataBind()

            ExportToExcel.Visible = IIf(count < 1, False, True)
            Panel1.Visible = IIf(count < 1, False, True)
            SetTotalResult(count)
        End Sub

我希望你能帮我解决这个问题。我似乎不知道可能是什么问题。我尝试了不同的方法并从互联网上找到了解决方案,但没有帮助。非常感谢。

4

1 回答 1

1

你应该使用

GridView1.DataSource = DataSource

从您的代码段中不清楚DataSource是什么,但您BindGridview也在方法中使用它。

开始为变量使用不同的名称,将它们完全命名为属性非常混乱并且会导致错误。

更新

Dim source As SqlDataSource

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)       
    source = New SqlDataSource
    source.ID = "mySource"
    source.SelectCommand = "select * from table"
    source.ConnectionString = connStr

    If Not Page.IsPostBack Then
        GridView1.DataSource = source
        GridView1.DataBind
    End If

End Sub

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
    GridView1.DataSource = source
    GridView1.PageIndex = e.NewPageIndex
    GridView1.DataBind
End Sub
于 2017-02-01T14:14:09.700 回答