今天要开发一个旅游论坛,在后台显示图片时用到了AspNetPager分页控件和DropDownList控件的综合运用,总的目标就是根据dropdownlist的SelectedValue值是否按条件进行分页,第一次用分页控件感觉很强大,刚好今天发布了7.4版,下载地址为http://www.webdiyer.com/download/show/29
废话不多说,附上代码:
前台显示界面代码(vs2010):
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JDImg.aspx.cs" Inherits="travel.Admin.JDImg" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DataList ID="DataList1" runat="server"
onitemcommand="DataList1_ItemCommand" RepeatColumns="2">
<ItemTemplate>
<table>
<tr>
<td rowspan="4">
<asp:Image ID="ImgID" Width="400px" Height="250px" runat="server" ImageUrl='<%#Eval("f_jdimg","~/jdmsgimg/{0}") %>' />
</td>
<td>
<asp:Label ID="lblImgID" runat="server" Text='<%#Eval("f_jdimgid") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblJDName" runat="server" Text='<%#Eval("t_jdMsg.f_jdName") %>'></asp:Label>
<asp:HiddenField ID="hfJDID" Value='<%#Eval("f_jdid") %>' runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblOther" runat="server" Text='<%#Eval("f_others") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="LBtnDel" CommandArgument='<%#Eval("f_jdimgid") %>'
CommandName="Delete" runat="server">删除</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<div id="DIVanp1" runat="server">
<webdiyer:AspNetPager ID="AspNetPager1" runat="server"
Width="100%" UrlPaging="true" ShowPageIndexBox="Always" PageIndexBoxType="DropDownList"
TextBeforePageIndexBox="转到页: " HorizontalAlign="right"
PageSize="12" OnPageChanged="AspNetPager1_PageChanged"
EnableTheming="true">nimenkeyihaohahwanwan
</webdiyer:AspNetPager>
</div>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;
using System.IO;
namespace travel.Admin
{
public partial class JDImg : System.Web.UI.Page
{
public static string value;//记录上次选择的景点ID
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Clear();
DropDownList1.DataSource = jdMsgManager.getAll();//BLL层返回的list数据源
DropDownList1.DataTextField = "f_jdName";
DropDownList1.DataValueField = "f_jdId";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, "==请选择景点==");
if (value != null)//当景点ID不为空时,把上次记录的id赋给dropdownlist的value
{
DropDownList1.SelectedValue = value;
}
DlBind(DropDownList1.SelectedValue);
}
}
private void DlBind(string jdId)
{
if (jdId == "0" || jdId == "==请选择景点==")
{
jdId = "0";
int? pageSize = AspNetPager1.PageSize = 4; //每页条数
int? pageIndex = AspNetPager1.CurrentPageIndex; // 当前页码
int? pageCount = 0;
int? rowCount = 0;
List<t_jdimg> list = jdimgManager.GetJDImgByFY(pageSize, pageIndex, Convert.ToInt32(jdId), ref pageCount, ref rowCount);//BLL层返回的list数据源
AspNetPager1.RecordCount = Convert.ToInt32(pageCount);
DataList1.DataSource = list;
DataList1.DataBind();
}
else
{
int? pageSize = AspNetPager1.PageSize = 4; //每页条数
int? pageIndex = AspNetPager1.CurrentPageIndex; // 当前页码
int? pageCount = 0;
int? rowCount = 0;
List<t_jdimg> list = jdimgManager.GetJDImgByFY(pageSize, pageIndex, Convert.ToInt32(jdId), ref pageCount, ref rowCount);//BLL层返回的list数据源
AspNetPager1.RecordCount = Convert.ToInt32(pageCount);
DataList1.DataSource = list;
DataList1.DataBind();
}
}
public void AspNetPager1_PageChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "0")
DlBind("0");
else
DlBind(DropDownList1.SelectedValue.ToString());
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "0")
{
value = string.Empty;
}
value = DropDownList1.SelectedValue;
DlBind(DropDownList1.SelectedValue);
}
//删除景点图片
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
LinkButton lbtn = e.Item.FindControl("LBtnDel") as LinkButton;
int id = Convert.ToInt32(lbtn.CommandArgument);
string msg = "";
bool b = jdimgManager.DelJDImg(id, ref msg);
if (b)
{
//删除服务器上的景点图片
Image img = e.Item.FindControl("ImgID") as Image;
string imgUrl = img.ImageUrl.ToString();
string path = Server.MapPath(imgUrl);
File.Delete(path);
DlBind(null);
return;
}
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + msg + "')</script>");
}
}
}