Html code:
<Form id = "form1" runat = "server">
<Div>
<Asp: DataList ID = "DataList1" runat = "server">
<HeaderTemplate>
<Table cellpadding = "0" cellspacing = "0" class = "style1">
<Tr>
<Td style = "width: 20%">
IP </td>
<Td style = "width: 20%">
Logon Time </td>
<Td style = "width: 20%">
Departure time </td>
<Td style = "width: 20%">
Browser </td>
<Td style = "width: 20%">
Operating System </td>
</Tr>
</Table>
</HeaderTemplate>
<ItemTemplate>
<Table cellpadding = "0" cellspacing = "0" class = "style1">
<Tr>
<Td style = "width: 20%">
<% # DataBinder. Eval (Container. DataItem, "IP") %> </td>
<Td style = "width: 20%">
<% # DataBinder. Eval (Container. DataItem, "LoginTime") %> </td>
<Td style = "width: 20%">
<% # DataBinder. Eval (Container. DataItem, "LeaveTime") %> </td>
<Td style = "width: 20%">
<% # DataBinder. Eval (Container. DataItem, "Browser") %> </td>
<Td style = "width: 20%">
<% # DataBinder. Eval (Container. DataItem, "OS") %> </td>
</Tr>
</Table>
</ItemTemplate>
</Asp: DataList>
</Div>
Total <asp: Label ID = "labcount" runat = "server" Text = ""> </asp: Label>
& Nbsp; Page & nbsp; Current
<Asp: Label ID = "labnowpage" runat = "server" Text = "1"> </asp: Label>
Page & nbsp;
<Asp: LinkButton ID = "lkbtnfirst" runat = "server" onclick = "lkbtnfirst_Click"> homepage </asp: LinkButton>
& Nbsp;
<Asp: LinkButton ID = "lkbtnfront" runat = "server" onclick = "lkbtnfront_Click"> previous page </asp: LinkButton>
& Nbsp;
<Asp: LinkButton ID = "lkbtnnext" runat = "server" onclick = "lkbtnnext_Click"> next page </asp: LinkButton>
& Nbsp;
<Asp: LinkButton ID = "lkbtnlast" runat = "server" onclick = "lkbtnlast_Click"> last page </asp: LinkButton>
</Form>
Datalist paging is implemented through the PagedDataSource class. This class encapsulates the data binding control's paging-related attributes to allow the control to perform paging operations.
Background code
Using System. data. sqlclient;
DB db = new DB ();
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Bind ();
}
}
Public void bind ()
{
Int currentpage = Convert. ToInt32 (labnowpage. Text );
PagedDataSource ps = new PagedDataSource (); // generate an instance of pageddatasource
String selstr = "select * from tb_CounterInfo ";
String slqcon = ConfigurationManager. receivettings ["getcon"]. ToString ();
SqlConnection con = new SqlConnection (slqcon );
Con. Open ();
SqlDataAdapter da = new SqlDataAdapter (selstr, con );
DataSet ds = new DataSet ();
Da. Fill (ds, "CounterInfo ");
Ps. DataSource = ds. Tables ["CounterInfo"]. DefaultView;
Ps. AllowPaging = true;
Ps. PageSize = 4;
Ps. CurrentPageIndex = currentpage-1;
Lkbtnfirst. Enabled = true;
Lkbtnfront. Enabled = true;
Lkbtnnext. Enabled = true;
Lkbtnlast. Enabled = true;
If (currentpage = 1)
{
Lkbtnlast. Enabled = false;
Lkbtnfront. Enabled = false;
}
If (currentpage = ps. PageCount)
{
Lkbtnnext. Enabled = false;
Lkbtnlast. Enabled = false;
}
This. labcount. Text = Convert. ToString (ps. PageCount );
This. DataList1.DataSource = ps;
This. DataList1.DataKeyField = "ID ";
This. DataList1.DataBind ();
}
Protected void lkbtnfirst_Click (object sender, EventArgs e)
{
This. labnowpage. Text = "1 ";
Bind ();
}
Protected void lkbtnfront_Click (object sender, EventArgs e)
{
This. labnowpage. Text = Convert. ToString (Convert. ToInt32 (this. labnowpage. Text)-1 );
Bind ();
}
Protected void lkbtnnext_Click (object sender, EventArgs e)
{
This. labnowpage. Text = Convert. ToString (Convert. ToInt32 (this. labnowpage. Text) + 1 );
Bind ();
}
Protected void lkbtnlast_Click (object sender, EventArgs e)
{
This. labnowpage. Text = this. labcount. Text;
Bind ();
}
The following two methods are generally used to obtain control data in the DataList control:
One is to directly access the control in Item through the e. Item. Controls [0] index, and the other is to use the FindControl () method for searching.