最近比较忙,今天总算有时间写点什么了。
下午没什么活的时候,把前些天没写完的关于DataList分页的功能全都实现了(费了好大力气),该实现包括了非常简单的首页、末页、前一页和后一页,还包括通过DropDownList分页,最后还实现了最麻烦的数字按钮分页效果——也就是baidu的分页效果(这个真的很麻烦,虽然方法有点菜,但再怎么说是做出来了,顶自己一下吧)。
现在把代码帖出来供大家研究:
aspx页面代码——很简单没什么说的:
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text='<%#Eval("name") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblPassword" runat="server" Text='<%#Eval("password") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<br />
<asp:Label ID="lblCount" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblPageCount" runat="server" Text="Label"></asp:Label>
<asp:LinkButton ID="lbtnFirst" runat="server" OnClick="lbtnFirst_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lbtnPre" runat="server" OnClick="lbtnPre_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">2</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">3</asp:LinkButton>
<asp:LinkButton ID="lbtnNext" runat="server" OnClick="lbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbtnLast" runat="server" OnClick="lbtnLast_Click">末页</asp:LinkButton>
<asp:DropDownList ID="ddl3" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl3_SelectedIndexChanged">
</asp:DropDownList>
cs里的代码——虽然代码很多,但是也没有什么难的:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["currentpage"] = 0;
BindDataToDataList();
BindDataToDDL3();
}
}
private int Page
{
get { return Convert.ToInt32(ViewState["currentpage"].ToString()); }
set { ViewState["currentpage"] = value; }
}
private void BindDataToDDL3()
{
ddl3.Items.Clear();
if (Convert.ToInt32(lblPageCount.Text) > 0)
{
for (int i = 1; i <= Convert.ToInt32(lblPageCount.Text); i++)
{
ddl3.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddl3.SelectedValue = "1";
}
}
private void BindDataToDataList()
{
string strcon = "Data Source=.;Initial Catalog=Login;Integrated Security=True";
SqlConnection conn = new SqlConnection(strcon);
string sql = "select * from info";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
PagedDataSource pds=new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 3;
pds.CurrentPageIndex = Page;
lblCount.Text = pds.DataSourceCount.ToString();
lblPageCount.Text = pds.PageCount.ToString();
LinkButton1.ForeColor = Color.Red;
DataList1.DataSource = pds;
DataList1.DataBind();
conn.Close();
if (pds.IsFirstPage)
{
lbtnFirst.Enabled = false;
lbtnPre.Enabled = false;
}
else
{
lbtnFirst.Enabled = true;
lbtnPre.Enabled = true;
}
if (pds.IsLastPage)
{
lbtnLast.Enabled = false;
lbtnNext.Enabled = false;
}
else
{
lbtnNext.Enabled = true;
lbtnLast.Enabled = true;
}
}
protected void lbtnFirst_Click(object sender, EventArgs e)
{
Page = 0;
BindDataToDataList();
valueToButton();
ShowButton();
ddl3.SelectedValue = (Page + 1).ToString();
}
protected void lbtnPre_Click(object sender, EventArgs e)
{
Page = Page - 1;
BindDataToDataList();
valueToButton();
ShowButton();
ddl3.SelectedValue = (Page + 1).ToString();
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
Page = Page + 1;
BindDataToDataList();
valueToButton();
ShowButton();
ddl3.SelectedValue = (Page + 1).ToString();
}
protected void lbtnLast_Click(object sender, EventArgs e)
{
Page = Convert.ToInt32(lblPageCount.Text) - 1;
BindDataToDataList();
valueToButton();
ShowButton();
ddl3.SelectedValue = (Page + 1).ToString();
}
private void ShowButton()
{
if (Convert.ToInt32(lblPageCount.Text) < 3)
{
switch (Convert.ToInt32(lblPageCount.Text))
{
case 1: LinkButton1.Visible = true; LinkButton2.Visible = false; LinkButton3.Visible = false; break;
case 2: LinkButton1.Visible = true; LinkButton2.Visible = true; LinkButton3.Visible = false; break;
default: LinkButton1.Visible = false; LinkButton2.Visible = false; LinkButton2.Visible = false; break;
}
}
else if (Convert.ToInt32(lblPageCount.Text) >= 3)
{
LinkButton1.Visible = true;
LinkButton2.Visible = true;
LinkButton3.Visible = true;
}
if (Convert.ToInt32(LinkButton3.Text) > Convert.ToInt32(lblPageCount.Text))
{
LinkButton3.Visible = false;
}
if (Convert.ToInt32(LinkButton2.Text) > Convert.ToInt32(lblPageCount.Text))
{
LinkButton2.Visible = false;
}
if (Convert.ToInt32(LinkButton1.Text) < 1)
{
LinkButton1.Visible = false;
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Page = Convert.ToInt32(LinkButton1.Text) - 1;
BindDataToDataList();
valueToButton();
ShowButton();
ddl3.SelectedValue = (Page + 1).ToString();
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
Page = Convert.ToInt32(LinkButton2.Text) - 1;
BindDataToDataList();
ShowButton();
ddl3.SelectedValue = (Page + 1).ToString();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
Page = Convert.ToInt32(LinkButton3.Text) - 1;
BindDataToDataList();
valueToButton();
ShowButton();
ddl3.SelectedValue = (Page + 1).ToString();
}
private void valueToButton()
{
int num1 = Page;
int num2 = Page + 1;
int num3 = Page + 2;
LinkButton1.Text = num1.ToString();
LinkButton2.Text = num2.ToString();
LinkButton3.Text = num3.ToString();
LinkButton1.ForeColor = Color.Blue;
LinkButton2.ForeColor = Color.Red;
}
protected void ddl3_SelectedIndexChanged(object sender, EventArgs e)
{
Page = Convert.ToInt32(ddl3.SelectedValue) - 1;
BindDataToDataList();
valueToButton();
ShowButton();
}
这里唯一注意的是要引用相应的命名空间。
虽然还是菜鸟,但是能实现这样的功能还是很高兴的,这个实现方法是我前几天坐公交车的时候闲着无聊偶然想起来的,看来通勤时间长也是有好处的。