写了一个带分页功能的DataGrid

本文介绍了一个简单的 ASP.NET 应用程序实现分页显示数据库记录的方法。通过使用 C# 和 SQL 查询,从 Northwind 数据库中获取 Customers 表的数据,并以分页形式展示在网页上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

闲着无聊,写了一个分页的数据列表,有写的不对的地方请大虾们指正:)


default.aspx:
ContractedBlock.gifExpandedBlockStart.gifCode
<%@ 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">
    
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
    
<asp:Literal ID="Literal2" runat="server"></asp:Literal>
    
</form>
</body>
</html>

default.aspx.cs:
ContractedBlock.gifExpandedBlockStart.gifCode
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        String pageindex 
= Request["pageindex"];
        
if (Request["pageindex"== null || Request["pageindex"].Length == 0)
            pageindex 
= "1";
        
try
        {
            
int.Parse(pageindex);
        }
        
catch
        {
            pageindex 
= "1";
        }
        dbconnection dbc 
= new dbconnection();
        String html 
= dbc.getdatalist("customers""customerid,companyname,contactname,contacttitle"10int.Parse(pageindex));
        Literal1.Text 
= html;

        
if (pageindex == "1")
            Literal2.Text 
= "<a href='default.aspx?pageindex=" + (int.Parse(pageindex) + 1+ "'>下一页</a> 第"+pageindex+"";
        
else
            Literal2.Text 
= "<a href='default.aspx?pageindex=" + (int.Parse(pageindex) + 1+ "'>下一页</a><a href='default.aspx?pageindex=" + (int.Parse(pageindex) - 1+ " '>上一页</a> 第"+pageindex+"";
    }
}

dbconnection.cs:
ContractedBlock.gifExpandedBlockStart.gifCode
using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Text;
/// <summary>
///dbconnection 的摘要说明
/// </summary>
public class dbconnection
{
    
private SqlConnection conn;
    
public dbconnection()
    {
        
//
        
//TODO: 在此处添加构造函数逻辑
        
//
        getConnection();
    }
    
private SqlConnection getConnection()
    {
        conn 
= new SqlConnection("server=192.168.0.100;uid=sa;pwd=sa;database=northwind");
        
return conn;
    }
    
private void openConnection()
    {
        
if (conn != null && conn.State != ConnectionState.Open) conn.Open();
    }
    
private void closeConnection()
    {
        
if (conn != null && conn.State != ConnectionState.Closed) conn.Close();
    }
    
public String getdatalist(String tablename, String fieldlist, int pagesize, int pageindex)
    {
        StringBuilder sb 
= new StringBuilder();

        String id 
= fieldlist.Substring(0, fieldlist.IndexOf(','));
        
int topn = (pageindex - 1* pagesize;
        String query 
= String.Format("select top {0} {1} from {2} where {3} not in (select top {4} {3} from {2})", pagesize, fieldlist, tablename, id, topn);
        SqlCommand cmd 
= new SqlCommand(query, conn);
        openConnection();
        SqlDataReader dr 
= cmd.ExecuteReader();
        sb.Append(
"<table border=0 cellpadding=0 cellspacing=1><tr style='background-color:grey;color:white;'>");
        DataTable dsheaders 
= dr.GetSchemaTable();
        
for (int i = 0; i < dsheaders.Rows.Count; i++)
        {
            sb.Append(String.Format(
"<th>{0}</th>", dsheaders.Rows[i]["columnname"]));
        }
        sb.Append(
"</tr>");
        
int color = 0;
        
while (dr.Read())
        {
            color
++;
            
if (color % 2 == 0) sb.Append("<tr style='background-color:lightblue;'>");
            
else sb.Append("<tr>");
            
for (int a = 0; a < dr.FieldCount; a++)
            {
                sb.Append(String.Format(
"<td>{0}</td>", dr.GetValue(a)));
            }
            sb.Append(
"</tr>");
        }
        sb.Append(
"</table>");
        closeConnection();
        
return sb.ToString();
    }
}




转载于:https://www.cnblogs.com/snow365/archive/2009/09/04/1560403.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值