当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  HtmlHelper之DataList实现

 HtmlHelper之DataList实现

点击次数:13 次 发布日期:2008-11-26 22:36:50 作者:源代码网
源代码网推荐     
源代码网推荐   每个开发者都想在不同的开发环境下封装功能来实现代码重用和提高开发速度,在WebForm中提供各式各样的控件;但如果在纯aspx下进行HTML表现那又如何办呢,在这里就介绍通过代码来封装一个DataList的HtmlHelper对象来达到其目的;虽然没有象服务控件那样有设计支持,但巧用匿名函数也可以实现自定义模板功能;其使用编写代码量几乎同于服务器控件。
源代码网推荐  DataList的实现代码
源代码网推荐  
源代码网推荐  
源代码网推荐  Code
源代码网推荐   public class DataList<T> : IHtmlHelper
源代码网推荐   {
源代码网推荐   IHtmlHelper Members#region IHtmlHelper Members
源代码网推荐  
源代码网推荐   public void Render(System.IO.TextWriter writer)
源代码网推荐   {
源代码网推荐   Html32TextWriter html = new Html32TextWriter(writer);
源代码网推荐   DataListItemArgs<T> e;
源代码网推荐   html.WriteBeginTag("Table");
源代码网推荐   html.WriteAttribute("class", Css);
源代码网推荐   html.WriteAttribute("cellpadding", "0");
源代码网推荐   html.WriteAttribute("cellspacing", "0");
源代码网推荐   html.Write(">");
源代码网推荐   html.WriteBeginTag("tr");
源代码网推荐   html.Write(">");
源代码网推荐   int row = 0;
源代码网推荐   for (int i = 0; i < DataSource.Count; i++)
源代码网推荐   {
源代码网推荐   html.WriteBeginTag("td");
源代码网推荐   html.WriteAttribute("class", ItemCss);
源代码网推荐   html.Write(">");
源代码网推荐   if (ItemBlock != null)
源代码网推荐   {
源代码网推荐   e = new DataListItemArgs<T>(i, row, DataSource[i]);
源代码网推荐   ItemBlock(e);
源代码网推荐   }
源代码网推荐   html.WriteEndTag("td");
源代码网推荐   if ((i + 1) % Columns == 0 || (i + 1) == DataSource.Count)
源代码网推荐   {
源代码网推荐   html.WriteEndTag("tr");
源代码网推荐   }
源代码网推荐   if ((i + 1) % Columns == 0 && (i + 1) != DataSource.Count)
源代码网推荐   {
源代码网推荐   html.WriteBeginTag("tr");
源代码网推荐   html.Write(">");
源代码网推荐   row++;
源代码网推荐   }
源代码网推荐   }
源代码网推荐   html.WriteEndTag("Table");
源代码网推荐   }
源代码网推荐  
源代码网推荐   #endregion
源代码网推荐   public string Css
源代码网推荐   {
源代码网推荐   get;
源代码网推荐   set;
源代码网推荐   }
源代码网推荐   public string ItemCss
源代码网推荐   {
源代码网推荐   get;
源代码网推荐   set;
源代码网推荐   }
源代码网推荐   public IList<T> DataSource
源代码网推荐   {
源代码网推荐   get;
源代码网推荐   set;
源代码网推荐   }
源代码网推荐   private int mColumns = 1;
源代码网推荐   public int Columns
源代码网推荐   {
源代码网推荐   get
源代码网推荐   {
源代码网推荐   return mColumns;
源代码网推荐   }
源代码网推荐   set
源代码网推荐   {
源代码网推荐   mColumns = value;
源代码网推荐   }
源代码网推荐   }
源代码网推荐   public Action<DataListItemArgs<T>> ItemBlock
源代码网推荐   {
源代码网推荐   get;
源代码网推荐   set;
源代码网推荐   }
源代码网推荐  
源代码网推荐   }
源代码网推荐   public class DataListItemArgs<T> : EventArgs
源代码网推荐   {
源代码网推荐   public DataListItemArgs(int index, int row, T source)
源代码网推荐   {
源代码网推荐   Index = index;
源代码网推荐   Row = row;
源代码网推荐   mSorce = source;
源代码网推荐   }
源代码网推荐   public int Index
源代码网推荐   {
源代码网推荐   get;
源代码网推荐   set;
源代码网推荐   }
源代码网推荐   public int Row
源代码网推荐   {
源代码网推荐   get;
源代码网推荐   set;
源代码网推荐   }
源代码网推荐   private T mSorce;
源代码网推荐   public T Source
源代码网推荐   {
源代码网推荐   get
源代码网推荐   {
源代码网推荐   return mSorce;
源代码网推荐   }
源代码网推荐   }
源代码网推荐   }
源代码网推荐  
源代码网推荐  
源代码网推荐  例程效果
源代码网推荐  
源代码网推荐  
源代码网推荐  例程调用代码
源代码网推荐  
源代码网推荐  
源代码网推荐  <%
源代码网推荐  
源代码网推荐   DataList<NorthWind.Entities.Files> list = new DataList<Files>();
源代码网推荐  
源代码网推荐   list.DataSource = view.Files;
源代码网推荐  
源代码网推荐   list.Columns = 4;
源代码网推荐  
源代码网推荐   list.ItemBlock = delegate(DataListItemArgs<Files> e)
源代码网推荐  
源代码网推荐   {
源代码网推荐  
源代码网推荐   %>
源代码网推荐  
源代码网推荐  
源代码网推荐   <table style="border-style: dotted;border-width: 1px"><tr><td>
源代码网推荐  
源代码网推荐   <img style=" height:150px;width:150px" src="http://www.zzchn.com/edu/20080220/FileLoad.aspx?id=<%=e.Source.ID %>" />
源代码网推荐  
源代码网推荐   </td></tr>
源代码网推荐  
源代码网推荐   <tr><td><%=e.Source.Description %></td></tr>
源代码网推荐  
源代码网推荐   <tr><td><%=e.Source.CreateTime %></td></tr>
源代码网推荐  
源代码网推荐   <tr><td><a href="javascript:if(confirm("是否要删除<%=e.Source.Description %>图片?")){document.location.href="FilesDelete.aspx?id=<%=e.Source.ID %>"}">删除</a></td></tr>
源代码网推荐  
源代码网推荐   </table>
源代码网推荐  
源代码网推荐  
源代码网推荐  <%};
源代码网推荐  
源代码网推荐   list.Render(Response.Output);
源代码网推荐  
源代码网推荐   %>
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华