我在原来的程序进行分页显示,把孟子的程序改了一下。我有几个地方不清楚,1。孟子的第一行的Inherits="DataGridPaging"不能加进去,2。Page_Load中有Handles MyBase .Init,作用是什么,我不清楚,我把它直接抄下来了。
3.dgSearch Result_Page中,startIndex的作用我不清楚,但是程序中别处也没有出现这个,我就直接抄下来了。
我原以为这几个地方会出问题,结果系统提示:dgSearchResult.CurrentPageIndex=e.NewPageIndex 编译错误,Reference to a non-shared member requires an object reference,这个问题困扰了我一整天,网上问了几次也没解决,似乎不是很大的问题,但是我却没能作出来,我把编译结果和源代码贴出来,大家帮忙指点一下。
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30469: Reference to a non-shared member requires an object reference.
Source Error:
Line 47: Dim startIndex As Integer
Line 48: startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
Line 49: dgSearchResult.CurrentPageIndex = e.NewPageIndex
Line 50: BindGrid()
Line 51: End Sub
程序源码:
<%@ Page Language=VB EnableSessionState=true Debug=true AutoEventWireup="false" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<HTML>
<HEAD>
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<TITLE>Search Results</TITLE>
<script runat="server">
Public Class DataGridPaging
Inherits System.Web.UI.Page
Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid
Protected WithEvents btnFirst As System.Web.UI.WebControls.LinkButton
Protected WithEvents btnPrev As System.Web.UI.WebControls.LinkButton
Protected WithEvents btnNext As System.Web.UI.WebControls.LinkButton
Protected WithEvents btnLast As System.Web.UI.WebControls.LinkButton
Protected WithEvents lblCurrentIndex As System.Web.UI.WebControls.Label
Protected WithEvents lblPageCount As System.Web.UI.WebControls.Label
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
#Region " Web Form Designer Generated Code "
This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Init
CODEGEN: This method call is required by the Web Form Designer
Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
OpenDatabase()
BindGrid()
End Sub
Sub dgSelectResult_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
Dim startIndex As Integer
startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
dgSearchResult.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub
Sub BindGrid()
Dim DBConn as SqlConnection
Dim DBCommand As SqlDataAdapter
Dim DSPageData as New DataSet
if Session("Search")="Null"
lblCount.Text = "请正确输入查询条件"
else If Session("SearchSQLJihua") = nothing Then
If Session("SearchSQLJihua")=nothing Then
DBCommand = New SqlDataAdapter _
(Session("SearchSQLRizhi"),DBConn)
DBCommand.Fill(DSPageData, _
"rizhi")
dgSearchResult.DataSource = _
DSPageData.Tables("rizhi").DefaultView
dgSearchResult.DataBind()
If DSPageData.Tables("rizhi").Rows.Count = 1 Then
lblCount.Text = "你从日志表中查到了1条记录."
Else
lblCount.Text = "你从日志表中查到了 " _
& DSPageData.Tables("rizhi").Rows.Count _
& " 条记录."
End If
Session("SearchSQLRizhi") = nothing
Else If Session("SearchSQLRizhi") = nothing Then
DBCommand = New SqlDataAdapter _
(Session("SearchSQLJihua"),DBConn)
DBCommand.Fill(DSPageData, _
"jihua")
dgSearchResult.DataSource = _
DSPageData.Tables("jihua").DefaultView
dgSearchResult.DataBind()
If DSPageData.Tables("jihua").Rows.Count = 1 Then
lblCount.Text = "你从计划表中查找有1条记录."
Else
lblCount.Text = "你从计划表中查到了 " _
& DSPageData.Tables("jihua").Rows.Count _
& " 条记录."
End If
Session("SearchSQLJihua") = nothing
End IF
End Sub
Dim DBConn as SqlConnection
Public Sub OpenDatabase()
DBConn = New SqlConnection("server=(local);" _
& "Initial Catalog=gzhb;" _
& "User Id=sa;" _
& "Password=yourpassword;" _
& "Trusted_Connection=yes;")
End Sub
End Class
</script>
</HEAD>
<BODY BACKGROUND="./house-wide.gif" TEXT="purple" LINK="blue" VLINK="black" ALINK="red"
LEFTMARGIN="150">
<Form runat="server" ID="Form1">
<asp:Label id="lblTitle" BorderWidth="5px" BorderStyle="7" BackColor="lightyellow" Width="439"
Font-Size="22pt" Font-Name="Comic Sans MS" Text="<CENTER>查询历史记录</CENTER>" runat="server" />
<P></P>
<asp:Label id="lblCount" Font-Size="15pt" Font-Name="Comic Sans MS" runat="server" />
<P></P>
<ASP:DataGrid id="dgSearchResult" Width="90%" BackColor="beige" AlternatingItemStyle-BackColor = "cornsilk"BorderColor="black" ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Arial" Font-Size="8pt"ForeColor="Black" AllowPaging="True" HeaderStyle-BackColor="burlywood" PageSize="5" OnPageIndexChanged ="dgSearchResult_Page" HeaderStyle-Font-Bold="True" runat="server" PagerStyle-Mode="NumericPages"
></ASP:DataGrid>
</Form>
</BODY>
</HTML>
你没有了解分页的本质!照抄是不行的。学会自己控制!
其实不需要那么复杂的,你在datagrid改了属性,然后再加响应事件就可以了