8.11.05

Động đất

Hôm nay vào lúc 3h5phút chiều tại công ty có động đất. Lần đầu tiên biết được cảm giác đó. Mọi thứ đều run lên, cảm giác sợ đến. CŨng may là chỉ là địa chấn nhỏ thôi, không sao hết.

1.11.05

Phân trang trong ASP.NET

Trong asp.net thì có rất nhiều cách để phân trang như: sử dụng paging trong Datagrid, hay sử dụng các câu lệnh SQL như cursor hay dùng bảng tạm... Nhưng đây là một cách khác. Sử dụng method Fill của DataAdapter để thêm vào DataSet các row theo index (chỉ mục).
Source code:
WebForm1.aspx



<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="paging_datalist.WebForm1" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

<HEAD>

<title>WebForm1</title>

<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

<meta name="CODE_LANGUAGE" Content="C#">

<meta name="vs_defaultClientScript" content="JavaScript">

<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

</HEAD>

<body MS_POSITIONING="GridLayout">

<form id="Form1" method="post" runat="server">

<table cellspacing="0" cellpadding="0" border="0" style="BORDER-RIGHT: red 1px solid; BORDER-TOP: red 1px solid; BORDER-LEFT: red 1px solid; BORDER-BOTTOM: red 1px solid">

<tr>

<td style="BORDER-RIGHT: red 1px solid; BORDER-LEFT-WIDTH: 1px; BORDER-LEFT-COLOR: red; BORDER-BOTTOM: red 1px solid">

<

<asp:DataList id="DataList1" runat="server" Visible="True" ForeColor="DarkGoldenrod">



<HeaderTemplate>

<font color="#6600ff">Using DataList</font>

</HeaderTemplate>

<FooterTemplate>



</FooterTemplate>

<ItemTemplate>

Product ID:

<%# DataBinder.Eval(Container.DataItem , "ProductID")%>

<br>

ProductName:

<%# DataBinder.Eval(Container.DataItem, "ProductName")%>

<br>

Unit Price:

<%# DataBinder.Eval(Container.DataItem, "UnitPrice")%>

<br>

<hr size="1">

</ItemTemplate>

</asp:DataList></td>

<td style="BORDER-BOTTOM: red 1px solid; HEIGHT: 492px">

<asp:Repeater id="Repeater1" runat="server">

<HeaderTemplate>

<font color="#6600ff">Using Repeater</font><br>

</HeaderTemplate>

<ItemTemplate>

Product ID:

<%# DataBinder.Eval(Container.DataItem , "ProductID")%>

<br>

ProductName:

<%# DataBinder.Eval(Container.DataItem, "ProductName")%>

<br>

Unit Price:

<%# DataBinder.Eval(Container.DataItem, "UnitPrice")%>

<br>

<hr>

</ItemTemplate>

<FooterTemplate>



</FooterTemplate>

</asp:Repeater>

</td>

</tr>

<tr>

<td rowSpan="1"><asp:LinkButton id="LinkButton1" runat="server">previous</asp:LinkButton>

<asp:LinkButton id="LinkButton2" runat="server">next</asp:LinkButton></td>

</tr>

<tr>

<td>Index:

<asp:Label id="Label1" runat="server"></asp:Label></td>

</tr>

<tr>

<td>Item of page:

<asp:TextBox id="TextBox2" runat="server"></asp:TextBox></td>

</tr>

<tr>

<td></td>

</tr>

</table>

</form>

</body>

</HTML>



WebForm1.aspx.cs:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/********************************************************************
created: 2005/11/01
created: 1:11:2005 14:51
filename: WebForm1.aspx.cs

file base: WebForm1.aspx
file ext: cs
author: Tran Vinh

purpose: Paging in Datalist and Repeater
*********************************************************************/



namespace paging_datalist
{
///
/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Repeater Repeater1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.LinkButton LinkButton2;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Label1.Text="0";
TextBox2.Text="5";
Build_List();
}
}
private void Build_List()
{
try
{
int ItemOfPage=int.Parse(TextBox2.Text);
string dsn = "uid=sa;pwd=123456;server=(local);database=Northwind";
SqlConnection con = new SqlConnection(dsn);
con.Open();
string strsql="select * from Products";
SqlDataAdapter da = new SqlDataAdapter(strsql,con);
DataSet ds = new DataSet();
int index = int.Parse(Label1.Text);
da.Fill(ds,index,ItemOfPage,"Products");
DataList1.DataSource=ds.Tables["Products"].DefaultView;
DataList1.DataBind();
Repeater1.DataSource=ds.Tables["Products"].DefaultView;
Repeater1.DataBind();
con.Close();

}
catch(Exception ex)
{
Response.Write(ex.Message+" "+ex.StackTrace);
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.LinkButton2.Click += new System.EventHandler(this.LinkButton2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void LinkButton1_Click(object sender, System.EventArgs e)
{
int index = int.Parse(Label1.Text);
index-=int.Parse(TextBox2.Text);
index=index<0?0:index;
Label1.Text= index.ToString();
Build_List();
}

private void LinkButton2_Click(object sender, System.EventArgs e)
{
int index = int.Parse(Label1.Text);
index+=int.Parse(TextBox2.Text);
Label1.Text= index.ToString();
Build_List();

}


}
}