首页 > 代码库 > 步步为营-73-asp.net的简单练习(根据美工提供静态页面,编写后台代码)
步步为营-73-asp.net的简单练习(根据美工提供静态页面,编写后台代码)
说明:实际企业中开发分工是很明确,往往程序员根据美工提供的UI界面进行后台代码的编写.
1.1 原始HTML页面
1.2 使用aspx进行修改
这里使用到了三层架构
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; namespace NewsCommon{ public static class SqlHelper { //01-连接字符串 #region 01连接字符串 private static string connStr = ConfigurationManager.ConnectionStrings["sqlStr"].ConnectionString; #endregion //增 改 删 public static int ExecuteNonQuery(string sql, params SqlParameter[] ps) { using (SqlConnection conn = new SqlConnection(connStr)) { SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddRange(ps); conn.Open(); return cmd.ExecuteNonQuery(); } } //查询标量值 首行首列 public static object ExecuteScalar(string sql, params SqlParameter[] ps) { using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddRange(ps); return (object)cmd.ExecuteScalar(); } } //查询 集合值 public static DataTable ExecuteTable(string sql, params SqlParameter[] ps) { using (SqlConnection conn = new SqlConnection (connStr)) { SqlDataAdapter sda = new SqlDataAdapter(sql,conn); sda.SelectCommand.Parameters.AddRange(ps); DataTable dt = new DataTable(); sda.Fill(dt); return dt; } } } }
using NewsCommon; using NewsModel; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NewsDAL { public class NewsInfoDal { public NewsInfoDal() { } public List<NewsInfoModel> GetNewsInfo(NewsInfoModel model) { List<NewsInfoModel> newsInfoList = new List<NewsInfoModel> (); string sql = "select * from NewsInfo"; DataTable dt = SqlHelper.ExecuteTable(sql); int count = dt.Rows.Count; if (count>0) { foreach (DataRow dr in dt.Rows) { NewsInfoModel newsInfo = new NewsInfoModel(); newsInfo.NewsId = Convert.ToInt32(dr["NewsId"].ToString()); newsInfo.NewsTitle = dr["NewsTitle"].ToString(); newsInfo.NewsContent = dr["NewsContent"].ToString(); newsInfo.SubBy = dr["SubBy"].ToString(); newsInfo.SubTime =Convert.ToDateTime(dr["SubTime"].ToString()); newsInfo.TypeId = Convert.ToInt32(dr["TypeId"].ToString()); newsInfoList.Add(newsInfo); } } return newsInfoList; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using NewsDAL; using NewsModel; namespace NewsBLL { public partial class NewsInfoBll { NewsInfoDal dal = new NewsInfoDal(); public List<NewsInfoModel> GetNewsInfo(NewsInfoModel model) { return dal.GetNewsInfo(model); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NewsModel { //NewsInfo public partial class NewsInfoModel { /// <summary> /// NewsId /// </summary> private int _newsid; public int NewsId { get { return _newsid; } set { _newsid = value; } } /// <summary> /// NewsTitle /// </summary> private string _newstitle; public string NewsTitle { get { return _newstitle; } set { _newstitle = value; } } /// <summary> /// NewsContent /// </summary> private string _newscontent; public string NewsContent { get { return _newscontent; } set { _newscontent = value; } } /// <summary> /// SubTime /// </summary> private DateTime _subtime; public DateTime SubTime { get { return _subtime; } set { _subtime = value; } } /// <summary> /// SubBy /// </summary> private string _subby; public string SubBy { get { return _subby; } set { _subby = value; } } /// <summary> /// TypeId /// </summary> private int _typeid; public int TypeId { get { return _typeid; } set { _typeid = value; } } } }
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <connectionStrings> <!--Data Source=127.0.0.1;uId=sa;pwd=sa; Initial Catalog=MVCDB;--> <add name="sqlStr" connectionString="server=.;database=News;uid=sa;pwd=sa;"/> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> </configuration>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01新闻列表.aspx.cs" Inherits="_02_根据UI提供的页面编写后台._01_新闻管理._01新闻列表" %> <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <title>新闻中心--北京华科世佳软件开发有限公司</title> <meta name="robots" content="index, follow" /> <meta name="author" content="北京多维网讯科技有限公司" /> <meta name="keywords" content="软件开发、房地产业管理类软件、新建商品房网上备案系统、存量房网上备案系统、统计与发布系统、项目管理系统、从业主题管理系统、产权产籍管理系统、测绘成果及管理系统"/> <meta name="description" content="北京华科世佳软件开发有限公司地处国家计算机应用软件研发腹地——北京市海淀区上地信息产业基地,具有明显的人才优势、技术优势和环境优势。我公司在2004年12月通过了北京市科委的软件企业认证和北京市软件行业协会软件产品的认定。" /> <link href=http://www.mamicode.com/"../style/style.css" type="text/css" rel="stylesheet" /> <link rel="icon" href=http://www.mamicode.com/"../favicon.ico" type="image/x-icon" /> </head> <body class="body_column"> <div id="wrap_column"> <!----------------------------------begin header_column----------------------------------> <div id="header_column" class="header_news"><!-- #BeginLibraryItem "/Library/header.lbi" --><h1 class="logo_column"><a href=http://www.mamicode.com/"../index.html"><img src=http://www.mamicode.com/"../images/logo.png" width="179" height="55" alt="北京华科世佳软件开发有限公司" /></a></h1> <!-- #EndLibraryItem --> <div class="nav_column"> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="754" height="37" id="FlashID" title="北京华科世佳软件开发有限公司"> <param name="movie" value=http://www.mamicode.com/"../flash/sub_fla.swf" /> <param name="quality" value=http://www.mamicode.com/"high" /> <param name="wmode" value=http://www.mamicode.com/"transparent" /> <param name="swfversion" value=http://www.mamicode.com/"8.0.35.0" /> <!-- 此 param 标签提示使用 Flash Player 6.0 r65 和更高版本的用户下载最新版本的 Flash Player。如果您不想让用户看到该提示,请将其删除。 --> <param name="expressinstall" value=http://www.mamicode.com/"../Script/expressInstall.swf" /> <!-- 下一个对象标签用于非 IE 浏览器。所以使用 IECC 将其从 IE 隐藏。 --> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data=http://www.mamicode.com/"../flash/sub_fla.swf" width="754" height="37"> <!--<![endif]--> <param name="quality" value=http://www.mamicode.com/"high" /> <param name="wmode" value=http://www.mamicode.com/"transparent" /> <param name="swfversion" value=http://www.mamicode.com/"8.0.35.0" /> <param name="expressinstall" value=http://www.mamicode.com/"../Script/expressInstall.swf" /> <!-- 浏览器将以下替代内容显示给使用 Flash Player 6.0 和更低版本的用户。 --> <div> <h4>此页面上的内容需要较新版本的 Adobe Flash Player。</h4> <p><a href=http://www.mamicode.com/"http://www.adobe.com/go/getflashplayer"><img src=http://www.mamicode.com/"http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="获取 Adobe Flash Player" width="112" height="33" /></a></p> </div> <!--[if !IE]>--> </object> <!--<![endif]--> </object> </div> </div> <!----------------------------------end header_column----------------------------------> <!----------------------------------begin main_column----------------------------------> <div id="main_column"> <div id="sidebar_column"> <h2 class="title_news">新闻中心</h2><!-- #BeginLibraryItem "/Library/menu_prd.lbi" --> <dl class="menu_prd"> <dt>软件产品</dt> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/NewHouse.html">新建商品房网上备案系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/StockHouse.html">存量房网上备案系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Statistics.html">统计与发布系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/ProjectManagement.html">项目管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Practitioners.html">从业主题管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Property.html">产权产籍管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Mapping.html">测绘成果及管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/MaintenanceFunds.html">维修资金管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/HousingSecurity.html">住房保障管理体系</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Transaction.html">房产交易资金监管系统</a></dd> </dl> <a href=http://www.mamicode.com/"../Contact/Contact.html" class="contct"><img src=http://www.mamicode.com/"../images/img_contact_sidebarcolumn.png" width="160" height="52" alt="联系我们" /></a><!-- #EndLibraryItem --></div> <div id="content_column"> <div class="position"><a class="graylink" href=http://www.mamicode.com/"../index.html">首页</a> < <span class="color0range">新闻中心</span></div> <div class="cont"> <ul class="list_news"> <%-- <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">新政百日后山西临汾房地产业情况调查</a></li> <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">上半年全区地税收入再创历史新高</a></li> <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">中国住宅建设的地产文化之路 海尔地产高峰论坛举行</a></li> <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">我国城镇化与房地产业的发展方向</a></li> <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">秋风专栏:不能给中房协影响政府决策的机会 </a></li> <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">上半年投资形势分析及全年展望</a></li> <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">甘肃省工商联房地产业商会行业合作联盟章程</a></li> <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">诸多不可抗力来袭 房地产业面临双重考验</a></li> <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">通许县地税局 加强房地产业税收管理</a></li> <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">构建特色鲜明的海南房地产业 完善供应体系</a></li> <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">新疆吐鲁番地区小税种实现大突破</a></li> <li><span>2010-08-18</span><a href=http://www.mamicode.com/"Shownews.html" target="_blank">顺德目前正在进行简政强镇事权改革将在9月底前完成</a></li>--%> <%=NewsHTML %>> </ul> <div class="pages">首页 | 前页 | 后页 | 尾页 页次:1/3页 </div> </div> </div> <div class="clear"></div> </div> <!----------------------------------end main_column----------------------------------> <!----------------------------------begin footer_column----------------------------------><!-- #BeginLibraryItem "/Library/fooer.lbi" --> <div id="footer_column"> <span class="copyright"></span><span class="frdlink">友情链接:<select name=""> <option>公司或合作伙伴网站</option> </select></span> </div><!-- #EndLibraryItem --><!----------------------------------end footer_column----------------------------------> </div> </body> </html> <html><script language="JavaScript"> </script></html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using NewsModel; using NewsBLL; using System.Text; namespace _02_根据UI提供的页面编写后台._01_新闻管理 { public partial class _01新闻列表 : System.Web.UI.Page { public NewsInfoBll bll = new NewsInfoBll(); public string NewsHTML { get; set; } protected void Page_Load(object sender, EventArgs e) { NewsInfoModel newsInfo = new NewsInfoModel(); List<NewsInfoModel> newsInfoList = bll.GetNewsInfo(newsInfo); StringBuilder sb = new StringBuilder(); if (newsInfoList.Count > 0) { foreach (NewsInfoModel item in newsInfoList) { string aLI = string.Format(@" <li><span>{0}</span><a href=http://www.mamicode.com/‘Shownews.html‘ target=‘_blank‘>{1}", item.SubTime, item.NewsTitle); sb.Append(aLI); } } NewsHTML = sb.ToString(); } } }
运行效果
1.3 此时存在一个问题,即列表内容太多,既影响美观又占用内存.最好将其进行分页处理
using NewsCommon; using NewsModel; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NewsDAL { public class NewsInfoDal { public NewsInfoDal() { } #region 01 获取分页信息 public List<NewsInfoModel> GetNewsInfoPage(int start, int end) { List<NewsInfoModel> newsInfoList = new List<NewsInfoModel> (); //01-创建SQL文 注意此时的sql语句需要用到row_number()函数 string sql = @"select * from (select * ,ROW_NUMBER() over(order by NewsId) as num from NewsInfo ) Temp where Temp.num between @start and @end"; //02-创建参数 SqlParameter[] ps = { new SqlParameter("@start",SqlDbType.Int), new SqlParameter("@end",SqlDbType.Int) }; ps[0].Value =http://www.mamicode.com/ start; ps[1].Value =http://www.mamicode.com/ end; DataTable dt = SqlHelper.ExecuteTable(sql,ps); int count = dt.Rows.Count; if (count>0) { foreach (DataRow dr in dt.Rows) { NewsInfoModel newsInfo = new NewsInfoModel(); newsInfo.NewsId = Convert.ToInt32(dr["NewsId"].ToString()); newsInfo.NewsTitle = dr["NewsTitle"].ToString(); newsInfo.NewsContent = dr["NewsContent"].ToString(); newsInfo.SubBy = dr["SubBy"].ToString(); newsInfo.SubTime =Convert.ToDateTime(dr["SubTime"].ToString()); newsInfo.TypeId = Convert.ToInt32(dr["TypeId"].ToString()); newsInfoList.Add(newsInfo); } } return newsInfoList; } #endregion #region 02 获取总的记录数 public int GetNewsInfoCount() { string sql = "select count(1) from NewsInfo"; int count = (int)SqlHelper.ExecuteScalar(sql); if (count < 1) { count = 0; } return count; } #endregion } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using NewsDAL; using NewsModel; namespace NewsBLL { public partial class NewsInfoBll { NewsInfoDal dal = new NewsInfoDal(); #region 01 获取页面信息 public List<NewsInfoModel> GetNewsInfoPage(int pageIndex, int pageSize) { //根据当前页码值 和 显示记录数进行计算 int start = (pageIndex - 1) * pageSize + 1; int end = pageIndex * pageSize; return dal.GetNewsInfoPage(start, end); } #endregion #region 02 获取总的页数 public int GetNewsInfoPageCount(int pageSize) { //01 获取总的记录数 int count = dal.GetNewsInfoCount(); //02 获取总的页数 double pageCount = Math.Ceiling(Convert.ToDouble(count / pageSize)); return Convert.ToInt32(pageCount); } #endregion } }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01新闻列表.aspx.cs" Inherits="_02_根据UI提供的页面编写后台._01_新闻管理._01新闻列表" %> <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <title>新闻中心--北京华科世佳软件开发有限公司</title> <meta name="robots" content="index, follow" /> <meta name="author" content="北京多维网讯科技有限公司" /> <meta name="keywords" content="软件开发、房地产业管理类软件、新建商品房网上备案系统、存量房网上备案系统、统计与发布系统、项目管理系统、从业主题管理系统、产权产籍管理系统、测绘成果及管理系统"/> <meta name="description" content="北京华科世佳软件开发有限公司地处国家计算机应用软件研发腹地——北京市海淀区上地信息产业基地,具有明显的人才优势、技术优势和环境优势。我公司在2004年12月通过了北京市科委的软件企业认证和北京市软件行业协会软件产品的认定。" /> <link href=http://www.mamicode.com/"../style/style.css" type="text/css" rel="stylesheet" /> <link rel="icon" href=http://www.mamicode.com/"../favicon.ico" type="image/x-icon" /> </head> <body class="body_column"> <div id="wrap_column"> <!----------------------------------begin header_column----------------------------------> <div id="header_column" class="header_news"><!-- #BeginLibraryItem "/Library/header.lbi" --><h1 class="logo_column"><a href=http://www.mamicode.com/"../index.html"><img src=http://www.mamicode.com/"../images/logo.png" width="179" height="55" alt="北京华科世佳软件开发有限公司" /></a></h1> <!-- #EndLibraryItem --> <div class="nav_column"> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="754" height="37" id="FlashID" title="北京华科世佳软件开发有限公司"> <param name="movie" value=http://www.mamicode.com/"../flash/sub_fla.swf" /> <param name="quality" value=http://www.mamicode.com/"high" /> <param name="wmode" value=http://www.mamicode.com/"transparent" /> <param name="swfversion" value=http://www.mamicode.com/"8.0.35.0" /> <!-- 此 param 标签提示使用 Flash Player 6.0 r65 和更高版本的用户下载最新版本的 Flash Player。如果您不想让用户看到该提示,请将其删除。 --> <param name="expressinstall" value=http://www.mamicode.com/"../Script/expressInstall.swf" /> <!-- 下一个对象标签用于非 IE 浏览器。所以使用 IECC 将其从 IE 隐藏。 --> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data=http://www.mamicode.com/"../flash/sub_fla.swf" width="754" height="37"> <!--<![endif]--> <param name="quality" value=http://www.mamicode.com/"high" /> <param name="wmode" value=http://www.mamicode.com/"transparent" /> <param name="swfversion" value=http://www.mamicode.com/"8.0.35.0" /> <param name="expressinstall" value=http://www.mamicode.com/"../Script/expressInstall.swf" /> <!-- 浏览器将以下替代内容显示给使用 Flash Player 6.0 和更低版本的用户。 --> <div> <h4>此页面上的内容需要较新版本的 Adobe Flash Player。</h4> <p><a href=http://www.mamicode.com/"http://www.adobe.com/go/getflashplayer"><img src=http://www.mamicode.com/"http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="获取 Adobe Flash Player" width="112" height="33" /></a></p> </div> <!--[if !IE]>--> </object> <!--<![endif]--> </object> </div> </div> <!----------------------------------end header_column----------------------------------> <!----------------------------------begin main_column----------------------------------> <div id="main_column"> <div id="sidebar_column"> <h2 class="title_news">新闻中心</h2><!-- #BeginLibraryItem "/Library/menu_prd.lbi" --> <dl class="menu_prd"> <dt>软件产品</dt> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/NewHouse.html">新建商品房网上备案系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/StockHouse.html">存量房网上备案系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Statistics.html">统计与发布系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/ProjectManagement.html">项目管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Practitioners.html">从业主题管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Property.html">产权产籍管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Mapping.html">测绘成果及管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/MaintenanceFunds.html">维修资金管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/HousingSecurity.html">住房保障管理体系</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Transaction.html">房产交易资金监管系统</a></dd> </dl> <a href=http://www.mamicode.com/"../Contact/Contact.html" class="contct"><img src=http://www.mamicode.com/"../images/img_contact_sidebarcolumn.png" width="160" height="52" alt="联系我们" /></a><!-- #EndLibraryItem --></div> <div id="content_column"> <div class="position"><a class="graylink" href=http://www.mamicode.com/"../index.html">首页</a> < <span class="color0range">新闻中心</span></div> <div class="cont"> <ul class="list_news"> <%=NewsHTML %>> </ul> <div class="pages"> <a href=http://www.mamicode.com/"01新闻列表.aspx?pageIndex=1">首页</a> | <a href=http://www.mamicode.com/"01新闻列表.aspx?pageIndex=<%=PageCurrent-1<1?1:PageCurrent-1 %>">前页</a> | <a href=http://www.mamicode.com/"01新闻列表.aspx?pageIndex=<%=PageCurrent+1 >= PageCount?PageCount:PageCurrent+1 %>">后页</a> | <a href=http://www.mamicode.com/"01新闻列表.aspx?pageIndex=<%=PageCount %>">尾页</a> 页次:<%=PageCurrent %>/<%=PageCount %>页 </div> </div> </div> <div class="clear"></div> </div> <!----------------------------------end main_column----------------------------------> <!----------------------------------begin footer_column----------------------------------><!-- #BeginLibraryItem "/Library/fooer.lbi" --> <div id="footer_column"> <span class="copyright"></span><span class="frdlink">友情链接:<select name=""> <option>公司或合作伙伴网站</option> </select></span> </div><!-- #EndLibraryItem --><!----------------------------------end footer_column----------------------------------> </div> </body> </html> <html><script language="JavaScript"> </script></html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using NewsModel; using NewsBLL; using System.Text; namespace _02_根据UI提供的页面编写后台._01_新闻管理 { public partial class _01新闻列表 : System.Web.UI.Page { //01 设置bll对象 public NewsInfoBll bll = new NewsInfoBll(); //02 设置新闻信息的HTML代码 public string NewsHTML { get; set; } //03 设置总页数和当前页数 public int PageCount { get; set; } public int PageCurrent { get; set; } protected void Page_Load(object sender, EventArgs e) { //01 设置显示页数 int pageSize = 5; //02 设置当前页数 int pageIndex; if (string.IsNullOrEmpty(Context.Request["pageIndex"]) || !int.TryParse(Context.Request["pageIndex"].ToString(), out pageIndex)) { pageIndex = 1; } //03 获取总的页数 int pageCount = bll.GetNewsInfoPageCount(pageSize); //04 判断当前页数和总页数的关系 pageIndex = pageIndex < 1 ? 1 : pageIndex; pageIndex = pageIndex > pageCount ? pageCount : pageIndex; List<NewsInfoModel> newsInfoList = bll.GetNewsInfoPage(pageIndex, pageSize); StringBuilder sb = new StringBuilder(); if (newsInfoList.Count > 0) { foreach (NewsInfoModel item in newsInfoList) { string aLI = string.Format(@" <li><span>{0}</span><a href=http://www.mamicode.com/‘Shownews.html‘ target=‘_blank‘>{1}", item.SubTime, item.NewsTitle); sb.Append(aLI); } } NewsHTML = sb.ToString(); PageCount = pageCount; PageCurrent = pageIndex; } } }
1.4 还有一种常见的分页以数字格式显示
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NewsCommon { public partial class PageBarHelper { /// <summary> /// 获取底部数字页码显示条 /// </summary> /// <param name="pageIndex">当前页码值</param> /// <param name="pageCount">总的页码值</param> /// <param name="pageShowCount">需要显示上下几页页码</param> /// <returns></returns> public static string GetPageBar(int pageIndex, int pageCount, int pageShowCount) { string pageBarHtml; //假设每次显示10页数据.pageShowCount=10,一共有18页数据 int start=0, end=0;//循环开始页,循环结束页 if (pageIndex <= pageShowCount / 2 && pageCount >= pageShowCount) { //当前页码in( 1 2 3) ==== 1 2 3 4 5 6 7 8 9 10 start = 1; end = start + pageShowCount; } else if (pageIndex <= pageShowCount / 2 && pageCount < pageShowCount) { //当前页码in( 1 2 3) ==== 1 2 3 4 5 start = 1; end = pageCount+1; } else if (pageIndex > pageShowCount / 2 && pageIndex + pageShowCount / 2 <= pageCount) { //当前页码in(8) ==== 3 4 5 6 7 8 9 10 11 12 start = pageIndex - pageShowCount / 2; end = start + pageShowCount; } else if (pageIndex > pageShowCount / 2 && pageIndex + pageShowCount / 2 > pageCount) { //当前页码in(16) ==== 9 10 11 12 13 14 15 16 17 18 start = pageCount - pageShowCount>0?pageCount - pageShowCount+1:1; end = pageCount+1; } StringBuilder sb = new StringBuilder(); if (pageIndex != 1 ) { sb.AppendFormat(@"<a href=http://www.mamicode.com/‘01新闻列表.aspx?pageIndex={0}‘>上一页", pageIndex - 1); } //循环遍历 for (int i = start; i < end; i++) { if (i == pageIndex) { sb.AppendFormat(@"<a href=http://www.mamicode.com/‘#‘>{0}", i); } else { sb.AppendFormat(@"<a href=http://www.mamicode.com/‘01新闻列表.aspx?pageIndex={0}‘>{0}", i); } } if (pageIndex != pageCount) { sb.AppendFormat(@"<a href=http://www.mamicode.com/‘01新闻列表.aspx?pageIndex={0}‘>下一页", pageIndex + 1); } pageBarHtml = sb.ToString(); return pageBarHtml; } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01新闻列表.aspx.cs" Inherits="_02_根据UI提供的页面编写后台._01_新闻管理._01新闻列表" %> <%@ Import Namespace="NewsCommon" %> <!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <title>新闻中心--北京华科世佳软件开发有限公司</title> <meta name="robots" content="index, follow" /> <meta name="author" content="北京多维网讯科技有限公司" /> <meta name="keywords" content="软件开发、房地产业管理类软件、新建商品房网上备案系统、存量房网上备案系统、统计与发布系统、项目管理系统、从业主题管理系统、产权产籍管理系统、测绘成果及管理系统"/> <meta name="description" content="北京华科世佳软件开发有限公司地处国家计算机应用软件研发腹地——北京市海淀区上地信息产业基地,具有明显的人才优势、技术优势和环境优势。我公司在2004年12月通过了北京市科委的软件企业认证和北京市软件行业协会软件产品的认定。" /> <link href=http://www.mamicode.com/"../style/style.css" type="text/css" rel="stylesheet" /> <link href=http://www.mamicode.com/"../style/PageBarStyle.css" rel="stylesheet" /> <link rel="icon" href=http://www.mamicode.com/"../favicon.ico" type="image/x-icon" /> </head> <body class="body_column"> <div id="wrap_column"> <!----------------------------------begin header_column----------------------------------> <div id="header_column" class="header_news"><!-- #BeginLibraryItem "/Library/header.lbi" --><h1 class="logo_column"><a href=http://www.mamicode.com/"../index.html"><img src=http://www.mamicode.com/"../images/logo.png" width="179" height="55" alt="北京华科世佳软件开发有限公司" /></a></h1> <!-- #EndLibraryItem --> <div class="nav_column"> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="754" height="37" id="FlashID" title="北京华科世佳软件开发有限公司"> <param name="movie" value=http://www.mamicode.com/"../flash/sub_fla.swf" /> <param name="quality" value=http://www.mamicode.com/"high" /> <param name="wmode" value=http://www.mamicode.com/"transparent" /> <param name="swfversion" value=http://www.mamicode.com/"8.0.35.0" /> <!-- 此 param 标签提示使用 Flash Player 6.0 r65 和更高版本的用户下载最新版本的 Flash Player。如果您不想让用户看到该提示,请将其删除。 --> <param name="expressinstall" value=http://www.mamicode.com/"../Script/expressInstall.swf" /> <!-- 下一个对象标签用于非 IE 浏览器。所以使用 IECC 将其从 IE 隐藏。 --> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data=http://www.mamicode.com/"../flash/sub_fla.swf" width="754" height="37"> <!--<![endif]--> <param name="quality" value=http://www.mamicode.com/"high" /> <param name="wmode" value=http://www.mamicode.com/"transparent" /> <param name="swfversion" value=http://www.mamicode.com/"8.0.35.0" /> <param name="expressinstall" value=http://www.mamicode.com/"../Script/expressInstall.swf" /> <!-- 浏览器将以下替代内容显示给使用 Flash Player 6.0 和更低版本的用户。 --> <div> <h4>此页面上的内容需要较新版本的 Adobe Flash Player。</h4> <p><a href=http://www.mamicode.com/"http://www.adobe.com/go/getflashplayer"><img src=http://www.mamicode.com/"http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="获取 Adobe Flash Player" width="112" height="33" /></a></p> </div> <!--[if !IE]>--> </object> <!--<![endif]--> </object> </div> </div> <!----------------------------------end header_column----------------------------------> <!----------------------------------begin main_column----------------------------------> <div id="main_column"> <div id="sidebar_column"> <h2 class="title_news">新闻中心</h2><!-- #BeginLibraryItem "/Library/menu_prd.lbi" --> <dl class="menu_prd"> <dt>软件产品</dt> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/NewHouse.html">新建商品房网上备案系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/StockHouse.html">存量房网上备案系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Statistics.html">统计与发布系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/ProjectManagement.html">项目管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Practitioners.html">从业主题管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Property.html">产权产籍管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Mapping.html">测绘成果及管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/MaintenanceFunds.html">维修资金管理系统</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/HousingSecurity.html">住房保障管理体系</a></dd> <dd><a class="graylink" href=http://www.mamicode.com/"../Product/Transaction.html">房产交易资金监管系统</a></dd> </dl> <a href=http://www.mamicode.com/"../Contact/Contact.html" class="contct"><img src=http://www.mamicode.com/"../images/img_contact_sidebarcolumn.png" width="160" height="52" alt="联系我们" /></a><!-- #EndLibraryItem --></div> <div id="content_column"> <div class="position"><a class="graylink" href=http://www.mamicode.com/"../index.html">首页</a> < <span class="color0range">新闻中心</span></div> <div class="cont"> <ul class="list_news"> <%=NewsHTML %>> </ul> <div class="pages"> <a href=http://www.mamicode.com/"01新闻列表.aspx?pageIndex=1">首页</a> | <a href=http://www.mamicode.com/"01新闻列表.aspx?pageIndex=<%=PageCurrent-1<1?1:PageCurrent-1 %>">前页</a> | <a href=http://www.mamicode.com/"01新闻列表.aspx?pageIndex=<%=PageCurrent+1 >= PageCount?PageCount:PageCurrent+1 %>">后页</a> | <a href=http://www.mamicode.com/"01新闻列表.aspx?pageIndex=<%=PageCount %>">尾页</a> 页次:<%=PageCurrent %>/<%=PageCount %>页 </div> <br /> <div class="page_nav"> <%=PageBarHelper.GetPageBar(PageCurrent ,PageCount ,5) %>> </div> </div> </div> <div class="clear"></div> </div> <!----------------------------------end main_column----------------------------------> <!----------------------------------begin footer_column----------------------------------><!-- #BeginLibraryItem "/Library/fooer.lbi" --> <div id="footer_column"> <span class="copyright"></span><span class="frdlink">友情链接:<select name=""> <option>公司或合作伙伴网站</option> </select></span> </div><!-- #EndLibraryItem --><!----------------------------------end footer_column----------------------------------> </div> </body> </html> <html><script language="JavaScript"> </script></html>
步步为营-73-asp.net的简单练习(根据美工提供静态页面,编写后台代码)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。