ASP.NET 학습의 페이지 데이터 귀속GridView
30319 단어 GridView
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView .aspx.cs" Inherits=" .GridView " %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText=" ID">
<ItemTemplate><%#Eval("Uid") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" ">
<ItemTemplate><%#Eval("Subject") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" ">
<ItemTemplate><%#Eval("Content") %></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="TRecordCount" runat="server"></asp:Label>
<asp:Label ID="TPageSize" runat="server"></asp:Label>
<asp:Label ID="TPageCount" runat="server"></asp:Label>
<asp:Label ID="numberPage" runat="server"></asp:Label>
<asp:LinkButton ID="First" runat="server" CommandName="first" OnCommand="First_Command"> </asp:LinkButton>
<asp:LinkButton ID="Pre" runat="server" CommandName="pre" OnCommand="First_Command"> </asp:LinkButton>
<asp:LinkButton ID="Next" runat="server" CommandName="next" OnCommand="First_Command"> </asp:LinkButton>
<asp:LinkButton ID="Last" runat="server" CommandName="last" OnCommand="First_Command"> </asp:LinkButton>
<asp:DropDownList ID="ddl" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
</asp:DropDownList>
</div>
</form>
</body>
</html>
백그라운드 코드:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
namespace
{
/// <summary>
/// GridView
/// :
/// 1、
/// 1) GridView 。 。 GridView1.AutoGenerateColumns = true;
/// 2) 【 、 、 、 、 、 、 、 、 】
///
/// 2、
/// 1) : 、 、 、
/// 2) : , SQLDataAdapter DataSet
/// 3) , 、 、 、
/// 4) 【 】 SQl
/// 5) : Sql ,
/// 6) : , 【 、 】
/// 7) 、 、 、 :
/// 8)
///
/// </summary>
public partial class GridView : System.Web.UI.Page
{
private int PageCount; //
private int RecordCount; //
private int PageSize=5; // //
private int CurrentPageIndex; //
string strConnection = ConfigurationManager.ConnectionStrings["MyDB"].ToString(); //
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
#region
if (!IsPostBack)
{ // : ,
// PageCount、RecordCount、PageSize
DataSet ds=GetDataSet();
RecordCount = ds.Tables[0].Rows.Count; //
//
if (RecordCount % PageSize == 0)
{ // , 0, 。
PageCount = RecordCount / PageSize;
}
else
{ //
PageCount = RecordCount / PageSize + 1;
}
CurrentPageIndex = 1; //
// lable , 。
TRecordCount.Text = RecordCount.ToString();
TPageCount.Text = PageCount.ToString();
TPageSize.Text = PageSize.ToString();
numberPage.Text = CurrentPageIndex.ToString();
// ddl [ ]
for (int i = 1; i <= PageCount; i++)
{
ddl.Items.Add(i.ToString());
}
//
ShowData();
}
#endregion
CurrentPageIndex = int.Parse(numberPage.Text.ToString()); // , CurrentPageIndex
PageCount = int.Parse(TPageCount.Text.ToString()); //
}
/// <summary>
/// , SQLDataAdapter DataSet 【 】
/// </summary>
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
using (SqlConnection sqlConn = new SqlConnection(strConnection))
{ //
string sqlStr = "select Uid,Subject,Content from tb_leaveWord"; //SQL
SqlDataAdapter sda = new SqlDataAdapter(sqlStr, sqlConn);
// ds
sda.Fill(ds);
}
return ds;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public DataSet GetEveryPageData()
{
DataSet ds = new DataSet();
using (SqlConnection sqlconn = new SqlConnection(strConnection))
{
string str = GetSqlStr();
SqlDataAdapter sda = new SqlDataAdapter(str, sqlconn);
sda.Fill(ds);
}
return ds;
}
/// <summary>
/// Sql
/// </summary>
/// <returns></returns>
public string GetSqlStr()
{
#region :
StringBuilder sb = new StringBuilder();
sb.Append("select top(");
sb.Append(PageSize.ToString());
sb.Append(")Uid,Subject,Content from tb_LeaveWord where ID not in((select top(");
sb.Append((PageSize * (CurrentPageIndex - 1)).ToString());
sb.Append(")ID from tb_LeaveWord))");
return sb.ToString();
#endregion
#region :
//StringBuilder sb = new StringBuilder();
//sb.AppendFormat("select top({0})Uid,Subject,Content from tb_LeaveWord where ID not in((select top({1})ID from tb_LeaveWord))", PageSize.ToString(), (PageSize * (CurrentPageIndex - 1)).ToString());
//return sb.ToString();
#endregion
#region :
//string str = "select top(" + PageSize.ToString() + ")Uid,Subject,Content from tb_LeaveWord where ID not in((select top(" + (PageSize * (CurrentPageIndex - 1)).ToString() + ")ID from tb_LeaveWord))";
#endregion
}
/// <summary>
///
/// </summary>
public void ShowData()
{
DataSet Sds = GetEveryPageData();
GridView1.DataSource = Sds;
// , 【 】
//GridView1.AutoGenerateColumns = true;
GridView1.DataBind();
numberPage.Text = CurrentPageIndex.ToString();
ddl.SelectedValue = CurrentPageIndex.ToString(); //
//
if(CurrentPageIndex==1){
First.Enabled = false;
Pre.Enabled = false;
}
else
{
First.Enabled = true;
Pre.Enabled = true;
}
if (CurrentPageIndex==PageCount)
{
Next.Enabled = false;
Last.Enabled = false;
}
else
{
Next.Enabled = true;
Last.Enabled = true;
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
string num = ddl.SelectedValue;
CurrentPageIndex = int.Parse(num);
ShowData();
}
/// <summary>
/// 、 、 、
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void First_Command(object sender, CommandEventArgs e)
{
string result = e.CommandName;
switch(result){
case "first":
CurrentPageIndex = 1;
break;
case "pre":
CurrentPageIndex--;
break;
case "next":
CurrentPageIndex++;
break;
case "last":
CurrentPageIndex = PageCount;
break;
}
ShowData();//
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
GridView의 데이터를 EXCEL로 내보내기백그라운드에서 데이터 조회 작업을 할 때, 우리는 GridView 컨트롤러를 사용하여 데이터 표시를 할 수 있습니다. 때로는 GridView 컨트롤러에 표시된 데이터를 EXCEL 파일로 내보내야 하고, GridVie...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.