📄 default2.aspx
字号:
<%@ Page Language="C#" debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
void Page_Load( Object sender , EventArgs e)
{
//Build the Grid only if the page has been accessed for the first time
if( !IsPostBack )
BuildGrid();
}
public void BuildGrid()
{
SqlConnection myConnection =
new SqlConnection(
"server=(local)\\NetSDK;database=Northwind;Trusted_Connection=yes" );
SqlDataAdapter myAdapter =
new SqlDataAdapter(
"SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products" ,
myConnection);
//Fill the DataSet
DataSet ds = new DataSet();
//Get the startRecord Count
//Remember database count is zero based so first decrease the value of
//the current page
int startRecord = ( int.Parse( CurrentPage.Value ) - 1 ) *
int.Parse( PageSize.Value );
//Fetch only the necessary records.
myAdapter.Fill( ds , startRecord , int.Parse( PageSize.Value ) , "Products");
//DataBind the DataList
MyDataList.DataSource = ds.Tables["Products"].DefaultView;
MyDataList.DataBind();
//Second Part
//Create a new Command to select the total number of records
SqlCommand myCmd = new SqlCommand( "SELECT Count(*) from Products",
myConnection );
myConnection.Open();
//retrieve the value
TotalSize.Value = myCmd.ExecuteScalar().ToString() ;
myConnection.Close();
BuildPagers();
}
public void Page_DataList( object sender, EventArgs e )
{
//Check for Button clicked
if( ((LinkButton)sender).ID == "Prev" )
{
//Check if we are on any page greater than 0
if( ( int.Parse( CurrentPage.Value ) - 1 ) >= 0 )
{
//Decrease the CurrentPage Value
CurrentPage.Value = ( int.Parse( CurrentPage.Value ) - 1 ).ToString() ;
}
}
else if( ((LinkButton)sender).ID == "Next" )
{
//Check if we can display the next page.
if( ( int.Parse( CurrentPage.Value ) * int.Parse( PageSize.Value ) )
< int.Parse( TotalSize.Value ) )
{
//Increment the CurrentPage value
CurrentPage.Value = ( int.Parse( CurrentPage.Value ) + 1 ).ToString() ;
}
}
//Rebuild the Grid
BuildGrid();
}
public void BuildPagers()
{
//Check if its possible to have the previous page
if( ( int.Parse( CurrentPage.Value ) - 1 ) <= 0 )
{
Prev.Enabled = false;
}
else
{
Prev.Enabled = true ;
}
//Check if its possible to have the next page
if( ( int.Parse( CurrentPage.Value ) * int.Parse( PageSize.Value ) )
>= int.Parse( TotalSize.Value ) )
{
Next.Enabled = false;
}
else
{
Next.Enabled = true ;
}
}
</script>
<body>
<h1>Products Listing</h1>
<form id="Form1" runat="server">
<ASP:DataList id="MyDataList" RepeatColumns="2"
RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<div style="padding:15,15,15,15;font-size:10pt;font-family:Verdana">
<div style="font:12pt verdana;color:darkred">
<i><b><%# DataBinder.Eval(Container.DataItem, "ProductName") %></i></b>
</div>
<br>
<b>Product ID: </b><%# DataBinder.Eval(Container.DataItem, "ProductID") %><br>
<b>Quantity per Unit: </b>
<%# DataBinder.Eval(Container.DataItem, "QuantityPerUnit") %>
<br>
<b>Price: </b><%# DataBinder.Eval(Container.DataItem, "UnitPrice", "$ {0}") %><p>
</div>
</ItemTemplate>
</ASP:DataList>
<input type="hidden" id="PageSize" value="10" runat="server">
<input type="hidden" id="CurrentPage" value="1" runat="server">
<input type="hidden" id="TotalSize" runat="server">
<asp:LinkButton id="Prev" Text="<< Previous" OnClick="Page_DataList" runat="server" />
<asp:LinkButton id="Next" Text="Next >>" OnClick="Page_DataList" runat="server" />
</form>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -