DataList 控件分页操作

发表于:2007-06-30来源:作者:点击数: 标签:
aspx 源文件 %@ Page language=c# Codebehind=radionj_list.aspx.cs AutoEventWireup=false Inherits=radio.radio.radionj_list % !DOCTYPE HTML PUBLIC -//W3C//D TD HTML 4.0 Transitional//EN HTML HEAD titleNJ列表/title meta name=GENERATOR Content=Mi
aspx 源文件

<%@ Page language="c#" Codebehind="radionj_list.aspx.cs" AutoEventWireup="false" Inherits="radio.radio.radionj_list" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>NJ列表</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">
<LINK href="admin/style/item.css" type="text/css" rel="stylesheet">
<style>
.normal
{
font-weight:normal
}
.bigsize
{
font-weight:bold;
}

<!-- asthlon发表于 05 3-25 -->
</style>

</HEAD>
<body MS_POSITIONING="GridLayout"> <!--<table border="0" cellspacing="0" cellpadding="0">

</table>-->
<form id="Form1" method="post" runat="server">
<div align="center">
<asp:datalist id="info_data" runat="server" ShowFooter="False" GridLines="Both" BorderColor="Control"
RepeatColumns="2" RepeatDirection="Horizontal" HorizontalAlign="Center" ShowHeader="False"
CellPadding="5" BorderWidth="0px" RepeatLayout="Flow">
<ItemStyle HorizontalAlign="Center" Height="120px" Width="140px" VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>
<table cellspacing="0" cellpadding="0">
<tr>
<td><img src="../images/r_7_11.jpg"></td>
<td valign="middle" bgcolor="#feclearcase/" target="_blank" >cc00" align="center"><a href=@#<%# DataBinder.Eval(Container.DataItem,"id","radio_nj_info.aspx?id={0}") %>@#>
<div style="overflow:hidden;width:90px;height:90px;"><img width=90 src=@#<%# Return_Nj_Detail((string)DataBinder.Eval(Container.DataItem,"content"),1)%>@#></div>
</a>
</td>
<Td><img src="../images/r_7_13.jpg"></Td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>
<div style="PADDING-LEFT:40px"><FONT color="#000000">共
<asp:label id="list_num" runat="server"></asp:label>条记录</FONT>
<asp:label id="list_at" runat="server"></asp:label><FONT color="#000000">&nbsp;</FONT><asp:linkbutton id="p0" runat="server" CommandName="p0"><font face="Webdings">9</font></asp:linkbutton>
<asp:linkbutton id="p1" runat="server" CommandName="p1">
<font face="Webdings">7</font></asp:linkbutton>
[<asp:PlaceHolder id="p_pagelist" runat="server"></asp:PlaceHolder>]
<asp:linkbutton id="p2" runat="server" CommandName="p2">
<font face="Webdings">8</font></asp:linkbutton>
<asp:linkbutton id="p3" runat="server" CommandName="p3">
<font face="Webdings">:</font></asp:linkbutton>
<!--
&nbsp;<asp:textbox id="atTxt" onkeyup="this.value=this.value.replace(/\D/g,@#@#)" runat="server" Width="35px"
onafterpaste="this.value=this.value.replace(/\D/g,@#@#)"></asp:textbox><asp:button id="Go_TO" runat="server" Text="GO"></asp:button>
-->
</div>
</div>
</form>
</body>
</HTML>


.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;

namespace radio.radio
{
/// <summary>
/// asthlon发表于 05 3-25。
/// </summary>
public class radionj_list : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList info_data;
protected System.Web.UI.WebControls.Label list_num;
protected System.Web.UI.WebControls.Label list_at;
protected System.Web.UI.WebControls.LinkButton p0;
protected System.Web.UI.WebControls.LinkButton p1;
protected System.Web.UI.WebControls.LinkButton p2;
protected System.Web.UI.WebControls.PlaceHolder p_pagelist;
protected System.Web.UI.WebControls.LinkButton p3;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
WebUse.SqlEC.DataBase dItem = new WebUse.SqlEC.DataBase();
string sql = "select count(id) from radio_dj";
ViewState["curpage"] = 0;
ViewState["pcount"] = dItem.StrSql_TxtSql(sql);
Bind_List();
Post_Control();
}
else
{
Post_Control();
}
// 在此处放置用户代码以初始化页面
}
private void Bind_List()
{
int Psize = 10;
int sIndex,len,at;
float f;
WebUse.SqlEC.DataBase dItem = new WebUse.SqlEC.DataBase();
len = Convert.ToInt32(Math.Ceiling(Convert.ToInt32(ViewState["pcount"]) / Psize));//+1
f = Convert.ToSingle(ViewState["pcount"]) / Psize ;
if(f > Convert.ToInt32(Convert.ToInt32(ViewState["pcount"]) / Psize))
{
len = len + 1;
}


sIndex = Convert.ToInt32(ViewState["curpage"]) * Psize;
SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter();
DataSet ds = new DataSet();
adp = dItem.Read_Adapter_NoArg("radio_dj_sel");//dItem.Read_Adapter_TxtSql(sql);
adp.Fill(ds,sIndex,Psize,"show");
info_data.DataSource = ds;
info_data.DataBind();

at = Convert.ToInt32(ViewState["curpage"]);
if(at <=0)
{
p0.Enabled = false;
p1.Enabled = false;
}
else
{
p1.Enabled = true;
p0.Enabled = true;
}
if(at >= len-1)
{
p2.Enabled = false;
p3.Enabled = false;
}
else
{
p2.Enabled = true;
p3.Enabled = true;
}
list_num.Text = Convert.ToString(ViewState["pcount"]);
list_at.Text = Convert.ToString(sIndex / Psize + 1) + "/" + len.ToString() ;
Clear_LinkButton_Style();
}
private void go_page(object sender, System.Web.UI.WebControls.CommandEventArgs e)
{
string cmdName;
int sIndex = Convert.ToInt32(ViewState["curpage"]);
int len = Convert.ToInt32(Math.Ceiling(Convert.ToInt32(ViewState["pcount"]) / 10));
cmdName = e.CommandName;
switch(cmdName)
{
case "p0":
sIndex = 0;
break;
case "p1":
sIndex -= 1;
break;
case "p2":
sIndex +=1;
break;
case "p3":
sIndex = len;
break;
}
ViewState["curpage"] = sIndex;
Bind_List();
}

