您的位置:首页 > 数据库

VS2005中Gridview绑定数据库数据并分页-----更新中……

2007-04-12 20:48 357 查看
首先创建一个数据库连接类
------------------------db.cs---------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// db 的摘要说明
/// </summary>
public class db
{
public db()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static System.Data.SqlClient.SqlConnection creatdb()
{
return new System.Data.SqlClient.SqlConnection("server=.;database=okweb;integrated security=true;");
}
}

-------------------------index.aspx.cs-------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.bindtogridview();
}
}
private void bindtogridview()
{
SqlConnection conn = db.creatdb();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select articleid,title from afu_news", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
this.GridView1.DataKeyNames = new string[]{"articleid"};
this.GridView1.DataSource = ds.Tables["emp"];
this.GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.bindtogridview();
}

----------------------index.aspx---------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="780">
<tr>
<td colspan="3" style="height: 64px">
</td>
</tr>
<tr>
<td style="width: 100px; height: 128px">
</td>
<td style="width: 47px; height: 128px">
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" Width="423px" style="font-size: 12px; vertical-align: text-bottom; list-style-type: none; text-align: left; color: black; font-family: 宋体; clear: right; float: right;" CellSpacing="1" GridLines="None" Height="50px" PageSize="5" AllowPaging=True DataKeyNames="articleid" OnPageIndexChanging="GridView1_PageIndexChanging">
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<RowStyle ForeColor="Black" BackColor="#DEDFDE" Height="25px" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" BorderStyle="None" Height="30px" Font-Overline="False" />
<PagerSettings FirstPageText="第一页" LastPageText="最后页" Mode="NextPreviousFirstLast"
NextPageText="下一页" PreviousPageText="上一页" />
</asp:GridView>
</td>
<td style="width: 100px; height: 128px">
</td>
</tr>
<tr>
<td style="width: 100px; height: 153px">
</td>
<td style="width: 47px; height: 153px">
</td>
<td style="width: 100px; height: 153px">
</td>
</tr>
</table>

</div>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