datagrid分页,点击哪一页都是相同的数据问题(保证不止这些数据)
--------OnlineBook.aspx---------------<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="OnlineBook.Index" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="width:300px;float:right">
<asp:Label ID="label2" runat="server" Text="用户名:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Label ID="label3" runat="server" Text="密码: "></asp:Label>
<asp:TextBox ID="password" runat="server" TextMode="Password" Width="138px"></asp:TextBox>
</div>
<div style="width:300px; display:inline">
<asp:Label ID="label1" runat="server" Text="节目查询"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="查询" />
<asp:HyperLink ID="HyperLink1" runat="server">高级搜索</asp:HyperLink>
</div>
</div>
<div>
<asp:DataGrid ID="dgcom" Style="z-index: 111; left: 138px; position: absolute; top: 218px"
Width="920px" BackColor="white" Height="182px" BorderColor="black" BorderStyle="None" AllowPaging="True" AllowCustomPaging="true"
PageSize="5" BorderWidth="1" CellPadding="3" OnPageIndexChanged="MyDataGrid_Page"
AutoGenerateColumns="false" runat="server">
<%-- <SelectedItemStyle Font-Bold="true" ForeColor="blue" BackColor="Black"></SelectedItemStyle>
<AlternatingItemStyle BackColor="Blue"></AlternatingItemStyle>
<ItemStyle ForeColor="Beige" BackColor="Blue"></ItemStyle>--%>
<HeaderStyle Font-Bold="true" ForeColor="Beige" BackColor="black"></HeaderStyle>
<%-- <FooterStyle ForeColor="#4A3C8C" BackColor="AliceBlue"></FooterStyle>--%>
<Columns>
<asp:BoundColumn HeaderText="书名" DataField="bookname" />
<asp:BoundColumn HeaderText="作者" DataField="author" />
<asp:BoundColumn HeaderText="出版社" DataField="address" />
<asp:BoundColumn HeaderText="ISBN号" DataField="booknumber" />
<asp:BoundColumn HeaderText="原价" DataField="yprice" />
<asp:BoundColumn HeaderText="折扣" DataField="zhekou" />
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="DarkGreen" BackColor="Red" Mode="NumericPages">
</PagerStyle>
</asp:DataGrid>
<asp:Label ID="label4"
Style="z-index: 114; left: 139px; position: absolute; top: 197px; width: 920px; height: 19px;"
BackColor="InactiveCaptionText" BorderColor="black"
BorderStyle="None" PageSize="5" GridLines="Horizontal" BorderWidth="1" CellPadding="3"
AutoGenerateColumns="false" OnItemCommand="Grid_CartCommand"
runat="server" Text="计算机类"></asp:Label>
</form>
</body>
</html>
------OnlineBook.aspx.cs---------
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
namespace OnlineBook
{
public partial class Index : System.Web.UI.Page
{
//DataSet getbata;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
public void BindGrid() {
dgcom.DataSource = CreateDataSource();
dgcom.VirtualItemCount = 16;
dgcom.DataBind();
}
public void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)
{
dgcom.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
ICollection CreateDataSource() {
string connectionstring = ConfigurationSettings.AppSettings["connectionstring"].ToString().Trim();//web.config的配置,在下面;
string sql = "select * from bookinfo where T_type='计算机'";
SqlConnection conn = new SqlConnection(connectionstring);
//SqlCommand mycmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataTable bata1 = new DataTable();
da.Fill(bata1);
DataView dv = new DataView(bata1);
return dv;
}
}
}
------web.config--------
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<!--
设置 compilation debug="true" 可将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<compilation defaultLanguage="C#" debug="true">
</compilation>
<!--
通过 <authentication> 节可以配置 用来
识别进入用户的
安全身份验证模式。
-->
<authentication mode="Windows" />
<customErrors mode="RemoteOnly" />
<authorization>
<allow users="*"/> <!--允许所有用户-->
</authorization>
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
<!--全球化
此节设置应用程序的全球化设置-->
</system.web>
<appSettings>
<add key="connectionstring" value="server=localhost;uid=sa;pwd=sql2005;database=shop"/>
</appSettings>
</configuration>