Recently, I saw a friend in the garden translate some information about ASP. net mvc article, very good, very interested to read the original author's blog (http://weblogs.asp.net/stephenwalther), see two of them write about the GridView, I just think of myself once also took the time to write this control (the Table below the http://www.hereur.cn/SiChuanEarthquake.xhtml/LoveWall is done with this extension method), the idea is roughly consistent, however, as a method extended by HtmlHelper, the method of Stephen Walther is similar to that of my first version, but as he said, there are many disadvantages, later, I found these problems and made some improvements. I think it is quite practical for those situations that do not require too complex logic. I will share them with you.
The same is to replace the traditional "header + foreach + footer" method. I have made two extension methods, Repeater and GridView, respectively. Because Repeater is relatively simple and basic, it is used in lightweight scenarios. The Repeater is sent first.
First, let's clarify the features of this Repeater:
1. You can freely convert <div>, <table>, <ul> tags, or other formats as needed. When using Table, you can specify the number of cells displayed in each row at will. When the last row has a zero header, the remaining empty cells are automatically filled.
2. Ability to customize headers and footer in methods (similar to these "complex" tasks, it should be said that the main driver of plug-ins such as Repeater and GridView is promoted in MVC ), let the entire Repeater achieve "integration" in WebForm, instead of the following situation:
<Ul>
<% Foreach (var item in collection) {%>
<Li> <% = item %> </li>
<% }%>
</Ul>
Imagine that tables containing thead and tbody will be more complex. Of course, this situation is not useless. for front-end controls such as art artists, these straightforward expressions are more conducive to our operations and control. Therefore, it must be noted that when complicated operations are required, do not abuse these extension methods. Otherwise, maintenance will be inconvenient.
In order to reach the first point mentioned above, we first create an enumeration type to specify the format used:
(Included in MvcTool/RepeaterExtension. cs)
Public enum RepeaterMode
{
/** // <Summary>
/// Do not automatically add more than mark, which is equivalent to foreach. However, header and footer are still valid.
/// </Summary>
None = 0,
Table,
Div,
Ul
}
Then we will build a RepeaterExtension class to extend the Repeater in the HtmlHelper method of the aspx page:
(Also included in MvcTool/RepeaterExtension. cs)
Public class SingleRepeater: IDisposable
{
...
}
/** // <Summary>
/// Repeater
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "helper"> </param>
/// <Param name = "dataSource"> data source </param>
/// <Param name = "header"> content before the start of the first single loop </param>
/// <Param name = "itemTempletes"> single item content </param>
/// <Param name = "separatorTemplate"> separate content cyclically each time (Table should be used with caution) </param>
/// <Param name = "footer"> content after the last cycle ends </param>
/// <Param name = "repeaterMode"> repeater mode </param>
/// <Param name = "colCount"> values of each line (valid only when repeaterMode is Table) </param>
/// <Param name = "htmlAttributes"> tag attributes </param>
/// <Returns> </returns>
Public static string Repeater <T> (this HtmlHelper helper, IEnumerable <T> dataSource,
String header, Expression <Func <T, string> itemTempletes, Expression <Func <T, string> separatorTemplate, string footer,
RepeaterMode repeaterMode, int colCount, object htmlAttributes)
{
...
}
RepeaterExtension mainly includes two methods: SingleRepeater () and Repeater <T> (). Because the specific implementation code is long, it is omitted here, you can view MvcTool/RepeaterExtension In the download below. cs file. For SingleRepeater (), you may have a question: Do you need to exist? Do I need to inherit IDisposable? Here I will briefly explain the purpose: in the current Code, whether it is SingleRepeater () or inheriting IDisposable behavior, it can be simplified and integrated into Repeater <T> () in the method, this is done here to leave a room for upgrading the page expression, such as writing in using () (to better control the page ).
Public enum RepeaterMode can be expanded based on your own needs, such as <ol>.
Next let's take a look at how to use aspx:
<% = Html. Repeater (ViewData. Model. UserInfo, // Data Source
"", // Header
Z => string. format ("{0}, age: {1}", z. userName, z. age), // item template, in order to get full control of data on the page, the string type must be returned
Z => "", // Interval
"", // Footer
RepeaterExtension. RepeaterMode. Ul, // repeat (table, div, ul)
0, // If Table is used, specify the number of columns it displays
Null // the attribute of the outermost tag, object Type
) %>
RepeaterExtension. RepeaterMode. Ul is used to specify the Repeater's display mode as Ul/Li.
Let's take a look at the actual output results:
UL (that is, the above Code output ):
Table Format (5 columns ):
No format is used (header is "[", footer is "]", interval is "| "):
Download Code:/Files/szw/MVCRepeater.rar
PS: This Repeater extension can improve the development efficiency by providing simple code for the output content. It is not only available in MVC, but also in WebForm. At the same time, some of the ideas and parameter usage are also a basis and warm-up for the next part of the GridView. Next article: develop some common plug-ins for ASP. net mvc (4) -- GridView.