Datalist.aspx
--------------------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DataList.aspx.vb" Inherits="DataList" Debug="true" %>
<%@ Import Namespace="system.data" %>
<%@Import Namespace="System.data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DataList</title>
<script language="vb" runat="server">
Dim conn As System.Data.SqlClient.SqlConnection
Dim currentpage As Integer
Dim PageSize, intPageCount, RecordCount As Integer
Dim startIndex As Integer
Dim str, strCount As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
conn = New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("www_22745678_comConnectionString").ConnectionString)
conn.Open()
'-----------------------------
PageSize = 50
'-----------------------------
If Not Page.IsPostBack Then
' 初始化
str = "select news_id,news_title,news_data From News"
strCount = "select count(news_id) as newsCount From News"
ViewState("str") = str
ViewState("strCount") = strCount
ViewState("currentpage") = 0
ViewState("intPageCount") = 0
startIndex = 0
RecordCount = CalculateRecord(ViewState("strCount")) ' 统计有多少条
' intPageCount = RecordCount / PageSize ''''--问题就出现在这边 ..
' intPageCount = 17 / 50
intPageCount = getPageCount(RecordCount / PageSize)
' If InStr(CStr(RecordCount / PageSize), ".") > 0 Then '1.44,1.52,0.51,0.11,0.5是基准
'intPageCount = intPageCount + 1
'Else
' intPageCount = intPageCount
' End If
' Response.Write(RecordCount / PageSize)
' Response.Write(intPageCount)
ViewState("intPageCount") = intPageCount
lblRecordCount.Text = RecordCount
lblintPageCount.Text = ViewState("intPageCount")
startIndex = ViewState("currentpage") * PageSize
DataListBind(str, startIndex)
End If
End Sub
Sub DataListBind(ByVal str As String, ByVal startIndex As Integer)
Dim ds As New DataSet
' Response.Write(str)
Dim myAdapter As New SqlDataAdapter(str, conn)
myAdapter.Fill(ds, startIndex, PageSize, "newsTable")
DataList1.DataSource = ds.Tables("newsTable").DefaultView
DataList1.DataBind()
Me.LinkButton1.Enabled = True
Me.LinkButton2.Enabled = True
Me.LinkButton3.Enabled = True
Me.LinkButton4.Enabled = True
If ViewState("currentpage") = 0 Then
Me.LinkButton1.Enabled = False
Me.LinkButton2.Enabled = False
End If
If ViewState("currentpage") = ViewState("intPageCount") - 1 Then
Me.LinkButton3.Enabled = False
Me.LinkButton4.Enabled = False
End If
End Sub
Sub PageIndex(ByVal sender As Object, ByVal e As DataListItemEventArgs)
Dim l As Label
l = e.Item.FindControl("itemIndex")
If Not l Is Nothing Then
l.Text = currentpage * PageSize + e.Item.ItemIndex + 1
End If
End Sub
Public Sub PageClick(ByVal sender As Object, ByVal e As CommandEventArgs)
currentpage = CInt(ViewState("currentpage"))
intPageCount = CInt(ViewState("intPageCount"))
str = ViewState("str")
RecordCount = CalculateRecord(ViewState("strCount"))
intPageCount = getPageCount(RecordCount / PageSize)
lblRecordCount.Text = RecordCount
lblintPageCount.Text = intPageCount
Dim cmd As String = e.CommandName
Select Case cmd
Case "First"
currentpage = 0
Case "pre"
currentpage -= 1
Case "Next"
currentpage += 1
Case "Last"
currentpage = intPageCount - 1
End Select
startIndex = currentpage * PageSize
' Response.Write(currentpage.ToString + "-" + startIndex.ToString)
ViewState("currentpage") = currentpage
ViewState("intPageCount") = intPageCount
ViewState("str") = str
DataListBind(ViewState("str"), startIndex)
' DataListBind("select news_id,news_title,news_data From News", 1)
End Sub
Public Sub search_Click(ByVal sender As Object, ByVal e As EventArgs)
ViewState("currentpage") = 0
ViewState("intPageCount") = 0
ViewState("str") = "select news_id,news_title,news_date From news where news_title like '%" + Keywords.Text.Trim + "%' "
ViewState("strCount") = "select count(news_id) as newsCount from News where news_title like '%" + Keywords.Text.Trim + "%'"
RecordCount = CalculateRecord(ViewState("strCount"))
lblRecordCount.Text = RecordCount
intPageCount = getPageCount(RecordCount / PageSize)
' intPageCount = RecordCount / PageSize
' Response.Write("<br>")
' Response.Write(RecordCount / PageSize)
' Response.Write("<br>")
' Response.Write(CInt(RecordCount / PageSize))
'If InStr(CStr(RecordCount / PageSize), ".") > 0 And (RecordCount / PageSize) < 1 Then '1.44,2,51,cint(0.11)=1,cint(0.66)=1
'intPageCount = intPageCount + 1
' Else
' intPageCount = intPageCount
' End If
lblintPageCount.Text = intPageCount
' Response.Write("<br>")
' Response.Write(intPageCount)
ViewState("intPageCount") = intPageCount
startIndex = ViewState("currentpage") * PageSize
DataListBind(ViewState("str"), startIndex)
End Sub
Function CalculateRecord(ByVal strCount As String) As Integer
Dim intCount As Integer
Dim mycmd As New SqlCommand(strCount, conn)
Dim mydr As SqlDataReader
mydr = mycmd.ExecuteReader
If mydr.Read Then
intCount = Int32.Parse(mydr("newsCount").ToString)
Else
intCount = 0
End If
mydr.Close()
Return intCount
End Function
Function getPageCount(ByVal str As String) As Integer
Dim realIntPageCount As Integer
If InStr(str, ".") <> 0 Then
realIntPageCount = Split(str, ".")(0) + 1
Else
realIntPageCount = Int32.Parse(str)
End If
Return realIntPageCount
End Function
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="Keywords" runat="server"></asp:TextBox>
<asp:Button ID="Button1" OnClick="search_Click" runat="server" Text="Search" />
<asp:DataList OnItemCreated="PageIndex" ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="itemIndex" runat="server" Text="Label"></asp:Label> <%#Eval("news_title") %>
</ItemTemplate>
</asp:DataList>
---------------------------------------------------------------
共[<asp:Label ID="lblRecordCount" runat="server" Text="Label"></asp:Label>]条,页数共[<asp:Label ID="lblintPageCount" runat="server" Text="Label"></asp:Label>]:
<asp:LinkButton ID="LinkButton1" CommandName="First" OnCommand="PageClick" runat="server">首 页</asp:LinkButton> |
<asp:LinkButton ID="LinkButton2" CommandName="pre" OnCommand="PageClick" runat="server">上一页</asp:LinkButton> |
<asp:LinkButton ID="LinkButton3" CommandName="Next" OnCommand="PageClick" runat="server">下一页</asp:LinkButton> |
<asp:LinkButton ID="LinkButton4" CommandName="Last" OnCommand="PageClick" runat="server">尾 页</asp:LinkButton>
</div>
</form>
</body>
</html>
<%@ Import Namespace="system.data" %>
<%@Import Namespace="System.data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DataList</title>
<script language="vb" runat="server">
Dim conn As System.Data.SqlClient.SqlConnection
Dim currentpage As Integer
Dim PageSize, intPageCount, RecordCount As Integer
Dim startIndex As Integer
Dim str, strCount As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
conn = New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("www_22745678_comConnectionString").ConnectionString)
conn.Open()
'-----------------------------
PageSize = 50
'-----------------------------
If Not Page.IsPostBack Then
' 初始化
str = "select news_id,news_title,news_data From News"
strCount = "select count(news_id) as newsCount From News"
ViewState("str") = str
ViewState("strCount") = strCount
ViewState("currentpage") = 0
ViewState("intPageCount") = 0
startIndex = 0
RecordCount = CalculateRecord(ViewState("strCount")) ' 统计有多少条
' intPageCount = RecordCount / PageSize ''''--问题就出现在这边 ..
' intPageCount = 17 / 50
intPageCount = getPageCount(RecordCount / PageSize)
' If InStr(CStr(RecordCount / PageSize), ".") > 0 Then '1.44,1.52,0.51,0.11,0.5是基准
'intPageCount = intPageCount + 1
'Else
' intPageCount = intPageCount
' End If
' Response.Write(RecordCount / PageSize)
' Response.Write(intPageCount)
ViewState("intPageCount") = intPageCount
lblRecordCount.Text = RecordCount
lblintPageCount.Text = ViewState("intPageCount")
startIndex = ViewState("currentpage") * PageSize
DataListBind(str, startIndex)
End If
End Sub
Sub DataListBind(ByVal str As String, ByVal startIndex As Integer)
Dim ds As New DataSet
' Response.Write(str)
Dim myAdapter As New SqlDataAdapter(str, conn)
myAdapter.Fill(ds, startIndex, PageSize, "newsTable")
DataList1.DataSource = ds.Tables("newsTable").DefaultView
DataList1.DataBind()
Me.LinkButton1.Enabled = True
Me.LinkButton2.Enabled = True
Me.LinkButton3.Enabled = True
Me.LinkButton4.Enabled = True
If ViewState("currentpage") = 0 Then
Me.LinkButton1.Enabled = False
Me.LinkButton2.Enabled = False
End If
If ViewState("currentpage") = ViewState("intPageCount") - 1 Then
Me.LinkButton3.Enabled = False
Me.LinkButton4.Enabled = False
End If
End Sub
Sub PageIndex(ByVal sender As Object, ByVal e As DataListItemEventArgs)
Dim l As Label
l = e.Item.FindControl("itemIndex")
If Not l Is Nothing Then
l.Text = currentpage * PageSize + e.Item.ItemIndex + 1
End If
End Sub
Public Sub PageClick(ByVal sender As Object, ByVal e As CommandEventArgs)
currentpage = CInt(ViewState("currentpage"))
intPageCount = CInt(ViewState("intPageCount"))
str = ViewState("str")
RecordCount = CalculateRecord(ViewState("strCount"))
intPageCount = getPageCount(RecordCount / PageSize)
lblRecordCount.Text = RecordCount
lblintPageCount.Text = intPageCount
Dim cmd As String = e.CommandName
Select Case cmd
Case "First"
currentpage = 0
Case "pre"
currentpage -= 1
Case "Next"
currentpage += 1
Case "Last"
currentpage = intPageCount - 1
End Select
startIndex = currentpage * PageSize
' Response.Write(currentpage.ToString + "-" + startIndex.ToString)
ViewState("currentpage") = currentpage
ViewState("intPageCount") = intPageCount
ViewState("str") = str
DataListBind(ViewState("str"), startIndex)
' DataListBind("select news_id,news_title,news_data From News", 1)
End Sub
Public Sub search_Click(ByVal sender As Object, ByVal e As EventArgs)
ViewState("currentpage") = 0
ViewState("intPageCount") = 0
ViewState("str") = "select news_id,news_title,news_date From news where news_title like '%" + Keywords.Text.Trim + "%' "
ViewState("strCount") = "select count(news_id) as newsCount from News where news_title like '%" + Keywords.Text.Trim + "%'"
RecordCount = CalculateRecord(ViewState("strCount"))
lblRecordCount.Text = RecordCount
intPageCount = getPageCount(RecordCount / PageSize)
' intPageCount = RecordCount / PageSize
' Response.Write("<br>")
' Response.Write(RecordCount / PageSize)
' Response.Write("<br>")
' Response.Write(CInt(RecordCount / PageSize))
'If InStr(CStr(RecordCount / PageSize), ".") > 0 And (RecordCount / PageSize) < 1 Then '1.44,2,51,cint(0.11)=1,cint(0.66)=1
'intPageCount = intPageCount + 1
' Else
' intPageCount = intPageCount
' End If
lblintPageCount.Text = intPageCount
' Response.Write("<br>")
' Response.Write(intPageCount)
ViewState("intPageCount") = intPageCount
startIndex = ViewState("currentpage") * PageSize
DataListBind(ViewState("str"), startIndex)
End Sub
Function CalculateRecord(ByVal strCount As String) As Integer
Dim intCount As Integer
Dim mycmd As New SqlCommand(strCount, conn)
Dim mydr As SqlDataReader
mydr = mycmd.ExecuteReader
If mydr.Read Then
intCount = Int32.Parse(mydr("newsCount").ToString)
Else
intCount = 0
End If
mydr.Close()
Return intCount
End Function
Function getPageCount(ByVal str As String) As Integer
Dim realIntPageCount As Integer
If InStr(str, ".") <> 0 Then
realIntPageCount = Split(str, ".")(0) + 1
Else
realIntPageCount = Int32.Parse(str)
End If
Return realIntPageCount
End Function
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="Keywords" runat="server"></asp:TextBox>
<asp:Button ID="Button1" OnClick="search_Click" runat="server" Text="Search" />
<asp:DataList OnItemCreated="PageIndex" ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="itemIndex" runat="server" Text="Label"></asp:Label> <%#Eval("news_title") %>
</ItemTemplate>
</asp:DataList>
---------------------------------------------------------------
共[<asp:Label ID="lblRecordCount" runat="server" Text="Label"></asp:Label>]条,页数共[<asp:Label ID="lblintPageCount" runat="server" Text="Label"></asp:Label>]:
<asp:LinkButton ID="LinkButton1" CommandName="First" OnCommand="PageClick" runat="server">首 页</asp:LinkButton> |
<asp:LinkButton ID="LinkButton2" CommandName="pre" OnCommand="PageClick" runat="server">上一页</asp:LinkButton> |
<asp:LinkButton ID="LinkButton3" CommandName="Next" OnCommand="PageClick" runat="server">下一页</asp:LinkButton> |
<asp:LinkButton ID="LinkButton4" CommandName="Last" OnCommand="PageClick" runat="server">尾 页</asp:LinkButton>
</div>
</form>
</body>
</html>