private void Post_Control()
{
int i;
int len = Convert.ToInt32(Math.Ceiling(Convert.ToInt32(ViewState["pcount"]) / 10));
float f = Convert.ToSingle(ViewState["pcount"]) / 10 ;
if(f > Convert.ToInt32(Convert.ToInt32(ViewState["pcount"]) / 10))
{
len = len + 1;
}
this.p_pagelist.Controls.Clear();
string s = "";
//int curpage = Convert.ToInt32(ViewState["curpage"]);
for(i=1;i<=len;i++)
{
System.Web.UI.WebControls.LinkButton lkbtn = new LinkButton();
s = " " + i.ToString() + " ";
lkbtn.Text = s;
lkbtn.CommandArgument = i.ToString();
this.p_pagelist.Controls.Add(lkbtn);
lkbtn.Command += new CommandEventHandler(lkbtn_Command);
}
}
private void Clear_LinkButton_Style()
{
int curpage = Convert.ToInt32(ViewState["curpage"])+1;
foreach(Control ictrl in this.p_pagelist.Controls)
{
if(ictrl.GetType().ToString().ToUpper().IndexOf("LINKBUTTON")>0)
{
LinkButton ilkbtn = (LinkButton)ictrl;
if(ilkbtn.CommandArgument.ToString() == curpage.ToString())
{
ilkbtn.CssClass = "bigsize";
}
else
{
ilkbtn.CssClass ="normal";
}
}
}
}
private void lkbtn_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
{
int gos,sIndex;
int len = Convert.ToInt32(Math.Ceiling(Convert.ToInt32(ViewState["pcount"]) / 10));
if(e.CommandArgument == null)
{
return;
}
sIndex = 0;
gos = Convert.ToInt32(e.CommandArgument);
//Clear_LinkButton_Style();
//LinkButton lkbtn = (LinkButton)sender;//
//lkbtn.CssClass = "bigsize";
if(gos<=0)
{
sIndex = 0;
}
else
{
if(gos>= len + 1)
{
sIndex = len;
}
else
{
sIndex = gos-1;
}

}
ViewState["curpage"] = sIndex;
Bind_List();
}
/**
private void Go_TO_Click(object sender, System.EventArgs e)
{
int gos,sIndex;
int len = Convert.ToInt32(Math.Ceiling(Convert.ToInt32(ViewState["pcount"]) / 9));
if(atTxt.Text == "")
{
return;
}
sIndex = 0;
gos = Convert.ToInt32(atTxt.Text);
if(gos<=0)
{
sIndex = 0;
}
else
{
if(gos>= len + 1)
{
sIndex = len;
}
else
{
sIndex = gos-1;
}

}
ViewState["curpage"] = sIndex;
Bind_List();
}
**/
/// <summary>
/// 取得NJ娱乐信息
/// </summary>
/// <param name="t"></param>
/// <param name="i"></param>
/// <returns></returns>
public string Return_Nj_Detail(string t,int i)
{
string rstr=null;
string[] istr = t.Split(@#,@#);
rstr = istr[i];
return rstr;

}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.p0.Command += new System.Web.UI.WebControls.CommandEventHandler(this.go_page);
this.p1.Command += new System.Web.UI.WebControls.CommandEventHandler(this.go_page);
this.p2.Command += new System.Web.UI.WebControls.CommandEventHandler(this.go_page);
this.p3.Command += new System.Web.UI.WebControls.CommandEventHandler(this.go_page);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}



原文转自:http://www.ltesting.net