| 
           
            | 
  (C#)  ASP.NET SQL Server List Record Paging/Pagination ตัวอย่างการเขียน ASP.NET กับ SQL Server โดยใช้ GridView (.NET 2.0) ในการแสดงข้อมูลและกำหนด AllowPaging="True" เพื่อแบ่งการแสดงผลออกเป็นหน้า ๆ 
    |  
        (C#) ASP.NET SQL Server List Record Paging/Pagination       |  
 Instance NameSpace
 
 C#
 Using System.Data; 
Using System.Data.SqlClient; 
 ASP.NET & System.Data.SqlClient
 
 Language Code : VB.NET ||
  C# 
 AspNetSQLServerListRecordPaging.aspx
 
 
 <%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void Page_Load(object sender,EventArgs e)		
	{
		if(!Page.IsPostBack)
		{
			BindData();
		}
    }
	void BindData()
	{
        SqlConnection objConn = new SqlConnection();
		SqlCommand objCmd = new SqlCommand();
        SqlDataAdapter dtAdapter = new SqlDataAdapter();
		DataSet ds = new DataSet();
		String strConnString,strSQL;
		strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
		strSQL = "SELECT * FROM customer";
		objConn.ConnectionString = strConnString;
		objCmd.Connection = objConn;
		objCmd.CommandText = strSQL	;
		objCmd.CommandType = CommandType.Text;
		dtAdapter.SelectCommand = objCmd;
		dtAdapter.Fill(ds);
		//*** BindData to GridView ***//
		myGridView.DataSource = ds;
		myGridView.DataBind();
		dtAdapter = null;		
		objConn.Close();
		objConn = null;
	}
	void myGridView_RowDataBound(Object s, GridViewRowEventArgs e) 
	{
		//*** CustomerID ***//
		Label lblCustomerID = (Label)(e.Row.FindControl("lblCustomerID"));
		if (lblCustomerID != null)
		{
			lblCustomerID.Text = (string)DataBinder.Eval(e.Row.DataItem, "CustomerID");
		}
		//*** Email ***//
		Label lblName = (Label)(e.Row.FindControl("lblName"));
		if (lblName != null)
		{
			lblName.Text = (string)DataBinder.Eval(e.Row.DataItem, "Name");
		}
		//*** Name ***//
		Label lblEmail = (Label)(e.Row.FindControl("lblEmail"));
		if (lblEmail != null)
		{
			lblEmail.Text = (string)DataBinder.Eval(e.Row.DataItem, "Email");
		}
		//*** CountryCode ***//
		Label lblCountryCode = (Label)(e.Row.FindControl("lblCountryCode"));
		if (lblCountryCode != null)
		{
			lblCountryCode.Text = (string)DataBinder.Eval(e.Row.DataItem, "CountryCode");
		}
		//*** Budget ***//
		Label lblBudget = (Label)(e.Row.FindControl("lblBudget"));
		if (lblBudget != null)
		{
			lblBudget.Text = DataBinder.Eval(e.Row.DataItem, "Budget").ToString();
		}
		//*** Used ***//
		Label lblUsed = (Label)(e.Row.FindControl("lblUsed"));
		if (lblUsed != null)
		{
			lblUsed.Text = DataBinder.Eval(e.Row.DataItem, "Used").ToString();
		}
	}
	
	void ShowPageCommand(Object s, GridViewPageEventArgs e)
	{
		myGridView.PageIndex = e.NewPageIndex;
		BindData();
	}
	
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - SQL Server</title>
</head>
<body>
	<form id="form1" runat="server">
		<asp:GridView id="myGridView" PageSize="2" 
		OnPageIndexChanging="ShowPageCommand"
		onRowDataBound="myGridView_RowDataBound"
		AutoGenerateColumns="False" AllowPaging="True" runat="server">
		<HeaderStyle BackColor="#cccccc"></HeaderStyle>
		<AlternatingRowStyle BackColor="#e8e8e8"></AlternatingRowStyle>
		<Columns>
		<asp:TemplateField HeaderText="CustomerID">
			<ItemTemplate>
				<asp:Label id="lblCustomerID" runat="server"></asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Name">
			<ItemTemplate>
				<asp:Label id="lblName" runat="server"></asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Email">
			<ItemTemplate>
				<asp:Label id="lblEmail" runat="server"></asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="CountryCode">
			<ItemTemplate>
				<asp:Label id="lblCountryCode" runat="server"></asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Budget">
			<ItemTemplate>
				<asp:Label id="lblBudget" runat="server"></asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Used">
			<ItemTemplate>
				<asp:Label id="lblUsed" runat="server"></asp:Label>
			</ItemTemplate>
		</asp:TemplateField>
		</Columns>
		</asp:GridView>		
	</form>
</body>
</html>
 Screenshot
 
 
   
 
 
 
 |  
            | 
			 ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท 
 |  
 
 
 
          
            | 
                
                  |  |  
                  |  | By : | ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |  
                  |  | Score Rating : |     |  |  
                  |  | Create/Update Date : | 2008-10-26 23:02:00            /
            2009-07-19 08:39:32 |  
                  |  | Download : |   |  |  
         
          | 
              
                | Sponsored Links / Related |  |  
          | 
 |  |   
          |  |  
 |   |