Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / HtmlHelper.cs
blob687ef1dc4518b10e9a3178ae88ee832045df7f94
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Helpers
17 using System;
18 using System.IO;
19 using System.Text;
20 using System.Collections;
21 using System.Reflection;
22 using System.Web.UI;
23 using Castle.MonoRail.Framework.Internal;
25 /// <summary>
26 /// Provides usefull common methods to generate HTML tags.
27 /// </summary>
28 /// <remarks>This helper provides the means to generate commonly used HTML tags.
29 /// All of it's methods return <see cref="String"/> that holds resulting HTML.
30 /// </remarks>
31 public class HtmlHelper : AbstractHelper
33 #region Constructors
34 /// <summary>
35 /// Initializes a new instance of the <see cref="HtmlHelper"/> class.
36 /// </summary>
37 public HtmlHelper() { }
38 /// <summary>
39 /// Initializes a new instance of the <see cref="HtmlHelper"/> class.
40 /// setting the Controller, Context and ControllerContext.
41 /// </summary>
42 /// <param name="engineContext">The engine context.</param>
43 public HtmlHelper(IEngineContext engineContext) : base(engineContext) { }
44 #endregion
46 #region Fieldset
48 /// <summary>
49 /// Creates a <b>fieldset</b> tag with a legend.
50 /// <code>
51 /// &lt;fieldset&gt;&lt;legend&gt;legendArg&lt;/legend&gt;
52 /// </code>
53 /// <seealso cref="HtmlHelper.EndFieldSet"/>
54 /// </summary>
55 /// <param name="legend">Legend to use within the fieldset.</param>
56 /// <returns>HTML string opening a fieldset tag, followed by a legend tag.</returns>
57 /// <remarks>Calling <c>FieldSet( "legendArg" )</c> results in:
58 /// <code>&lt;fieldset&gt;&lt;legend&gt;legendArg&lt;/legend&gt;</code>
59 /// </remarks>
60 /// <example>This example shows how to use <see cref="FieldSet"/> together with <see cref="EndFieldSet"/>:
61 /// <code>
62 /// $HtmlHelper.FieldSet( "legendArg" )
63 /// ...
64 /// $HtmlHelper.EndFieldSet()
65 /// </code>
66 /// </example>
67 public virtual String FieldSet(String legend)
69 return String.Format("<fieldset><legend>{0}</legend>", legend);
72 /// <summary>
73 /// Creates a closing <b>fieldset</b> tag.
74 /// <code>
75 /// &lt;/fieldset&gt;
76 /// </code>
77 /// <seealso cref="HtmlHelper.FieldSet"/>
78 /// </summary>
79 /// <returns>HTML string closing the fieldset.</returns>
80 /// <remarks>This method should be invoked after <see cref="FieldSet"/> to close the fieldset.
81 /// Calling <c>EndFieldSet()</c> results in:
82 /// <code>&lt;/fieldset&gt;</code>
83 /// </remarks>
84 /// <example>This example shows how to use <see cref="FieldSet"/> together with <see cref="EndFieldSet"/>:
85 /// <code>
86 /// $HtmlHelper.FieldSet( "legendArg" )
87 /// ...
88 /// $HtmlHelper.EndFieldSet()
89 /// </code>
90 /// </example>
91 public virtual String EndFieldSet()
93 return "</fieldset>";
96 #endregion
98 #region Form and FormTo
100 ///<overloads>This method has three overloads.</overloads>
101 /// <summary>
102 /// Creates a <b>form</b> tag with "<b>post</b>" method and specified <paramref name="action"/>.
103 /// <code>
104 /// &lt;form method=&quot;post&quot; action=&quot;actionArg&quot;&gt;
105 /// </code>
106 /// <seealso cref="HtmlHelper.EndForm"/>
107 /// </summary>
108 /// <param name="action">Target action for the form.</param>
109 /// <returns>HTML string with form opening tag.</returns>
110 /// <remarks>Calling <c>Form( "actionArg" )</c> results in:
111 /// <code>&lt;form method=&quot;post&quot; action=&quot;actionArg&quot;&gt;</code>
112 /// </remarks>
113 /// <example>This example shows how to use <see cref="Form(String)"/> together with <see cref="EndForm"/>:
114 /// <code>
115 /// $HtmlHelper.Form( "actionArg" )
116 /// ...
117 /// $HtmlHelper.EndForm()
118 /// </code>
119 /// </example>
120 public virtual String Form(String action)
122 StringBuilder sb = new StringBuilder();
124 StringWriter sbWriter = new StringWriter(sb);
125 HtmlTextWriter writer = new HtmlTextWriter(sbWriter);
127 writer.WriteBeginTag("form");
128 writer.WriteAttribute("method", "post");
129 writer.WriteAttribute("action", action);
130 writer.Write(HtmlTextWriter.TagRightChar);
131 writer.WriteLine();
133 return sbWriter.ToString();
136 /// <summary>
137 /// Creates a <b>form</b> tag with the specified <paramref name="method"/>, <paramref name="action"/> and
138 /// <paramref name="id"/> attributes.
139 /// <code>
140 /// &lt;form method=&quot;methodArg&quot; action=&quot;actionArg&quot; id=&quot;idArg&quot;&gt;
141 /// </code>
142 /// <seealso cref="HtmlHelper.EndForm"/>
143 /// </summary>
144 /// <param name="action">Target action for the form.</param>
145 /// <param name="id">Form HTML ID.</param>
146 /// <param name="method">Form method (get, post, etc).</param>
147 /// <returns>HTML string with form opening tag.</returns>
148 /// <remarks>Calling <c>Form( "actionArg", "idArg", "methodArg" )</c> results in:
149 /// <code>&lt;form method=&quot;methodArg&quot; action=&quot;actionArg&quot; id=&quot;idArg&quot;&gt;</code>
150 /// </remarks>
151 /// <example>This example shows how to use <b>Form</b> together with <see cref="EndForm"/>:
152 /// <code>
153 /// $HtmlHelper.Form( "actionArg", "idArg", "methodArg" )
154 /// ...
155 /// $HtmlHelper.EndForm()
156 /// </code>
157 /// </example>
158 public virtual String Form(String action, String id, String method)
160 return Form(action, id, method, null);
163 /// <summary>
164 /// Creates a <b>form</b> tag with the specified <paramref name="method"/> and <paramref name="action"/> attributes,
165 /// <paramref name="id"/> and <paramref name="onSubmit"/> event handler.
166 /// <code>
167 /// &lt;form method=&quot;methodArg&quot; action=&quot;actionArg&quot; id=&quot;idArg&quot; onsubmit=&quot;onSubmitArg&quot;&gt;
168 /// </code>
169 /// <seealso cref="HtmlHelper.EndForm"/>
170 /// </summary>
171 /// <param name="action">Target action for the form.</param>
172 /// <param name="id">Form HTML ID.</param>
173 /// <param name="method">Form method (get, post, etc).</param>
174 /// <param name="onSubmit">JavaScript inline code to be invoked upon form submission.</param>
175 /// <returns>HTML string with form opening tag.</returns>
176 /// <remarks>Calling <c>Form( "actionArg", "idArg", "methodArg", "onSubmitArg" )</c> results in:
177 /// <code>&lt;form method=&quot;methodArg&quot; action=&quot;actionArg&quot; id=&quot;idArg&quot; onsubmit=&quot;onSubmitArg&quot;&gt;</code>
178 /// </remarks>
179 /// <example>This example shows how to use <b>Form</b> together with <see cref="EndForm"/>:
180 /// <code>
181 /// $HtmlHelper.Form( "actionArg", "idArg", "methodArg", "submitHandler()" )
182 /// ...
183 /// $HtmlHelper.EndForm()
184 /// </code>
185 /// </example>
186 public virtual String Form(String action, String id, String method, String onSubmit)
188 StringBuilder sb = new StringBuilder();
190 StringWriter sbWriter = new StringWriter(sb);
191 HtmlTextWriter writer = new HtmlTextWriter(sbWriter);
193 writer.WriteBeginTag("form");
194 writer.WriteAttribute("method", method);
195 writer.WriteAttribute("action", action);
196 writer.WriteAttribute("id", id);
197 if (onSubmit != null)
198 writer.WriteAttribute("onsubmit", onSubmit);
199 writer.Write(HtmlTextWriter.TagRightChar);
200 writer.WriteLine();
202 return sbWriter.ToString();
205 /// <summary>
206 /// Creates a <b>form</b> tag with the specified <paramref name="action"/> attribute.
207 /// <code>
208 /// &lt;form action=&quot;actionArg&quot;&gt;
209 /// </code>
210 /// <seealso cref="HtmlHelper.EndForm"/>
211 /// </summary>
212 /// <param name="action">Target action for the form.</param>
213 /// <param name="attributes">Html Attributes for the form tag</param>
214 /// <returns>HTML string with form opening tag.</returns>
215 public virtual String Form(String action, IDictionary attributes)
217 return String.Format("<form action=\"{0}\" {1}>", action, GetAttributes(attributes));
220 /// <summary>
221 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkTo(String, String)"/> methods.
222 /// </summary>
223 /// <param name="action">An action on the current controller.</param>
224 /// <returns>HTML string with form opening tag.</returns>
225 public virtual String FormTo(String action)
227 return FormTo(ControllerContext.Name, action, null);
230 /// <summary>
231 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkTo(String, String)"/> methods.
232 /// </summary>
233 /// <param name="controller">A controller name.</param>
234 /// <param name="action">An action on <paramref name="controller"/>.</param>
235 /// <returns>HTML string with form opening tag.</returns>
236 public virtual String FormTo(String controller, String action)
238 return FormTo(controller, action, null);
241 /// <summary>
242 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkTo(String, String)"/> methods.
243 /// </summary>
244 /// <param name="controller">A controller name.</param>
245 /// <param name="action">An action on <paramref name="controller"/>.</param>
246 /// <param name="id">Object to use for the action ID argument.</param>
247 /// <returns>HTML string with form opening tag.</returns>
248 public virtual String FormTo(String controller, String action, object id)
250 return FormToAttributed(controller, action, id, null);
253 /// <summary>
254 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkToAttributed(String, String, String, IDictionary)"/> methods.
255 /// </summary>
256 /// <param name="controller">A controller name.</param>
257 /// <param name="action">An action on <paramref name="controller"/>.</param>
258 /// <param name="attributes">Additional attributes for the <b>form</b> tag.</param>
259 /// <returns>HTML string with form opening tag.</returns>
260 public virtual String FormToAttributed(String controller, String action, IDictionary attributes)
262 return FormToAttributed(controller, action, null, attributes);
265 /// <summary>
266 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkToAttributed(String, String, String, IDictionary)"/> methods.
267 /// </summary>
268 /// <param name="controller">A controller name.</param>
269 /// <param name="action">An action on <paramref name="controller"/>.</param>
270 /// <param name="id">Object to use for the action ID argument.</param>
271 /// <param name="attributes">Additional attributes for the <b>form</b> tag.</param>
272 /// <returns>HTML string with form opening tag.</returns>
273 public virtual String FormToAttributed(String controller, String action, object id, IDictionary attributes)
275 return FormToAttributed(controller, action, id, null, attributes);
278 /// <summary>
279 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkToAttributed(String, String, String, IDictionary)"/> methods.
280 /// </summary>
281 /// <param name="controller">A controller name.</param>
282 /// <param name="action">An action on <paramref name="controller"/>.</param>
283 /// <param name="id">Object to use for the action ID argument.</param>
284 /// <param name="method">Form method (get, post, etc).</param>
285 /// <param name="attributes">Additional attributes for the <b>form</b> tag.</param>
286 /// <returns>HTML string with form opening tag.</returns>
287 public virtual String FormToAttributed(String controller, String action, object id, string method, IDictionary attributes)
289 string formAction = UrlHelper.For(DictHelper.Create("controller=" + controller, "action=" + action));
291 if (id != null)
293 formAction += "?id=" + id;
296 if (method == null)
298 method = "post";
301 if (attributes == null)
303 attributes = DictHelper.Create("method=" + method);
305 else
307 attributes["method"] = method;
310 return Form(formAction, attributes);
313 /// <summary>
314 /// Creates a closing <b>form</b> tag.
315 /// <code>
316 /// &lt;/form&gt;
317 /// </code>
318 /// <seealso cref="Form(String)"/>
319 /// </summary>
320 /// <returns>HTML string with form closing tag.</returns>
321 /// <remarks>
322 /// Calling <c>EndForm()</c> results in:
323 /// <code>&lt;/form&gt;</code>
324 /// </remarks>
325 /// <example>This example shows how to use <see cref="Form(String,String,String,String)"/> together with <see cref="EndForm"/>:
326 /// <code>
327 /// $HtmlHelper.Form( "actionArg", "idArg", "methodArg", "submitHandler()" )
328 /// ...
329 /// $HtmlHelper.EndForm()
330 /// </code>
331 /// </example>
332 public virtual String EndForm()
334 return "</form>";
337 #endregion
339 #region Link and LinkTo and LinkToWithPost
341 ///<overloads>This method has two overloads.</overloads>
342 /// <summary>
343 /// Creates an anchor (link) to the <paramref name="target"/>
344 /// <code>
345 /// &lt;a href=&quot;/sometarget.html&quot;&gt;linkText&lt;/a&gt;
346 /// </code>
347 /// </summary>
348 /// <param name="target">link's target.</param>
349 /// <param name="linkText">Text of the link.</param>
350 /// <returns>HTML string with anchor to the specified <paramref name="target"/>.</returns>
351 /// <remarks>Calling <c>Link( "something.html", "to something" )</c> results in:
352 /// <code>&lt;a href=&quot;something.html&quot;&gt;something&lt;/a&gt;</code>
353 /// </remarks>
354 /// <example>This example shows how to use <b>Link</b>:
355 /// <code>
356 /// $HtmlHelper.Link( "mypage.html", "This is a link to my page" )
357 /// </code>
358 /// </example>
359 public virtual String Link(String target, String linkText)
361 return LinkTo(target, linkText);
364 /// <summary>
365 /// Creates an anchor (link) to the <paramref name="target"/>
366 /// <code>
367 /// &lt;a href=&quot;/sometarget.html&quot;&gt;linkText&lt;/a&gt;
368 /// </code>
369 /// </summary>
370 /// <param name="target">link's target.</param>
371 /// <param name="linkText">Text of the link.</param>
372 /// <param name="attributes">Additional attributes for the <b>a</b> tag.</param>
373 /// <returns>HTML string with anchor to the specified <paramref name="target"/>.</returns>
374 /// <remarks>Calling <c>Link( "something.html", "to something", $DictHelper.CreateDict("class=mylinkclass") )</c> results in:
375 /// <code>&lt;a href=&quot;something.html&quot; class=&quot;mylinkclass&quot;&gt;something&lt;/a&gt;</code>
376 /// </remarks>
377 /// <example>This example shows how to use <b>Link</b>:
378 /// <code>
379 /// $HtmlHelper.Link( "mypage.html", "This is a link to my page", $DictHelper.CreateDict("class=mylinkclass") )
380 /// </code>
381 /// </example>
382 public virtual String Link(String target, String linkText, IDictionary attributes)
384 return String.Format("<a href=\"{0}\" {1}>{2}</a>",
385 target, GetAttributes(attributes), linkText);
388 ///<overloads>This method has three overloads.</overloads>
389 /// <summary>
390 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller.
391 /// <code>
392 /// &lt;a href=&quot;/website/currentController/actionArg.rails&quot;&gt;nameArg&lt;/a&gt;
393 /// </code>
394 /// </summary>
395 /// <param name="name">Name for the link.</param>
396 /// <param name="action">Action to link to.</param>
397 /// <returns>HTML string with anchor to the specified <paramref name="action"/>.</returns>
398 /// <remarks>Calling <c>LinkTo( "nameArg", "actionArg" )</c> results in:
399 /// <code>&lt;a href=&quot;/websiter/currentController/actionArg.rails&quot;&gt;nameArg&lt;/a&gt;</code>
400 /// </remarks>
401 /// <example>This example shows how to use <b>LinkTo</b>:
402 /// <code>
403 /// $HtmlHelper.LinkTo( "linkName", "requiredAction" )
404 /// </code>
405 /// </example>
406 public virtual String LinkTo(String name, String action)
408 string url = UrlHelper.For(DictHelper.Create("action=" + action));
410 return String.Format("<a href=\"{0}\">{1}</a>", url, name);
413 ///<overloads>This method has three overloads.</overloads>
414 /// <summary>
415 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller.
416 /// <code>
417 /// &lt;a href=&quot;/website/currentController/actionArg.rails&quot;&gt;nameArg&lt;/a&gt;
418 /// </code>
419 /// </summary>
420 /// <param name="name">Name for the link.</param>
421 /// <param name="options">link options</param>
422 /// <returns>HTML string with anchor to the specified <paramref name="options"/>.</returns>
423 /// <remarks>Calling <c>LinkTo( "nameArg", DictHelper.CreateDict("controller=home","action=index") )</c> results in:
424 /// <code>&lt;a href=&quot;/websiter/home/index.rails&quot;&gt;nameArg&lt;/a&gt;</code>
425 /// </remarks>
426 /// <example>This example shows how to use <b>LinkTo</b>:
427 /// <code>
428 /// $HtmlHelper.LinkTo( "linkName", DictHelper.CreateDict("controller=home","action=index") )
429 /// </code>
430 /// </example>
431 public virtual String LinkTo(String name, IDictionary options)
433 string url = UrlHelper.For(options);
434 // remove the common action attribute
435 CommonUtils.ObtainEntryAndRemove(options, "area");
436 CommonUtils.ObtainEntryAndRemove(options, "controller");
437 CommonUtils.ObtainEntryAndRemove(options, "action");
439 // and consider the other options to be link attributes
440 return String.Format("<a href=\"{0}\" {2}>{1}</a>", url, name, GetAttributes(options));
443 /// <summary>
444 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/>.
445 /// <code>
446 /// &lt;a href=&quot;/website/controllerArg/actionArg.rails&quot;&gt;nameArg&lt;/a&gt;
447 /// </code>
448 /// </summary>
449 /// <param name="name">Name for the link.</param>
450 /// <param name="controller">Controller to link to.</param>
451 /// <param name="action">Action to link to.</param>
452 /// <returns>HTML string with anchor to the specified <paramref name="controller"/>
453 /// and <paramref name="action"/>.</returns>
454 /// <remarks>Calling <c>LinkTo( "nameArg", options )</c> results in:
455 /// <code>&lt;a href=&quot;/website/controllerArg/actionArg.rails&quot;&gt;nameArg&lt;/a&gt;</code>
456 /// </remarks>
457 /// <example>This example shows how to use <b>LinkTo</b>:
458 /// <code>
459 /// $HtmlHelper.LinkTo( "linkName", {@action:} )
460 /// </code>
461 /// </example>
462 public virtual String LinkTo(String name, String controller, String action)
464 string url = UrlHelper.For(DictHelper.Create("controller=" + controller, "action=" + action));
466 return String.Format("<a href=\"{0}\">{1}</a>", url, name);
469 /// <summary>
470 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/>
471 /// passing provided <paramref name="id"/>.
472 /// <code>
473 /// &lt;a href=&quot;/website/controllerArg/actionArg.rails?id=objectId&quot;&gt;nameArg&lt;/a&gt;
474 /// </code>
475 /// </summary>
476 /// <param name="name">Name for the link.</param>
477 /// <param name="controller">Controller to link to.</param>
478 /// <param name="action">Action to link to.</param>
479 /// <param name="id">Object to use for the action ID argument.</param>
480 /// <returns>HTML string with anchor to the specified <paramref name="controller"/>,
481 /// <paramref name="action"/> and <paramref name="id"/>.</returns>
482 /// <remarks>Calling <c>LinkTo( "nameArg", "controllerArg", "actionArg", object )</c> results in:
483 /// <code>&lt;a href=&quot;/website/controllerArg/actionArg.rails?id=object&quot;&gt;nameArg&lt;/a&gt;</code>
484 /// <para>
485 /// <see cref="String.Format(String, object)"/> is used to convert <paramref name="id"/> to the actual <see cref="String"/>.</para>
486 /// </remarks>
487 /// <example>This example shows how to use <b>LinkTo</b>:
488 /// <code>
489 /// $HtmlHelper.LinkTo( "linkName", "someController", "requiredAction", objectToRefByID )
490 /// </code>
491 /// </example>
492 public virtual String LinkTo(String name, String controller, String action, object id)
494 string url = UrlHelper.For(DictHelper.Create("controller=" + controller, "action=" + action));
496 return String.Format("<a href=\"{0}?id={1}\">{2}</a>", url, id, name);
499 /// <summary>
500 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/>
501 /// <code>
502 /// &lt;a href=&quot;/website/controllerArg/actionArg.rails&quot;&gt;nameArg&lt;/a&gt;
503 /// </code>
504 /// </summary>
505 /// <param name="name">Name for the link.</param>
506 /// <param name="controller">Controller to link to.</param>
507 /// <param name="action">Action to link to.</param>
508 /// <param name="attributes">Additional attributes for the <b>a</b> tag.</param>
509 /// <returns>HTML string with anchor to the specified <paramref name="controller"/></returns>
510 /// <remarks>Calling <c>LinkToAttributed( "nameArg", "controllerArg", "actionArg", IDictionary )</c> results in:
511 /// <code>&lt;a href=&quot;/website/controllerArg/actionArg.rails&quot;&gt;nameArg&lt;/a&gt;</code>
512 /// </remarks>
513 /// <example>This example shows how to use <b>LinkToAttributed</b>:
514 /// <code>
515 /// $HtmlHelper.LinkToAttributed( "linkName", "someController", "requiredAction", $DictHelper.CreateDict("class=something") )
516 /// </code>
517 /// </example>
518 public virtual String LinkToAttributed(String name, String controller, String action, IDictionary attributes)
520 string url = UrlHelper.For(DictHelper.Create("controller=" + controller, "action=" + action));
522 return String.Format("<a href=\"{0}\" {2}>{1}</a>", url, name, GetAttributes(attributes));
525 /// <summary>
526 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/>
527 /// <code>
528 /// &lt;a href=&quot;/website/controllerArg/actionArg.rails?id=x&quot;&gt;nameArg&lt;/a&gt;
529 /// </code>
530 /// </summary>
531 /// <param name="name">Name for the link.</param>
532 /// <param name="controller">Controller to link to.</param>
533 /// <param name="action">Action to link to.</param>
534 /// <param name="id">The ID to be passed as a parameter for the action</param>
535 /// <param name="attributes">Additional attributes for the <b>a</b> tag.</param>
536 /// <returns>HTML string with anchor to the specified <paramref name="controller"/></returns>
537 /// <remarks>Calling <c>LinkToAttributed( "nameArg", "controllerArg", "actionArg", IDictionary )</c> results in:
538 /// <code>&lt;a href=&quot;/website/controllerArg/actionArg.rails&quot;&gt;nameArg&lt;/a&gt;</code>
539 /// </remarks>
540 /// <example>This example shows how to use <b>LinkToAttributed</b>:
541 /// <code>
542 /// $HtmlHelper.LinkToAttributed( "linkName", "someController", "requiredAction", $DictHelper.CreateDict("class=something") )
543 /// </code>
544 /// </example>
545 public virtual String LinkToAttributed(String name, String controller, String action, object id, IDictionary attributes)
547 string url = UrlHelper.For(DictHelper.Create("controller=" + controller, "action=" + action));
549 return String.Format("<a href=\"{0}?id={1}\" {3}>{2}</a>", url, id, name, GetAttributes(attributes));
552 /// <summary>
553 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller that posts
554 /// using a hidden form element.
555 /// </summary>
556 /// <param name="name">Name for the link.</param>
557 /// <param name="action">Action to link to on the current controller.</param>
558 /// <returns>HTML string with anchor that posts to the current controller</returns>
559 public virtual String LinkToWithPost(String name, String action)
561 return LinkToWithPost(name, action, null);
564 /// <summary>
565 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller that posts
566 /// using a hidden form element.
567 /// </summary>
568 /// <param name="name">Name for the link.</param>
569 /// <param name="action">Action to link to on the current controller.</param>
570 /// <param name="id">The ID to be passed as a parameter for the action.</param>
571 /// <returns>HTML string with anchor that posts to the current controller</returns>
572 public virtual String LinkToWithPost(String name, String action, object id)
574 return LinkToWithPost(name, ControllerContext.Name, action, id);
577 /// <summary>
578 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller that posts
579 /// using a hidden form element.
580 /// </summary>
581 /// <param name="name">Name for the link.</param>
582 /// <param name="action">Action to link to on the current controller.</param>
583 /// <param name="confirm">Guards the form submission with a javascript confirm popup.</param>
584 /// <returns>HTML string with anchor that posts to the current controller</returns>
585 public virtual String LinkToWithPost(String name, String action, String confirm)
587 return LinkToWithPost(name, ControllerContext.Name, action, confirm);
590 /// <summary>
591 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller that posts
592 /// using a hidden form element.
593 /// </summary>
594 /// <param name="name">Name for the link.</param>
595 /// <param name="action">Action to link to on the current controller.</param>
596 /// <param name="id">The ID to be passed as a parameter for the action.</param>
597 /// <param name="confirm">Guards the form submission with a javascript confirm popup.</param>
598 /// <returns>HTML string with anchor that posts to the current controller</returns>
599 public virtual String LinkToWithPost(String name, String action, object id, string confirm)
601 return LinkToWithPost(name, ControllerContext.Name, action, id, confirm);
604 /// <summary>
605 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/> that posts
606 /// using a hidden form element.
607 /// </summary>
608 /// <param name="name">Name for the link.</param>
609 /// <param name="controller">Controller to link to.</param>
610 /// <param name="action">Action to link to.</param>
611 /// <param name="id">The ID to be passed as a parameter for the action.</param>
612 /// <returns>HTML string with anchor that posts to the specified <paramref name="controller"/></returns>
613 public virtual String LinkToWithPost(String name, String controller, String action, object id)
615 return LinkToWithPost(name, controller, action, id, null);
618 /// <summary>
619 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/> that posts
620 /// using a hidden form element.
621 /// </summary>
622 /// <param name="name">Name for the link.</param>
623 /// <param name="controller">Controller to link to.</param>
624 /// <param name="action">Action to link to.</param>
625 /// <param name="confirm">Guards the form submission with a javascript confirm popup.</param>
626 /// <returns>HTML string with anchor that posts to the specified <paramref name="controller"/></returns>
627 public virtual String LinkToWithPost(String name, String controller, String action, String confirm)
629 return LinkToWithPost(name, controller, action, null, confirm);
632 /// <summary>
633 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/> that posts
634 /// using a hidden form element.
635 /// </summary>
636 /// <param name="name">Name for the link.</param>
637 /// <param name="controller">Controller to link to.</param>
638 /// <param name="action">Action to link to.</param>
639 /// <param name="id">The ID to be passed as a parameter for the action.</param>
640 /// <param name="confirm">Guards the form submission with a javascript confirm popup.</param>
641 /// <returns>HTML string with anchor that posts to the specified <paramref name="controller"/></returns>
642 public virtual String LinkToWithPost(String name, String controller, String action, object id, String confirm)
644 return LinkToWithPostAttributed(name, controller, action, id, confirm, null);
647 /// <summary>
648 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/> that posts
649 /// using a hidden form element.
650 /// </summary>
651 /// <param name="name">Name for the link.</param>
652 /// <param name="controller">Controller to link to.</param>
653 /// <param name="action">Action to link to.</param>
654 /// <param name="confirm">Guards the form submission with a javascript confirm popup.</param>
655 /// <param name="attributes">Additional attributes for the <b>a</b> tag.</param>
656 /// <returns>HTML string with anchor that posts to the specified <paramref name="controller"/></returns>
657 public virtual String LinkToWithPostAttributed(String name, String controller, String action, String confirm,
658 IDictionary attributes)
660 return LinkToWithPostAttributed(name, controller, action, null, confirm, attributes);
663 /// <summary>
664 /// Creates an anchor (link) to the <paramref name="action"/>
665 /// on the specified <paramref name="controller"/> that posts
666 /// using a hidden form element.
667 /// </summary>
668 /// <param name="name">Name for the link.</param>
669 /// <param name="controller">Controller to link to.</param>
670 /// <param name="action">Action to link to.</param>
671 /// <param name="id">The ID to be passed as a parameter for the action.</param>
672 /// <param name="confirm">Guards the form submission with a javascript confirm popup.</param>
673 /// <param name="attributes">Additional attributes for the <b>a</b> tag.</param>
674 /// <returns>HTML string with anchor that posts to the specified <paramref name="controller"/></returns>
675 public virtual String LinkToWithPostAttributed(String name, String controller, String action, object id, String confirm,
676 IDictionary attributes)
678 IDictionary formAttributes = DictHelper.Create("style=display:inline;margin:0;");
680 string onclickAttribute = "this.parentNode.submit();return false;";
682 if (confirm != null && confirm != "")
684 onclickAttribute = "if(confirm('" + confirm + "')){{this.parentNode.submit();}};return false;";
687 if (attributes == null)
689 attributes = DictHelper.Create("onclick=" + onclickAttribute);
691 else
693 attributes["onclick"] = onclickAttribute;
696 StringBuilder stringBuilder = new StringBuilder();
698 if (id != null)
700 stringBuilder.Append(FormToAttributed(controller, action, id, formAttributes));
701 stringBuilder.Append(LinkToAttributed(name, controller, action, id, attributes));
703 else
705 stringBuilder.Append(FormToAttributed(controller, action, formAttributes));
706 stringBuilder.Append(LinkToAttributed(name, controller, action, attributes));
709 stringBuilder.Append(EndForm());
711 return stringBuilder.ToString();
714 #endregion
716 #region MapToVirtual
718 /// <summary>
719 /// Maps <paramref name="target"/> to website virtual path.
720 /// <code>/website/targetArg</code>
721 /// </summary>
722 /// <param name="target">Target path to map.</param>
723 /// <returns>URL string pointing to the <paramref name="target"/> in the context of the website.</returns>
724 /// <remarks>Calling <c>MapToVirtual( "targetArg" )</c> results in:
725 /// <code>/website/targetArg</code>
726 /// </remarks>
727 /// <example>This example shows how to use <see cref="MapToVirtual"/>:
728 /// <code>
729 /// $HtmlHelper.MapToVirtual( "targetFolder/targetFile.html" )
730 /// </code>
731 /// </example>
732 public virtual String MapToVirtual(String target)
734 String appPath = Context.ApplicationPath.EndsWith("/") ? Context.ApplicationPath : Context.ApplicationPath + "/";
736 String targetPath = target.StartsWith("/") ? target.Substring(1) : target;
738 return String.Concat(appPath, targetPath);
741 #endregion
743 #region LabelFor
745 ///<overloads>This method has two overloads.</overloads>
746 /// <summary>
747 /// Creates a label for the element indicated with
748 /// <paramref name="forId"/>.
749 /// <code>
750 /// &lt;label for=&quot;forIdArg&quot;&gt;labelArg&lt;/label&gt;
751 /// </code>
752 /// </summary>
753 /// <param name="forId">ID of the element for which to create the lable.</param>
754 /// <param name="label">Label name.</param>
755 /// <returns>HTML string with generated label.</returns>
756 /// <remarks>Calling <c>LabelFor( "forIdArg", "labelArg" )</c> results in:
757 /// <code>&lt;label for=&quot;forIdArg&quot;&gt;labelArg&lt;/label&gt;</code>
758 /// </remarks>
759 /// <example>This example shows how to use <see cref="LabelFor(String,String)"/>:
760 /// <code>
761 /// $HtmlHelper.LabelFor( "forIdArg", "labelArg" )
762 /// </code>
763 /// </example>
764 public virtual String LabelFor(String forId, String label)
766 return LabelFor(forId, label, null);
769 /// <summary>
770 /// Creates a label for the element indicated with
771 /// <paramref name="forId"/>.
772 /// <code>
773 /// &lt;label key1=&quot;value1&quot; key2=&quot;value2&quot; for=&quot;forIdArg&quot;&gt;labelArg&lt;/label&gt;
774 /// </code>
775 /// </summary>
776 /// <param name="forId">ID of the element for which to create the label.</param>
777 /// <param name="label">Label name.</param>
778 /// <param name="attributes">Additional attributes to add to the label.</param>
779 /// <returns>HTML string with generated label.</returns>
780 /// <remarks>Calling <c>LabelFor( "forIdArg", "labelArg", IDictionary )</c> results in:
781 /// <code>&lt;label key5=&quot;value5&quot; key4=&quot;value4&quot; key1=&quot;value1&quot; key3=&quot;value3&quot; key2=&quot;value2&quot; for=&quot;forIdArg&quot;&gt;labelArg&lt;/label&gt;</code>
782 /// </remarks>
783 /// <para>
784 /// <paramref name="attributes"/> is used to generate additional attributes for the <b>label</b> tag.
785 /// <see cref="IDictionary.Keys"/> are used to name attributes.
786 /// <see cref="IDictionary.Values"/> are used to assign those attributes values.
787 /// </para>
788 /// <example>This example shows how to use <see cref="LabelFor(String,String,IDictionary)"/>:
789 /// <code>
790 /// $HtmlHelper.LabelFor( "forIdArg", "labelArg", IDictionary )
791 /// </code>
792 /// </example>
793 public virtual String LabelFor(String forId, String label, IDictionary attributes)
795 StringBuilder sb = new StringBuilder();
796 StringWriter sbWriter = new StringWriter(sb);
797 HtmlTextWriter writer = new HtmlTextWriter(sbWriter);
799 writer.WriteBeginTag("label");
800 writer.Write(" ");
801 writer.Write(GetAttributes(attributes));
802 writer.WriteAttribute("for", forId);
803 writer.Write(HtmlTextWriter.TagRightChar);
804 writer.Write(label);
805 writer.WriteEndTag("label");
806 writer.WriteLine();
808 return sbWriter.ToString();
811 #endregion
813 #region DateTime
815 ///<overloads>This method has two overloads.</overloads>
816 /// <summary>
817 /// Creates three <b>select</b> tags to input day, month and year.
818 /// <code>
819 /// &lt;select name=&quot;nameArgday&quot; id=&quot;nameArgday&quot; &gt; ... &lt;/select&gt;
820 /// &lt;select name=&quot;nameArgmonth&quot; id=&quot;nameArgmonth&quot; &gt; ... &lt;/select&gt;
821 /// &lt;select name=&quot;nameArgyear&quot; id=&quot;nameArgyear&quot; &gt; ... &lt;/select&gt;
822 /// </code>
823 /// </summary>
824 /// <param name="name">Name to use with <b>name</b> and <b>id</b> arguments of the <b>select</b> tag.</param>
825 /// <param name="value"><see cref="System.DateTime"/> to use for default selected date.</param>
826 /// <returns>A long HTML string with three <b>select</b> tag input date.</returns>
827 /// <remarks>Calling <c>DateTime( "nameArg", new DateTime( 2005, 07, 15 ) )</c> results in:
828 /// <code>
829 /// &lt;select name=&quot;nameArgday&quot; id=&quot;nameArgday&quot; &gt; &lt;option&gt;1&lt;/option&gt;
830 /// &lt;option&gt;2&lt;/option&gt;
831 /// ...
832 /// &lt;option&gt;14&lt;/option&gt;
833 /// &lt;option selected&gt;15&lt;/option&gt;
834 /// &lt;option&gt;16&lt;/option&gt;
835 /// ...
836 /// &lt;option&gt;30&lt;/option&gt;
837 /// &lt;option&gt;31&lt;/option&gt;
838 /// &lt;/select&gt; &lt;select name=&quot;nameArgmonth&quot; id=&quot;nameArgmonth&quot; &gt; &lt;option&gt;1&lt;/option&gt;
839 /// &lt;option&gt;2&lt;/option&gt;
840 /// ...
841 /// &lt;option&gt;6&lt;/option&gt;
842 /// &lt;option selected&gt;7&lt;/option&gt;
843 /// &lt;option&gt;8&lt;/option&gt;
844 /// ...
845 /// &lt;option&gt;11&lt;/option&gt;
846 /// &lt;option&gt;12&lt;/option&gt;
847 /// &lt;/select&gt; &lt;select name=&quot;nameArgyear&quot; id=&quot;nameArgyear&quot; &gt; &lt;option&gt;1930&lt;/option&gt;
848 /// &lt;option&gt;1931&lt;/option&gt;
849 /// ...
850 /// &lt;option&gt;2004&lt;/option&gt;
851 /// &lt;option selected&gt;2005&lt;/option&gt;
852 /// &lt;option&gt;2006&lt;/option&gt;
853 /// ...
854 /// &lt;option&gt;2029&lt;/option&gt;
855 /// &lt;/select&gt;</code>
856 /// As above example shows the year range is hardcoded between 1930 and 2029.
857 /// <para>
858 /// <paramref name="name"/> is used to generate <b>name</b> and <b>id</b> for each <b>select</b> tag.
859 /// Supplied <see cref="String"/> is concatenated with "day", "month", or "year" to create
860 /// <see cref="String"/> for the tag attributes.
861 /// </para>
862 /// </remarks>
863 /// <example>This example shows how to use <b>DateTime</b>:
864 /// <code>
865 /// $HtmlHelper.DateTime( "nameArg", new DateTime( 2005, 07, 15 ) )
866 /// </code>
867 /// </example>
868 public virtual String DateTime(String name, DateTime value)
870 return DateTime(name, value, null);
873 /// <summary>
874 /// Creates three <b>select</b> tags to input day, month and year.
875 /// <code>
876 /// &lt;select name=&quot;nameArgday&quot; id=&quot;nameArgday&quot; key1=&quot;value1&quot; key3=&quot;value3&quot; key2=&quot;value2&quot; &gt; ... &lt;/select&gt;
877 /// &lt;select name=&quot;nameArgmonth&quot; id=&quot;nameArgmonth&quot; key1=&quot;value1&quot; key3=&quot;value3&quot; key2=&quot;value2&quot; &gt; ... &lt;/select&gt;
878 /// &lt;select name=&quot;nameArgyear&quot; id=&quot;nameArgyear&quot; key1=&quot;value1&quot; key3=&quot;value3&quot; key2=&quot;value2&quot; &gt; ... &lt;/select&gt;
879 /// </code>
880 /// </summary>
881 /// <param name="name">Name to use with <b>name</b> and <b>id</b> arguments of the <b>select</b> tag.</param>
882 /// <param name="value"><see cref="System.DateTime"/> to use for default selected date.</param>
883 /// <param name="attributes">Additional attributes for <b>select</b> tags.</param>
884 /// <returns>A long HTML string with three <b>select</b> tag input date.</returns>
885 /// <remarks>Calling <c>DateTime( "nameArg", new DateTime( 2005, 07, 15 ), IDictionary )</c> results in:
886 /// <code>
887 /// &lt;select name=&quot;nameArgday&quot; id=&quot;nameArgday&quot; key1=&quot;value1&quot; key2=&quot;value2&quot; &gt; &lt;option&gt;1&lt;/option&gt;
888 /// &lt;option&gt;2&lt;/option&gt;
889 /// ...
890 /// &lt;option&gt;14&lt;/option&gt;
891 /// &lt;option selected&gt;15&lt;/option&gt;
892 /// &lt;option&gt;16&lt;/option&gt;
893 /// ...
894 /// &lt;option&gt;30&lt;/option&gt;
895 /// &lt;option&gt;31&lt;/option&gt;
896 /// &lt;/select&gt; &lt;select name=&quot;nameArgmonth&quot; id=&quot;nameArgmonth&quot; key1=&quot;value1&quot; key2=&quot;value2&quot; &gt; &lt;option&gt;1&lt;/option&gt;
897 /// &lt;option&gt;2&lt;/option&gt;
898 /// ...
899 /// &lt;option&gt;6&lt;/option&gt;
900 /// &lt;option selected&gt;7&lt;/option&gt;
901 /// &lt;option&gt;8&lt;/option&gt;
902 /// ...
903 /// &lt;option&gt;11&lt;/option&gt;
904 /// &lt;option&gt;12&lt;/option&gt;
905 /// &lt;/select&gt; &lt;select name=&quot;nameArgyear&quot; id=&quot;nameArgyear&quot; key1=&quot;value1&quot; key2=&quot;value2&quot; &gt; &lt;option&gt;1930&lt;/option&gt;
906 /// &lt;option&gt;1931&lt;/option&gt;
907 /// ...
908 /// &lt;option&gt;2004&lt;/option&gt;
909 /// &lt;option selected&gt;2005&lt;/option&gt;
910 /// &lt;option&gt;2006&lt;/option&gt;
911 /// ...
912 /// &lt;option&gt;2029&lt;/option&gt;
913 /// &lt;/select&gt;</code>
914 /// As above example shows the year range is hardcoded between 1930 and 2029.
915 /// <para>
916 /// <paramref name="name"/> is used to generate <b>name</b> and <b>id</b> for each <b>select</b> tag.
917 /// Supplied <see cref="String"/> is concatenated with "day", "month", or "year" to create
918 /// <see cref="String"/> for the tag attributes.
919 /// </para>
920 /// <para>
921 /// <paramref name="attributes"/> is used to generate additional attributes for each of the <b>select</b> tags.
922 /// <see cref="IDictionary.Keys"/> are used to name attributes.
923 /// <see cref="IDictionary.Values"/> are used to assign those attributes values.
924 /// </para>
925 /// </remarks>
926 /// <example>This example shows how to use <b>DateTime</b>:
927 /// <code>
928 /// $HtmlHelper.DateTime( "nameArg", new DateTime( 2005, 07, 15 ), IDictionary )
929 /// </code>
930 /// </example>
931 public virtual String DateTime(String name, DateTime value, IDictionary attributes)
933 String[] days = new String[31];
934 int index = 0;
935 for(int i = 1; i < 32; i++)
936 days[index++] = i.ToString();
938 String[] months = new String[12];
939 index = 0;
940 for(int i = 1; i < 13; i++)
941 months[index++] = i.ToString();
943 String[] years = new String[100];
944 index = 0;
945 for(int i = 1930; i < 2030; i++)
946 years[index++] = i.ToString();
948 StringBuilder sb = new StringBuilder(1024);
950 sb.Append(Select(name + "day", attributes));
951 sb.Append(CreateOptionsFromPrimitiveArray(days, value.Day.ToString()));
952 sb.Append(EndSelect());
953 sb.Append(' ');
954 sb.Append(Select(name + "month", attributes));
955 sb.Append(CreateOptionsFromPrimitiveArray(months, value.Month.ToString()));
956 sb.Append(EndSelect());
957 sb.Append(' ');
958 sb.Append(Select(name + "year", attributes));
959 sb.Append(CreateOptionsFromPrimitiveArray(years, value.Year.ToString()));
960 sb.Append(EndSelect());
962 return sb.ToString();
965 #endregion
967 #region TextArea
969 /// <summary>
970 /// Creates a text area element.
971 /// <code>&lt;textarea id=&quot;nameArg&quot; name=&quot;nameArg&quot; cols=&quot;10&quot; rows=&quot;10&quot;&gt;valueArg&lt;/textarea&gt;</code>
972 /// </summary>
973 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
974 /// <param name="cols"><b>cols</b> attribute value.</param>
975 /// <param name="rows"><b>rows</b> attribute value.</param>
976 /// <param name="value">Text to place inside of the text area.</param>
977 /// <returns>HTML string with closed <b>textarea</b> tag.</returns>
978 /// <remarks>Calling <c>TextArea( "nameArg", 10, 10, "valueArg" )</c> results in:
979 /// <code>&lt;textarea id=&quot;nameArg&quot; name=&quot;nameArg&quot; cols=&quot;10&quot; rows=&quot;10&quot;&gt;valueArg&lt;/textarea&gt;</code>
980 /// </remarks>
981 /// <example>This example shows how to use <see cref="TextArea"/>:
982 /// <code>
983 /// $HtmlHelper.TextArea( "nameArg", 10, 20, "Text inside text area." )
984 /// </code>
985 /// </example>
986 public virtual String TextArea(String name, int cols, int rows, String value)
988 return String.Format("<textarea id=\"{0}\" name=\"{0}\" cols=\"{1}\" rows=\"{2}\">{3}</textarea>",
989 name, cols, rows, value);
992 #endregion
994 #region InputButton
996 /// <overloads>This method has three overloads.</overloads>
997 /// <summary>
998 /// Creates an input element of the button type.
999 /// <code>&lt;input type=&quot;button&quot; value=&quot;valueArg&quot; /&gt;</code>
1000 /// </summary>
1001 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1002 /// <returns>HTML string with button type <b>input</b> tag.</returns>
1003 /// <remarks>Calling <c>InputButton( "valueArg" )</c> results in:
1004 /// <code>&lt;input type=&quot;button&quot; name=&quot;valueArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1005 /// </remarks>
1006 /// <example>This example shows how to use <b>InputButton</b>:
1007 /// <code>
1008 /// $HtmlHelper.InputButton( "valueArg" )
1009 /// </code>
1010 /// </example>
1011 public virtual String InputButton(String value)
1013 return InputButton(value, value);
1016 /// <summary>
1017 /// Creates an input element of the button type.
1018 /// <code>&lt;input type=&quot;button&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1019 /// </summary>
1020 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
1021 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1022 /// <returns>HTML string with button type <b>input</b> tag.</returns>
1023 public virtual String InputButton(String name, String value)
1025 return InputButton(name, value, null);
1028 /// <summary>
1029 /// Creates an input element of the button type.
1030 /// <code>&lt;input type=&quot;button&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1031 /// </summary>
1032 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
1033 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1034 /// <param name="attributes">Additional attributes for the <b>input</b> tag.</param>
1035 /// <returns>HTML string with button type <b>input</b> tag.</returns>
1036 public virtual String InputButton(String name, String value, IDictionary attributes)
1038 return String.Format("<input type=\"button\" name=\"{0}\" value=\"{1}\" {2} />",
1039 name, value, GetAttributes(attributes));
1042 #endregion
1044 #region InputCheckbox
1046 /// <overloads>This method has three overloads.</overloads>
1047 /// <summary>
1048 /// Creates an input element of the checkbox type.
1049 /// <code>&lt;input type=&quot;checkbox&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1050 /// </summary>
1051 /// <param name="name">Value for <c>name</c> and <c>id</c> attributes.</param>
1052 /// <param name="value"><see cref="String"/> for <c>value</c> attribute.</param>
1053 /// <returns>HTML string with checkbox type <c>input</c> tag.</returns>
1054 /// <remarks>Calling <c>InputCheckbox( "name", "1" )</c> results in:
1055 /// <code>&lt;input type=&quot;checkbox&quot; name=&quot;name&quot; id=&quot;name&quot; value=&quot;1&quot; /&gt;</code>
1056 /// </remarks>
1057 public virtual String InputCheckbox(String name, Object value)
1059 return InputCheckbox(name, value, null);
1062 /// <summary>
1063 /// Creates an input element of the checkbox type.
1064 /// <code>&lt;input type=&quot;checkbox&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1065 /// </summary>
1066 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
1067 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1068 /// <param name="isChecked">If true, adds the <c>checked</c> attributed to the tag</param>
1069 /// <returns>HTML string with checkbox type <b>input</b> tag.</returns>
1070 public virtual String InputCheckbox(String name, Object value, bool isChecked)
1072 IDictionary attributes = null;
1074 if (isChecked)
1076 attributes = new Hashtable();
1077 attributes["checked"] = null;
1080 return InputCheckbox(name, value, attributes);
1083 /// <summary>
1084 /// Creates an input element of the checkbox type.
1085 /// <code>&lt;input type=&quot;checkbox&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1086 /// </summary>
1087 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
1088 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1089 /// <param name="attributes">Additional attributes for the <b>input</b> tag.</param>
1090 /// <returns>HTML string with checkbox type <b>input</b> tag.</returns>
1091 public virtual String InputCheckbox(String name, Object value, IDictionary attributes)
1093 return String.Format("<input type=\"checkbox\" name=\"{0}\" id=\"{0}\" value=\"{1}\" {2} />",
1094 name, value, GetAttributes(attributes));
1097 #endregion
1099 #region InputRadio
1101 /// <overloads>This method has two overloads.</overloads>
1102 /// <summary>
1103 /// Creates an input element of the radio type.
1104 /// <code>&lt;input type=&quot;radio&quot; name=&quot;nameArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1105 /// </summary>
1106 /// <param name="name">Value for <b>name</b> attribute.</param>
1107 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1108 /// <returns>HTML string with radio type <b>input</b> tag.</returns>
1109 /// <remarks>Calling <c>InputRadio( "name", "1" )</c> results in:
1110 /// <code>&lt;input type=&quot;radio&quot; name=&quot;name&quot; value=&quot;1&quot; /&gt;</code>
1111 /// </remarks>
1112 public virtual String InputRadio(String name, Object value)
1114 return InputRadio(name, value, null);
1117 /// <summary>
1118 /// Creates an input element of the radio type.
1119 /// <code>&lt;input type=&quot;radio&quot; name=&quot;nameArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1120 /// </summary>
1121 /// <param name="name">Value for <b>name</b> attribute.</param>
1122 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1123 /// <param name="attributes">Additional attributes for the <b>input</b> tag.</param>
1124 /// <returns>HTML string with radio type <b>input</b> tag.</returns>
1125 public virtual String InputRadio(String name, Object value, IDictionary attributes)
1127 return String.Format("<input type=\"radio\" name=\"{0}\" value=\"{1}\" {2} />",
1128 name, value, GetAttributes(attributes));
1131 #endregion
1133 #region InputFile
1135 /// <overloads>This method has two overloads.</overloads>
1136 /// <summary>
1137 /// Creates an input element of the file type.
1138 /// <code>&lt;input type=&quot;file&quot; name=&quot;nameArg&quot; /&gt;</code>
1139 /// </summary>
1140 /// <param name="name">Value for <b>name</b> attribute.</param>
1141 /// <returns>HTML string with file type <b>input</b> tag.</returns>
1142 /// <remarks>Calling <c>InputFile( "name" )</c> results in:
1143 /// <code>&lt;input type=&quot;file&quot; name=&quot;name&quot; /&gt;</code>
1144 /// </remarks>
1145 public virtual String InputFile(String name)
1147 return InputFile(name, null);
1150 /// <summary>
1151 /// Creates an input element of the file type.
1152 /// <code>&lt;input type=&quot;file&quot; name=&quot;nameArg&quot; /&gt;</code>
1153 /// </summary>
1154 /// <param name="name">Value for <b>name</b> attribute.</param>
1155 /// <param name="attributes">Additional attributes for the <b>input</b> tag.</param>
1156 /// <returns>HTML string with file type <b>input</b> tag.</returns>
1157 public virtual String InputFile(String name, IDictionary attributes)
1159 return String.Format("<input type=\"file\" name=\"{0}\" {1} />",
1160 name, GetAttributes(attributes));
1163 #endregion
1165 #region InputText
1167 /// <overloads>This method has four overloads.</overloads>
1168 /// <summary>
1169 /// Creates an input element of the text type.
1170 /// <code>&lt;input type=&quot;text&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1171 /// </summary>
1172 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
1173 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1174 /// <returns>HTML string with text type <b>input</b> tag.</returns>
1175 /// <remarks>Calling <c>InputText( "nameArg", "valueArg" )</c> results in:
1176 /// <code>&lt;input type=&quot;text&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1177 /// </remarks>
1178 /// <example>This example shows how to use <b>InputText</b>:
1179 /// <code>
1180 /// $HtmlHelper.InputText( "nameArg", "valueArg" )
1181 /// </code>
1182 /// </example>
1183 public virtual String InputText(String name, String value)
1185 return InputText(name, value, name);
1188 /// <summary>
1189 /// Creates an input element of the text type of specified
1190 /// <paramref name="size"/> and <paramref name="maxlength"/>.
1191 /// <code>&lt;input type=&quot;text&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; size=&quot;10&quot; maxlength=&quot;10&quot; /&gt;</code>
1192 /// </summary>
1193 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
1194 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1195 /// <param name="size"><b>size</b> attribute value.</param>
1196 /// <param name="maxlength"><b>maxlength</b> attribute value.</param>
1197 /// <returns>HTML string with text type <b>input</b> tag.</returns>
1198 /// <remarks>Calling <c>InputText( "nameArg", "valueArg", 10, 10 )</c> results in:
1199 /// <code>&lt;input type=&quot;text&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; size=&quot;10&quot; maxlength=&quot;10&quot; /&gt;</code>
1200 /// </remarks>
1201 /// <example>This example shows how to use <b>InputText</b>:
1202 /// <code>
1203 /// $HtmlHelper.InputText( "nameArg", "valueArg", 10, 10 )
1204 /// </code>
1205 /// </example>
1206 public virtual String InputText(String name, String value, int size, int maxlength)
1208 return String.Format("<input type=\"text\" name=\"{0}\" id=\"{0}\" value=\"{1}\" size=\"{2}\" maxlength=\"{3}\" />",
1209 name, value, size, maxlength);
1212 /// <summary>
1213 /// Creates an input element of the text type with specified
1214 /// <paramref name="size"/>, <paramref name="maxlength"/> and <paramref name="attributes"/>.
1215 /// <code>&lt;input type=&quot;text&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; size=&quot;10&quot; maxlength=&quot;10&quot; /&gt;</code>
1216 /// </summary>
1217 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
1218 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1219 /// <param name="size"><b>size</b> attribute value.</param>
1220 /// <param name="maxlength"><b>maxlength</b> attribute value.</param>
1221 /// <param name="attributes">Additional attributes for the <b>input</b> tag.</param>
1222 /// <returns>HTML string with text type <b>input</b> tag.</returns>
1223 /// <remarks>Calling <c>InputText( "nameArg", "valueArg", 10, 10, IDictionary )</c> results in:
1224 /// <code>&lt;input type=&quot;text&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; size=&quot;10&quot; maxlength=&quot;10&quot; key1=&quot;value1&quot; key2=&quot;value2&quot; /&gt;</code>
1225 /// <para>
1226 /// <paramref name="attributes"/> is used to generate additional attributes for the <b>label</b> tag.
1227 /// <see cref="IDictionary.Keys"/> are used to name attributes.
1228 /// <see cref="IDictionary.Values"/> are used to assign those attributes values.
1229 /// </para>
1230 /// </remarks>
1231 /// <example>This example shows how to use <b>InputText</b>:
1232 /// <code>
1233 /// $HtmlHelper.InputText( "nameArg", "valueArg", 10, 10, IDictionary )
1234 /// </code>
1235 /// </example>
1236 public virtual String InputText(String name, String value, int size, int maxlength, IDictionary attributes)
1238 return
1239 String.Format("<input type=\"text\" name=\"{0}\" id=\"{0}\" value=\"{1}\" size=\"{2}\" maxlength=\"{3}\" {4}/>",
1240 name, value, size, maxlength, GetAttributes(attributes));
1243 /// <summary>
1244 /// Creates an input element of the text type with custom <paramref name="name"/> and <paramref name="id"/>.
1245 /// <code>&lt;input type=&quot;text&quot; name=&quot;nameArg&quot; id=&quot;idArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1246 /// </summary>
1247 /// <param name="name"><b>name</b> attribute value.</param>
1248 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1249 /// <param name="id"><b>id</b> attribute value.</param>
1250 /// <returns>HTML string with text type <b>input</b> tag.</returns>
1251 /// <remarks>Calling <c>InputText( "nameArg", "valueArg", "idArg" )</c> results in:
1252 /// <code>&lt;input type=&quot;text&quot; name=&quot;nameArg&quot; id=&quot;idArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1253 /// </remarks>
1254 /// <example>This example shows how to use <b>InputText</b>:
1255 /// <code>
1256 /// $HtmlHelper.InputText( "nameArg", "valueArg", "idArg" )
1257 /// </code>
1258 /// </example>
1259 public virtual String InputText(String name, String value, String id)
1261 if (id == null) id = name;
1263 return String.Format("<input type=\"text\" name=\"{0}\" id=\"{1}\" value=\"{2}\" />", name, id, value);
1266 /// <overloads>This method has two overloads.</overloads>
1267 /// <summary>
1268 /// Creates a hidden type input element.
1269 /// <code>&lt;input type=&quot;hidden&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1270 /// </summary>
1271 public virtual String InputText(String name, String value, IDictionary attributes)
1273 return String.Format("<input type=\"text\" name=\"{0}\" id=\"{0}\" value=\"{1}\" {2}/>",
1274 name, value, GetAttributes(attributes));
1277 #endregion
1279 #region InputPassword
1281 /// <summary>
1282 /// Creates an input element of password type
1283 /// </summary>
1284 public virtual String InputPassword(String name)
1286 return InputPassword(name, null);
1289 /// <summary>
1290 /// Creates an input element of password type
1291 /// </summary>
1292 public virtual String InputPassword(String name, String value)
1294 return InputPassword(name, value, null);
1297 /// <summary>
1298 /// Creates an input element of password type
1299 /// </summary>
1300 public virtual String InputPassword(String name, String value, IDictionary attributes)
1302 if (value == null) value = String.Empty;
1304 return String.Format("<input type=\"password\" name=\"{0}\" id=\"{0}\" value=\"{1}\" {2}/>",
1305 name, value, GetAttributes(attributes));
1308 #endregion
1310 #region InputHidden
1312 /// <summary>
1313 /// Creates an input hidden element
1314 /// </summary>
1315 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
1316 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1317 /// <returns>HTML string with hidden type <b>input</b> tag.</returns>
1318 /// <remarks>Calling <c>InputHidden( "nameArg", "valueArg" )</c> results in:
1319 /// <code>&lt;input type=&quot;hidden&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;valueArg&quot; /&gt;</code>
1320 /// </remarks>
1321 /// <example>This example shows how to use <b>InputHidden</b>:
1322 /// <code>
1323 /// $HtmlHelper.InputHidden( "nameArg", "valueArg" )
1324 /// </code>
1325 /// </example>
1326 public virtual String InputHidden(String name, String value)
1328 return String.Format("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\" />", name, value);
1331 /// <summary>
1332 /// Creates a hidden type input element.
1333 /// <code>&lt;input type=&quot;hidden&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;object&quot; /&gt;</code>
1334 /// </summary>
1335 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
1336 /// <param name="value"><see cref="object"/> to supply <see cref="String"/> for <b>value</b> attribute.</param>
1337 /// <returns>HTML string with hidden type <b>input</b> tag.</returns>
1338 /// <remarks>Calling <c>InputHidden( "nameArg", object )</c> results in:
1339 /// <code>&lt;input type=&quot;hidden&quot; name=&quot;nameArg&quot; id=&quot;nameArg&quot; value=&quot;object&quot; /&gt;</code>
1340 /// <para>
1341 /// <see cref="String"/> for <b>value</b> attribute is retrieved from <paramref name="value"/>
1342 /// via <see cref="object.ToString"/>.
1343 /// </para>
1344 /// <para>If <paramref name="value"/> is <c>null</c> <see cref="String.Empty"/> is used as the <b>value</b>
1345 /// <see cref="String"/>.</para>
1346 /// </remarks>
1347 /// <example>This example shows how to use <b>InputHidden</b>:
1348 /// <code>
1349 /// $HtmlHelper.InputHidden( "nameArg", object )
1350 /// </code>
1351 /// </example>
1352 public virtual String InputHidden(String name, object value)
1354 return InputHidden(name, value != null ? value.ToString() : String.Empty);
1357 #endregion
1359 #region SubmitButton
1361 ///<overloads>This method has two overloads.</overloads>
1362 /// <summary>
1363 /// Creates a submit button.
1364 /// <code>&lt;input type=&quot;submit&quot; value=&quot;valueArg&quot; /&gt;</code>
1365 /// </summary>
1366 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1367 /// <returns>HTML string with submit type <b>input</b> tag.</returns>
1368 /// <remarks>Calling <c>SubmitButton( "valueArg" )</c> results in:
1369 /// <code>&lt;input type=&quot;submit&quot; value=&quot;valueArg&quot; /&gt;</code>
1370 /// </remarks>
1371 /// <example>This example shows how to use <b>SubmitButton</b>:
1372 /// <code>
1373 /// $HtmlHelper.SubmitButton( "valueArg" )
1374 /// </code>
1375 /// </example>
1376 public virtual String SubmitButton(String value)
1378 return SubmitButton(value, null);
1381 /// <summary>
1382 /// Creates a submit button.
1383 /// <code>&lt;input type=&quot;submit&quot; value=&quot;valueArg&quot; key1=&quot;value1&quot; key2=&quot;value2&quot; /&gt;</code>
1384 /// </summary>
1385 /// <param name="value"><see cref="String"/> for <b>value</b> attribute.</param>
1386 /// <param name="attributes">Additional attributes for the <b>input</b> tag.</param>
1387 /// <remarks>Calling <c>SubmitButton( "valueArg", IDictionary )</c> results in:
1388 /// <code>&lt;input type=&quot;submit&quot; value=&quot;valueArg&quot; key1=&quot;value1&quot; key2=&quot;value2&quot; /&gt;</code>
1389 /// <para>
1390 /// <paramref name="attributes"/> is used to generate additional attributes for the <b>label</b> tag.
1391 /// <see cref="IDictionary.Keys"/> are used to name attributes.
1392 /// <see cref="IDictionary.Values"/> are used to assign those attributes values.
1393 /// </para>
1394 /// </remarks>
1395 /// <example>This example shows how to use <b>SubmitButton</b>:
1396 /// <code>
1397 /// $HtmlHelper.SubmitButton( "valueArg", IDictionary )
1398 /// </code>
1399 /// </example>
1400 public virtual String SubmitButton(String value, IDictionary attributes)
1402 StringBuilder sb = new StringBuilder();
1403 StringWriter sbWriter = new StringWriter(sb);
1404 HtmlTextWriter writer = new HtmlTextWriter(sbWriter);
1406 writer.WriteBeginTag("input");
1407 writer.WriteAttribute("type", "submit");
1408 writer.WriteAttribute("value", value);
1409 writer.Write(" ");
1410 writer.Write(GetAttributes(attributes));
1411 writer.Write(HtmlTextWriter.SelfClosingTagEnd);
1412 writer.WriteLine();
1414 return sbWriter.ToString();
1417 #endregion
1419 #region Select
1421 ///<overloads>This method has two overloads.</overloads>
1422 /// <summary>
1423 /// Creates opening <b>select</b> tag.
1424 /// <code>&lt;select name=&quot;nameArg&quot; id=&quot;nameArg&quot;&gt;</code>
1425 /// </summary>
1426 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
1427 /// <returns>HTML string with opening <b>select</b> tag.</returns>
1428 /// <remarks>Calling <c>Select( "nameArg" )</c> results in:
1429 /// <code>&lt;select name=&quot;nameArg&quot; id=&quot;nameArg&quot;&gt;</code>
1430 /// </remarks>
1431 /// <example>This example shows how to use <b>Select</b> together with <see cref="EndSelect"/>:
1432 /// <code>
1433 /// $HtmlHelper.Select( "nameArg" )
1434 /// ...
1435 /// $HtmlHelper.EndSelect()
1436 /// </code>
1437 /// </example>
1438 public virtual String Select(String name)
1440 return String.Format("<select name=\"{0}\" id=\"{0}\">", name);
1443 /// <summary>
1444 /// Creates opening <b>select</b> tag.
1445 /// <code>&lt;select name=&quot;nameArg&quot; id=&quot;nameArg&quot; key1=&quot;value1&quot; key2=&quot;value2&quot; &gt;</code>
1446 /// </summary>
1447 /// <param name="name">Value for <b>name</b> and <b>id</b> attributes.</param>
1448 /// <param name="attributes">Additional attributes for the <b>select</b> tag.</param>
1449 /// <remarks>Calling <c>Select( "nameArg", IDictionary )</c> results in:
1450 /// <code>&lt;select name=&quot;nameArg&quot; id=&quot;nameArg&quot; key1=&quot;value1&quot; key2=&quot;value2&quot; &gt;</code>
1451 /// <para>
1452 /// <paramref name="attributes"/> is used to generate additional attributes for the <b>label</b> tag.
1453 /// <see cref="IDictionary.Keys"/> are used to name attributes.
1454 /// <see cref="IDictionary.Values"/> are used to assign those attributes values.
1455 /// </para>
1456 /// </remarks>
1457 /// <example>This example shows how to use <b>Select</b> together with <see cref="EndSelect"/>:
1458 /// <code>
1459 /// $HtmlHelper.Select( "nameArg", IDictionary )
1460 /// ...
1461 /// $HtmlHelper.EndSelect()
1462 /// </code>
1463 /// </example>
1464 public virtual String Select(String name, IDictionary attributes)
1466 String htmlAttrs = GetAttributes(attributes);
1468 return String.Format("<select name=\"{0}\" id=\"{0}\" {1}>", name, htmlAttrs);
1471 /// <summary>
1472 /// Creates a closing <b>select</b> tag.
1473 /// </summary>
1474 /// <remarks>Calling <c>EndSelect()</c> results in:
1475 /// <code>&lt;/select&gt;</code>
1476 /// </remarks>
1477 /// <example>This example shows how to use <see cref="Select(String)"/> together with <b>EndSelect</b>:
1478 /// <code>
1479 /// $HtmlHelper.Select( "nameArg" )
1480 /// ...
1481 /// $HtmlHelper.EndSelect()
1482 /// </code>
1483 /// </example>
1484 public virtual String EndSelect()
1486 return String.Format("</select>");
1489 #endregion
1491 #region Create options
1493 /// <summary>
1494 /// Creates an opening <b>optgroup</b> element.
1495 /// </summary>
1496 /// <param name="label">The label attribute.</param>
1497 /// <returns>An opening <b>optgroup</b> element.</returns>
1498 public virtual String OptionGroup(String label)
1500 return String.Format("<optgroup label=\"{0}\">", label);
1503 /// <summary>
1504 /// Creates a closing <b>optgroup</b> element.
1505 /// </summary>
1506 /// <returns>A closing <b>optgroup</b> element.</returns>
1507 public virtual String EndOptionGroup()
1509 return String.Format("</optgroup>");
1512 /// <summary>
1513 /// TODO: Document this!
1514 /// </summary>
1515 public virtual String CreateOption(String text, object value)
1517 return CreateOption(text, value, null);
1520 /// <summary>
1521 /// TODO: Document this!
1522 /// </summary>
1523 /// <remarks>
1524 /// Valid html attributes include: selected and disabled
1525 /// </remarks>
1526 public virtual String CreateOption(String text, object value, IDictionary htmlAttributes)
1528 if (value != null)
1530 return String.Format("<option {0} value=\"{1}\">{2}</option>", GetAttributes(htmlAttributes), value, text);
1532 else
1534 return String.Format("<option {0}>{1}</option>", GetAttributes(htmlAttributes), text);
1538 /// <summary>
1539 /// Creates <b>option</b> elements from <see cref="Array"/>. Marks the
1540 /// option that matches the <paramref name="selected"/> argument (if provided).
1541 /// <code>
1542 /// &lt;option&gt;0&lt;/option&gt;
1543 /// &lt;option&gt;1&lt;/option&gt;
1544 /// ...
1545 /// &lt;option&gt;5&lt;/option&gt;
1546 /// &lt;option selected&gt;selectedArg&lt;/option&gt;
1547 /// &lt;option&gt;object&lt;/option&gt;
1548 /// </code>
1549 /// </summary>
1550 /// <param name="elems">Array of values for each <b>option</b> tag.</param>
1551 /// <param name="selected">Name of the <b>option</b> tag to mark selected.</param>
1552 /// <returns>HTML string with array of <b>option</b> tags.</returns>
1553 /// <remarks>Calling <c>CreateOptionsFromPrimitiveArray( Array, "selectedArg" )</c> results in:
1554 /// <code>
1555 /// &lt;option&gt;0&lt;/option&gt;
1556 /// &lt;option&gt;1&lt;/option&gt;
1557 /// &lt;option&gt;2&lt;/option&gt;
1558 /// &lt;option&gt;3&lt;/option&gt;
1559 /// &lt;option&gt;4&lt;/option&gt;
1560 /// &lt;option&gt;5&lt;/option&gt;
1561 /// &lt;option selected&gt;selectedArg&lt;/option&gt;
1562 /// &lt;option&gt;object&lt;/option&gt;
1563 /// </code>
1564 /// <para>
1565 /// Elements in the array are converted to <see cref="String"/> using <see cref="StringBuilder.AppendFormat(String,object)"/>.
1566 /// </para>
1567 /// </remarks>
1568 /// <example>This example shows how to use <b>CreateOptionsFromPrimitiveArray</b>:
1569 /// <code>
1570 /// $HtmlHelper.CreateOptionsFromPrimitiveArray( Array, "selectedArg" )
1571 /// </code>
1572 /// </example>
1573 public virtual String CreateOptionsFromPrimitiveArray(Array elems, String selected)
1575 if (elems.GetLength(0) == 0) return String.Empty;
1577 StringBuilder sb = new StringBuilder();
1579 foreach(object elem in elems)
1581 sb.AppendFormat("\t<option{0}>{1}</option>\r\n",
1582 elem.ToString() == selected ? " selected=\"selected\"" : "", elem);
1585 return sb.ToString();
1588 ///<overloads>This method has two overloads.</overloads>
1589 /// <summary>
1590 /// Creates options elements from an <see cref="Array"/>.
1591 /// <code>
1592 /// &lt;option value=&quot;valueProp&quot;&gt;textProp&lt;/option&gt;
1593 /// &lt;option value=&quot;0&quot;&gt;textProp2&lt;/option&gt;
1594 /// &lt;option value=&quot;5&quot;&gt;textProp3&lt;/option&gt;
1595 /// </code>
1596 /// <seealso cref="CreateOptions(ICollection,String,String)"/>
1597 /// </summary>
1598 /// <param name="elems">Collection of objects each of which describes an <b>option</b> tag.</param>
1599 /// <param name="textProperty">Name of the <paramref name="elems"/>
1600 /// objects property with the value for each <b>option</b> tag's
1601 /// text.</param>
1602 /// <param name="valueProperty">Name of the <paramref
1603 /// name="elems"/> objects property with the value for each
1604 /// <b>option</b> tag's <b>value</b> attribute value.</param>
1605 /// <returns>HTML string with array of <b>option</b> tags.</returns>
1606 ///
1607 /// <remarks>Calling <c>CreateOptionsFromArray( Array, "textPropertyArg",
1608 /// "valuePropertyArg", object )</c> with specific type objects results in:
1609 /// <code>
1610 /// &lt;option value=&quot;valueProp&quot;&gt;textProp&lt;/option&gt;
1611 /// &lt;option value=&quot;0&quot;&gt;textProp2&lt;/option&gt;
1612 /// &lt;option value=&quot;5&quot;&gt;textProp3&lt;/option&gt;
1613 /// </code>
1614 /// <para>Calling <c>CreateOptionsFromArray( Array, "textPropertyArg",
1615 /// "valuePropertyArg", object )</c> with random type objects results in:
1616 /// <code>
1617 /// &lt;option&gt;0&lt;/option&gt;
1618 /// &lt;option&gt;1&lt;/option&gt;
1619 /// &lt;option&gt;2&lt;/option&gt;
1620 /// &lt;option&gt;3&lt;/option&gt;
1621 /// &lt;option&gt;4&lt;/option&gt;
1622 /// &lt;option&gt;5&lt;/option&gt;
1623 /// &lt;option&gt;object&lt;/option&gt;
1624 /// &lt;option&gt;MR.Logic.Controllers.HtmlHelperController+SampleClass&lt;/option&gt;
1625 /// </code>
1626 /// Notice that the last <b>option</b> was generated from an object of the type
1627 /// with the properties specified by <paramref
1628 /// name="textProperty"/> and <pararef name="valueProperty"/>, but the method
1629 /// is already in the mode of working with random type objects.
1630 /// <note>Explanation bellow describes two different modes of working with the method.</note>
1631 /// </para>
1632 /// <para>There are two possible usages of the method depending on
1633 /// the types of <see cref="object"/>s which can be present in
1634 /// <paramref name="elems"/>:
1635 /// <list type="definition">
1636 /// <item>
1637 /// <term>Random type objects</term>
1638 /// <description>Array is full of
1639 /// random type objects. Properties specified by <paramref
1640 /// name="textProperty"/> and <pararef name="valueProperty"/> aren't
1641 /// used. Instead <b>value</b> argument is ommited and <see
1642 /// cref="Object.ToString"/> is invoked on each item in <paramref
1643 /// name="elems"/> to retrieve text for an <b>option</b> tag.
1644 /// </description>
1645 /// </item>
1646 /// <item>
1647 /// <term>Single type objects</term>
1648 /// <description>Array is objects
1649 /// of one time. In this case <paramref name="textProperty"/> and
1650 /// <paramref name="valueProperty"/> can specify the names of the
1651 /// properties of that type to use for <b>option</b> tags
1652 /// generation.
1653 /// </description>
1654 /// </item>
1655 /// </list>
1656 /// <note>You cannot mix <i>random type objects</i> and <i>specific type objects</i>.
1657 /// <b>CreateOptionsFromArray</b>
1658 /// is looking at the first item in the <paramref name="elems"/>
1659 /// collection to get <see cref="MethodInfo"/> to access specified
1660 /// properties. If usage is mixed either an unexpected exception will be
1661 /// thrown or options will have unexpected strings.
1662 /// </note>
1663 /// </para>
1664 /// <para><b>CreateOptionsFromArray</b> relies on <see cref="CreateOptions(ICollection,String,String)"/> to generate
1665 /// all <b>option</b> tags.</para>
1666 /// </remarks>
1667 /// <example>This example shows how to use <b>CreateOptions</b>:
1668 /// <code>
1669 /// $HtmlHelper.CreateOptionsFromArray( ICollection, "textPropertyArg", "valuePropertyArg" )
1670 /// </code>
1671 /// </example>
1672 public virtual String CreateOptionsFromArray(Array elems, String textProperty, String valueProperty)
1674 return CreateOptions(elems, textProperty, valueProperty);
1677 /// <summary>
1678 /// Creates options elements from an <see cref="Array"/>.
1679 /// <code>
1680 /// &lt;option value=&quot;valueProp&quot; selected&gt;textProp&lt;/option&gt;
1681 /// &lt;option value=&quot;0&quot;&gt;textProp2&lt;/option&gt;
1682 /// &lt;option value=&quot;5&quot;&gt;textProp3&lt;/option&gt;
1683 /// </code>
1684 /// <seealso cref="CreateOptions(ICollection,String,String,object)"/>
1685 /// </summary>
1686 /// <param name="elems">Collection of objects each of which describes an <b>option</b> tag.</param>
1687 /// <param name="textProperty">Name of the <paramref name="elems"/>
1688 /// objects property with the value for each <b>option</b> tag's
1689 /// text.</param>
1690 /// <param name="valueProperty">Name of the <paramref
1691 /// name="elems"/> objects property with the value for each
1692 /// <b>option</b> tag's <b>value</b> attribute value.</param>
1693 /// <param name="selectedValue"><see cref="object"/> indicating which
1694 /// <b>option</b> tag is to be marked with <b>selected</b>
1695 /// attribute.</param>
1696 /// <returns>HTML string with array of <b>option</b> tags.</returns>
1697 ///
1698 /// <remarks>Calling <c>CreateOptionsFromArray( Array, "textPropertyArg",
1699 /// "valuePropertyArg", object )</c> with specific type objects results in:
1700 /// <code>
1701 /// &lt;option value=&quot;valueProp&quot; selected&gt;textProp&lt;/option&gt;
1702 /// &lt;option value=&quot;0&quot;&gt;textProp2&lt;/option&gt;
1703 /// &lt;option value=&quot;5&quot;&gt;textProp3&lt;/option&gt;
1704 /// </code>
1705 /// <para>Calling <c>CreateOptionsFromArray( Array, "textPropertyArg",
1706 /// "valuePropertyArg", object )</c> with random type objects results in:
1707 /// <code>
1708 /// &lt;option&gt;0&lt;/option&gt;
1709 /// &lt;option&gt;1&lt;/option&gt;
1710 /// &lt;option&gt;2&lt;/option&gt;
1711 /// &lt;option&gt;3&lt;/option&gt;
1712 /// &lt;option&gt;4&lt;/option&gt;
1713 /// &lt;option&gt;5&lt;/option&gt;
1714 /// &lt;option selected&gt;object&lt;/option&gt;
1715 /// &lt;option&gt;MR.Logic.Controllers.HtmlHelperController+SampleClass&lt;/option&gt;
1716 /// </code>
1717 /// Notice that the last <b>option</b> was generated from an object of the type
1718 /// with the properties specified by <paramref
1719 /// name="textProperty"/> and <pararef name="valueProperty"/>, but the method
1720 /// is already in the mode of working with random type objects.
1721 /// <note>Explanation bellow describes two different modes of working with the method.</note>
1722 /// </para>
1723 /// <para>There are two possible usages of the method depending on
1724 /// the types of <see cref="object"/>s which can be present in
1725 /// <paramref name="elems"/>:
1726 /// <list type="definition">
1727 /// <item>
1728 /// <term>Random type objects</term>
1729 /// <description>Array is full of
1730 /// random type objects. Properties specified by <paramref
1731 /// name="textProperty"/> and <pararef name="valueProperty"/> aren't
1732 /// used. Instead <b>value</b> argument is ommited and <see
1733 /// cref="Object.ToString"/> is invoked on each item in <paramref
1734 /// name="elems"/> to retrieve text for an <b>option</b> tag.
1735 /// </description>
1736 /// </item>
1737 /// <item>
1738 /// <term>Single type objects</term>
1739 /// <description>Array is objects
1740 /// of one time. In this case <paramref name="textProperty"/> and
1741 /// <paramref name="valueProperty"/> can specify the names of the
1742 /// properties of that type to use for <b>option</b> tags
1743 /// generation.
1744 /// </description>
1745 /// </item>
1746 /// </list>
1747 /// <note>You cannot mix <i>random type objects</i> and <i>specific type objects</i>.
1748 /// <b>CreateOptionsFromArray</b>
1749 /// is looking at the first item in the <paramref name="elems"/>
1750 /// collection to get <see cref="MethodInfo"/> to access specified
1751 /// properties. If usage is mixed either an unexpected exception will be
1752 /// thrown or options will have unexpected strings.
1753 /// </note>
1754 /// </para>
1755 /// <para><b>CreateOptionsFromArray</b> relies on <see cref="CreateOptions(ICollection,String,String,object)"/> to generate
1756 /// all <b>option</b> tags.</para>
1757 /// </remarks>
1758 /// <example>This example shows how to use <b>CreateOptions</b>:
1759 /// <code>
1760 /// $HtmlHelper.CreateOptionsFromArray( ICollection, "textPropertyArg", "valuePropertyArg", object )
1761 /// </code>
1762 /// </example>
1763 public virtual String CreateOptionsFromArray(Array elems, String textProperty, String valueProperty, object selectedValue)
1765 return CreateOptions(elems, textProperty, valueProperty, selectedValue);
1768 ///<overloads>This method has two overloads.</overloads>
1769 /// <summary>
1770 /// Creates options elements from an <see cref="ICollection"/>.
1771 /// <code>
1772 /// &lt;option value=&quot;valueProp&quot;&gt;textProp&lt;/option&gt;
1773 /// &lt;option value=&quot;0&quot;&gt;textProp2&lt;/option&gt;
1774 /// &lt;option value=&quot;5&quot;&gt;textProp3&lt;/option&gt;
1775 /// </code>
1776 /// </summary>
1777 /// <param name="elems">Collection of objects each of which describes an <b>option</b> tag.</param>
1778 /// <param name="textProperty">Name of the <paramref name="elems"/>
1779 /// objects property with the value for each <b>option</b> tag's
1780 /// text.</param>
1781 /// <param name="valueProperty">Name of the <paramref
1782 /// name="elems"/> objects property with the value for each
1783 /// <b>option</b> tag's <b>value</b> attribute value.</param>
1784 /// <returns>HTML string with array of <b>option</b> tags.</returns>
1785 ///
1786 /// <remarks>Calling <c>CreateOptions( ICollection, "textPropertyArg",
1787 /// "valuePropertyArg", object )</c> with specific type objects results in:
1788 /// <code>
1789 /// &lt;option value=&quot;valueProp&quot;&gt;textProp&lt;/option&gt;
1790 /// &lt;option value=&quot;0&quot;&gt;textProp2&lt;/option&gt;
1791 /// &lt;option value=&quot;5&quot;&gt;textProp3&lt;/option&gt;
1792 /// </code>
1793 /// <para>Calling <c>CreateOptions( ICollection, "textPropertyArg",
1794 /// "valuePropertyArg", object )</c> with random type objects results in:
1795 /// <code>
1796 /// &lt;option&gt;0&lt;/option&gt;
1797 /// &lt;option&gt;1&lt;/option&gt;
1798 /// &lt;option&gt;2&lt;/option&gt;
1799 /// &lt;option&gt;3&lt;/option&gt;
1800 /// &lt;option&gt;4&lt;/option&gt;
1801 /// &lt;option&gt;5&lt;/option&gt;
1802 /// &lt;option&gt;object&lt;/option&gt;
1803 /// &lt;option&gt;MR.Logic.Controllers.HtmlHelperController+SampleClass&lt;/option&gt;
1804 /// </code>
1805 /// Notice that the last <b>option</b> was generated from an object of the type
1806 /// with the properties specified by <paramref
1807 /// name="textProperty"/> and <pararef name="valueProperty"/>, but the method
1808 /// is already in the mode of working with random type objects.
1809 /// <note>Explanation bellow describes two different modes of working with the method.</note>
1810 /// </para>
1811 /// <para>There are two possible usages of the method depending on
1812 /// the types of <see cref="object"/>s which can be present in
1813 /// <paramref name="elems"/>:
1814 /// <list type="definition">
1815 /// <item>
1816 /// <term>Random type objects</term>
1817 /// <description>Array is full of
1818 /// random type objects. Properties specified by <paramref
1819 /// name="textProperty"/> and <pararef name="valueProperty"/> aren't
1820 /// used. Instead <b>value</b> argument is ommited and <see
1821 /// cref="Object.ToString"/> is invoked on each item in <paramref
1822 /// name="elems"/> to retrieve text for an <b>option</b> tag.
1823 /// </description>
1824 /// </item>
1825 /// <item>
1826 /// <term>Single type objects</term>
1827 /// <description>Array is objects
1828 /// of one time. In this case <paramref name="textProperty"/> and
1829 /// <paramref name="valueProperty"/> can specify the names of the
1830 /// properties of that type to use for <b>option</b> tags
1831 /// generation.
1832 /// </description>
1833 /// </item>
1834 /// </list>
1835 /// <note>You cannot mix <i>random type objects</i> and <i>specific type objects</i>. <b>CreateOptions</b>
1836 /// is looking at the first item in the <paramref name="elems"/>
1837 /// collection to get <see cref="MethodInfo"/> to access specified
1838 /// properties. If usage is mixed either an unexpected exception will be
1839 /// thrown or options will have unexpected strings.
1840 /// </note>
1841 /// </para>
1842 /// </remarks>
1843 /// <example>This example shows how to use <b>CreateOptions</b>:
1844 /// <code>
1845 /// $HtmlHelper.CreateOptions( ICollection, "textPropertyArg", "valuePropertyArg" )
1846 /// </code>
1847 /// </example>
1848 public virtual String CreateOptions(ICollection elems, String textProperty, String valueProperty)
1850 return CreateOptions(elems, textProperty, valueProperty, null);
1853 /// <summary>
1854 /// Creates options elements from an <see cref="ICollection"/>.
1855 /// <code>
1856 /// &lt;option value=&quot;valueProp&quot; selected&gt;textProp&lt;/option&gt;
1857 /// &lt;option value=&quot;0&quot;&gt;textProp2&lt;/option&gt;
1858 /// &lt;option value=&quot;5&quot;&gt;textProp3&lt;/option&gt;
1859 /// </code>
1860 /// </summary>
1861 /// <param name="elems">Collection of objects each of which describes an <b>option</b> tag.</param>
1862 /// <param name="textProperty">Name of the <paramref name="elems"/>
1863 /// objects property with the value for each <b>option</b> tag's
1864 /// text.</param>
1865 /// <param name="valueProperty">Name of the <paramref
1866 /// name="elems"/> objects property with the value for each
1867 /// <b>option</b> tag's <b>value</b> attribute value.</param>
1868 /// <param name="selectedValue"><see cref="object"/> indicating which
1869 /// <b>option</b> tag is to be marked with <b>selected</b>
1870 /// attribute.</param>
1871 /// <returns>HTML string with array of <b>option</b> tags.</returns>
1872 ///
1873 /// <remarks>Calling <c>CreateOptions( ICollection, "textPropertyArg",
1874 /// "valuePropertyArg", object )</c> with specific type objects results in:
1875 /// <code>
1876 /// &lt;option value=&quot;valueProp&quot; selected&gt;textProp&lt;/option&gt;
1877 /// &lt;option value=&quot;0&quot;&gt;textProp2&lt;/option&gt;
1878 /// &lt;option value=&quot;5&quot;&gt;textProp3&lt;/option&gt;
1879 /// </code>
1880 /// <para>Calling <c>CreateOptions( ICollection, "textPropertyArg",
1881 /// "valuePropertyArg", object )</c> with random type objects results in:
1882 /// <code>
1883 /// &lt;option&gt;0&lt;/option&gt;
1884 /// &lt;option&gt;1&lt;/option&gt;
1885 /// &lt;option&gt;2&lt;/option&gt;
1886 /// &lt;option&gt;3&lt;/option&gt;
1887 /// &lt;option&gt;4&lt;/option&gt;
1888 /// &lt;option&gt;5&lt;/option&gt;
1889 /// &lt;option selected&gt;object&lt;/option&gt;
1890 /// &lt;option&gt;MR.Logic.Controllers.HtmlHelperController+SampleClass&lt;/option&gt;
1891 /// </code>
1892 /// Notice that the last <b>option</b> was generated from an object of the type
1893 /// with the properties specified by <paramref
1894 /// name="textProperty"/> and <pararef name="valueProperty"/>, but the method
1895 /// is already in the mode of working with random type objects.
1896 /// <note>Explanation bellow describes two different modes of working with the method.</note>
1897 /// </para>
1898 /// <para>There are two possible usages of the method depending on
1899 /// the types of <see cref="object"/>s which can be present in
1900 /// <paramref name="elems"/>:
1901 /// <list type="definition">
1902 /// <item>
1903 /// <term>Random type objects</term>
1904 /// <description>Array is full of
1905 /// random type objects. Properties specified by <paramref
1906 /// name="textProperty"/> and <pararef name="valueProperty"/> aren't
1907 /// used. Instead <b>value</b> argument is ommited and <see
1908 /// cref="Object.ToString"/> is invoked on each item in <paramref
1909 /// name="elems"/> to retrieve text for an <b>option</b> tag.
1910 /// </description>
1911 /// </item>
1912 /// <item>
1913 /// <term>Single type objects</term>
1914 /// <description>Array is objects
1915 /// of one time. In this case <paramref name="textProperty"/> and
1916 /// <paramref name="valueProperty"/> can specify the names of the
1917 /// properties of that type to use for <b>option</b> tags
1918 /// generation.
1919 /// </description>
1920 /// </item>
1921 /// </list>
1922 /// <note>You cannot mix <i>random type objects</i> and <i>specific type objects</i>.
1923 /// <b>CreateOptions</b>
1924 /// is looking at the first item in the <paramref name="elems"/>
1925 /// collection to get <see cref="MethodInfo"/> to access specified
1926 /// properties. If usage is mixed either an unexpected exception will be
1927 /// thrown or options will have unexpected strings.
1928 /// </note>
1929 /// </para>
1930 /// </remarks>
1931 /// <example>This example shows how to use <b>CreateOptions</b>:
1932 /// <code>
1933 /// $HtmlHelper.CreateOptions( ICollection, "textPropertyArg", "valuePropertyArg", object )
1934 /// </code>
1935 /// </example>
1936 public virtual String CreateOptions(ICollection elems, String textProperty, String valueProperty, object selectedValue)
1938 if (elems == null) throw new ArgumentNullException("elems");
1940 if (elems.Count == 0) return String.Empty;
1942 IEnumerator enumerator = elems.GetEnumerator();
1943 enumerator.MoveNext();
1944 object guidanceElem = enumerator.Current;
1946 bool isMultiple = (selectedValue != null && selectedValue.GetType().IsArray);
1948 MethodInfo defaultValueMethodInfo = GetMethod(guidanceElem, valueProperty);
1949 MethodInfo defaultTextMethodInfo = null;
1951 if (textProperty != null)
1953 defaultTextMethodInfo = GetMethod(guidanceElem, textProperty);
1956 StringBuilder sb = new StringBuilder();
1958 foreach(object elem in elems)
1960 if (elem == null) continue;
1962 object value = null;
1964 MethodInfo valueMethodInfo = defaultValueMethodInfo;
1965 MethodInfo textMethodInfo = defaultTextMethodInfo;
1967 if (elem.GetType() != guidanceElem.GetType())
1969 if (valueProperty != null)
1971 valueMethodInfo = GetMethod(elem, valueProperty);
1973 if (textProperty != null)
1975 textMethodInfo = GetMethod(elem, textProperty);
1979 if (valueMethodInfo != null) value = valueMethodInfo.Invoke(elem, null);
1981 object text = textMethodInfo != null ? textMethodInfo.Invoke(elem, null) : elem.ToString();
1983 if (value != null)
1985 bool selected = IsSelected(value, selectedValue, isMultiple);
1987 sb.AppendFormat("\t<option {0} value=\"{1}\">{2}</option>\r\n",
1988 selected ? "selected=\"selected\"" : "", value, text);
1990 else
1992 bool selected = IsSelected(text, selectedValue, isMultiple);
1994 sb.AppendFormat("\t<option {0} >{1}</option>\r\n",
1995 selected ? "selected=\"selected\"" : "", text);
1999 return sb.ToString();
2002 /// <summary>
2003 /// Determines whether the specified value is selected.
2004 /// </summary>
2005 /// <param name="value">Value to be tested.</param>
2006 /// <param name="selectedValue">Selected value.</param>
2007 /// <param name="isMultiple"><see langword="true"/> if <paramref name="selectedValue"/> is
2008 /// <see cref="Type.IsArray"/>; otherwise, <see langword="false"/>.</param>
2009 /// <returns>
2010 /// <see langword="true"/> if the specified <paramref name="value"/> is selected; otherwise, <see langword="false"/>.
2011 /// </returns>
2012 /// <remarks>Specified <paramref name="value"/> is selected if it <see cref="Object.Equals(object)"/>
2013 /// to the <paramref name="selectedValue"/>. Or if <paramref name="selectedValue"/> is an
2014 /// array <paramref name="value"/> is selected if <see cref="Array.IndexOf(Array, object)"/> can find it
2015 /// in <paramref name="selectedValue"/>.</remarks>
2016 private static bool IsSelected(object value, object selectedValue, bool isMultiple)
2018 if (!isMultiple)
2020 return value.Equals(selectedValue);
2022 else
2024 return Array.IndexOf((Array) selectedValue, value) != -1;
2028 /// <summary>
2029 /// Gets the property get method.
2030 /// </summary>
2031 /// <param name="elem">Object specifying the type for which to get the method.</param>
2032 /// <param name="property">Property name.</param>
2033 /// <returns><see cref="MethodInfo"/> to be used to retrieve the property value.
2034 /// If <paramref name="property"/> is <c>null</c> <c>null</c> is returned.</returns>
2035 /// <remarks>This method is used to get the <see cref="MethodInfo"/> to retrieve
2036 /// specified property from the specified type.</remarks>
2037 /// <exception cref="ArgumentNullException">Thrown is <paramref name="elem"/> is <c>null</c>.</exception>
2038 private static MethodInfo GetMethod(object elem, String property)
2040 if (elem == null) throw new ArgumentNullException("elem");
2041 if (property == null) return null;
2043 return
2044 elem.GetType().GetMethod("get_" + property, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
2047 #endregion
2049 #region List
2051 ///<overloads>This method has two overloads.</overloads>
2052 /// <summary>
2053 /// Builds an unordered <b>ul</b> list from supplied <see cref="ICollection"/>.
2054 /// <code>
2055 /// &lt;ul&gt;
2056 /// &lt;li&gt;0&lt;/li&gt;
2057 /// ...
2058 /// &lt;li&gt;object&lt;/li&gt;
2059 /// &lt;/ul&gt;
2060 /// </code>
2061 /// </summary>
2062 /// <param name="elements">Collection with items to use for the list generation.</param>
2063 /// <returns>HTML string with <b>ul</b> tag list.</returns>
2064 /// <remarks>Calling <c>BuildUnorderedList( ICollection )</c> results in:
2065 /// <code>
2066 /// &lt;ul&gt;
2067 /// &lt;li&gt;0&lt;/li&gt;
2068 /// &lt;li&gt;1&lt;/li&gt;
2069 /// &lt;li&gt;2&lt;/li&gt;
2070 /// &lt;li&gt;3&lt;/li&gt;
2071 /// &lt;li&gt;4&lt;/li&gt;
2072 /// &lt;li&gt;5&lt;/li&gt;
2073 /// &lt;li&gt;object&lt;/li&gt;
2074 /// &lt;/ul&gt;
2075 /// </code>
2076 /// <para>Items in <paramref name="elements"/> are converted to string through
2077 /// <see cref="Object.ToString"/>.</para>
2078 /// </remarks>
2079 /// <example>This example shows how to use <b>BuildUnorderedList</b>:
2080 /// <code>
2081 /// $HtmlHelper.BuildUnorderedList( ICollection )
2082 /// </code>
2083 /// </example>
2084 public virtual String BuildUnorderedList(ICollection elements)
2086 return BuildUnorderedList(elements, null, null);
2089 /// <summary>
2090 /// Builds an unordered <b>ul</b> list from supplied <see cref="ICollection"/> with
2091 /// <b>ul</b> and <b>li</b> tags CSS class set to supplied attributes.
2092 /// <code>
2093 /// &lt;ol class=&quot;styleClassArg&quot;&gt;
2094 /// &lt;li class=&quot;itemClassArg&quot;&gt;0&lt;/li&gt;
2095 /// ...
2096 /// &lt;li class=&quot;itemClassArg&quot;&gt;object&lt;/li&gt;
2097 /// &lt;/ol&gt;
2098 /// </code>
2099 /// </summary>
2100 /// <param name="elements">Collection with items to use for the list generation.</param>
2101 /// <param name="styleClass">CSS class name of the list <b>ul</b> tag.</param>
2102 /// <param name="itemClass">CSS class name of the list item <b>li</b> tag.</param>
2103 /// <returns>HTML string with <b>ul</b> tag list.</returns>
2104 /// <remarks>Calling <c>BuildUnorderedList( ICollection, "styleClassArg", "itemClassArg" )</c> results in:
2105 /// <code>
2106 /// &lt;ol class=&quot;styleClassArg&quot;&gt;
2107 /// &lt;li class=&quot;itemClassArg&quot;&gt;0&lt;/li&gt;
2108 /// &lt;li class=&quot;itemClassArg&quot;&gt;1&lt;/li&gt;
2109 /// &lt;li class=&quot;itemClassArg&quot;&gt;2&lt;/li&gt;
2110 /// &lt;li class=&quot;itemClassArg&quot;&gt;3&lt;/li&gt;
2111 /// &lt;li class=&quot;itemClassArg&quot;&gt;4&lt;/li&gt;
2112 /// &lt;li class=&quot;itemClassArg&quot;&gt;5&lt;/li&gt;
2113 /// &lt;li class=&quot;itemClassArg&quot;&gt;object&lt;/li&gt;
2114 /// &lt;/ol&gt;
2115 /// </code>
2116 /// <para>Items in <paramref name="elements"/> are converted to string through
2117 /// <see cref="Object.ToString"/>.</para>
2118 /// </remarks>
2119 /// <example>This example shows how to use <b>BuildOrderedList</b>:
2120 /// <code>
2121 /// $HtmlHelper.BuildUnorderedList( ICollection, "styleClassArg", "itemClassArg" )
2122 /// </code>
2123 /// </example>
2124 public virtual String BuildUnorderedList(ICollection elements, String styleClass, String itemClass)
2126 return BuildList("ul", elements, styleClass, itemClass);
2129 ///<overloads>This method has two overloads.</overloads>
2130 /// <summary>
2131 /// Builds an ordered <b>ol</b> list from supplied <see cref="ICollection"/>.
2132 /// <code>
2133 /// &lt;ol&gt;
2134 /// &lt;li&gt;0&lt;/li&gt;
2135 /// ...
2136 /// &lt;li&gt;object&lt;/li&gt;
2137 /// &lt;/ol&gt;
2138 /// </code>
2139 /// </summary>
2140 /// <param name="elements">Collection with items to use for the list generation.</param>
2141 /// <returns>HTML string with <b>ol</b> tag list.</returns>
2142 /// <remarks>Calling <c>BuildOrderedList( ICollection )</c> results in:
2143 /// <code>
2144 /// &lt;ol&gt;
2145 /// &lt;li&gt;0&lt;/li&gt;
2146 /// &lt;li&gt;1&lt;/li&gt;
2147 /// &lt;li&gt;2&lt;/li&gt;
2148 /// &lt;li&gt;3&lt;/li&gt;
2149 /// &lt;li&gt;4&lt;/li&gt;
2150 /// &lt;li&gt;5&lt;/li&gt;
2151 /// &lt;li&gt;object&lt;/li&gt;
2152 /// &lt;/ol&gt;
2153 /// </code>
2154 /// <para>Items in <paramref name="elements"/> are converted to string through
2155 /// <see cref="Object.ToString"/>.</para>
2156 /// </remarks>
2157 /// <example>This example shows how to use <b>BuildOrderedList</b>:
2158 /// <code>
2159 /// $HtmlHelper.BuildOrderedList( ICollection )
2160 /// </code>
2161 /// </example>
2162 public virtual String BuildOrderedList(ICollection elements)
2164 return BuildOrderedList(elements, null, null);
2167 /// <summary>
2168 /// Builds an ordered <b>ol</b> list from supplied <see cref="ICollection"/> with
2169 /// <b>ol</b> and <b>li</b> tags CSS class set to supplied attributes.
2170 /// <code>
2171 /// &lt;ol class=&quot;styleClassArg&quot;&gt;
2172 /// &lt;li class=&quot;itemClassArg&quot;&gt;0&lt;/li&gt;
2173 /// ...
2174 /// &lt;li class=&quot;itemClassArg&quot;&gt;object&lt;/li&gt;
2175 /// &lt;/ol&gt;
2176 /// </code>
2177 /// </summary>
2178 /// <param name="elements">Collection with items to use for the list generation.</param>
2179 /// <param name="styleClass">CSS class name of the list <b>ol</b> tag.</param>
2180 /// <param name="itemClass">CSS class name of the list item <b>li</b> tag.</param>
2181 /// <returns>HTML string with <b>ol</b> tag list.</returns>
2182 /// <remarks>Calling <c>BuildOrderedList( ICollection, "styleClassArg", "itemClassArg" )</c> results in:
2183 /// <code>
2184 /// &lt;ol class=&quot;styleClassArg&quot;&gt;
2185 /// &lt;li class=&quot;itemClassArg&quot;&gt;0&lt;/li&gt;
2186 /// &lt;li class=&quot;itemClassArg&quot;&gt;1&lt;/li&gt;
2187 /// &lt;li class=&quot;itemClassArg&quot;&gt;2&lt;/li&gt;
2188 /// &lt;li class=&quot;itemClassArg&quot;&gt;3&lt;/li&gt;
2189 /// &lt;li class=&quot;itemClassArg&quot;&gt;4&lt;/li&gt;
2190 /// &lt;li class=&quot;itemClassArg&quot;&gt;5&lt;/li&gt;
2191 /// &lt;li class=&quot;itemClassArg&quot;&gt;object&lt;/li&gt;
2192 /// &lt;/ol&gt;
2193 /// </code>
2194 /// <para>Items in <paramref name="elements"/> are converted to string through
2195 /// <see cref="Object.ToString"/>.</para>
2196 /// </remarks>
2197 /// <example>This example shows how to use <b>BuildOrderedList</b>:
2198 /// <code>
2199 /// $HtmlHelper.BuildOrderedList( ICollection, "styleClassArg", "itemClassArg" )
2200 /// </code>
2201 /// </example>
2202 public virtual String BuildOrderedList(ICollection elements, String styleClass, String itemClass)
2204 return BuildList("ol", elements, styleClass, itemClass);
2207 /// <summary>
2208 /// Builds a list with list tag specified by <paramref name="tag"/>
2209 /// from supplied <see cref="ICollection"/> with
2210 /// list tag and <b>li</b> tags CSS class set to supplied attributes.
2211 /// <code>
2212 /// &lt;listTag class=&quot;styleClassArg&quot;&gt;
2213 /// &lt;li class=&quot;itemClassArg&quot;&gt;0&lt;/li&gt;
2214 /// ...
2215 /// &lt;li class=&quot;itemClassArg&quot;&gt;object&lt;/li&gt;
2216 /// &lt;/listTag&gt;
2217 /// </code>
2218 /// </summary>
2219 /// <param name="tag">List tag name.</param>
2220 /// <param name="elements">Collection with items to use for the list generation.</param>
2221 /// <param name="styleClass">CSS class name of the list <b>ol</b> tag.</param>
2222 /// <param name="itemClass">CSS class name of the list item <b>li</b> tag.</param>
2223 /// <returns>HTML string with list of the requested type.</returns>
2224 /// <remarks>This method is can be used to generate custom type HTML list.
2225 /// Currently HTML support only two types of lists ordered (<b>ol</b> tag) and unodered
2226 /// (<b>ul</b>tag). In general this method should be used by other methods responsible
2227 /// for constructing some specific list.
2228 /// <para>
2229 /// Calling <c>BuildList( "listTag", ICollection, "styleClassArg", "itemClassArg" )</c> results in:
2230 /// <code>
2231 /// &lt;listTag class=&quot;styleClassArg&quot;&gt;
2232 /// &lt;li class=&quot;itemClassArg&quot;&gt;0&lt;/li&gt;
2233 /// &lt;li class=&quot;itemClassArg&quot;&gt;1&lt;/li&gt;
2234 /// &lt;li class=&quot;itemClassArg&quot;&gt;2&lt;/li&gt;
2235 /// &lt;li class=&quot;itemClassArg&quot;&gt;3&lt;/li&gt;
2236 /// &lt;li class=&quot;itemClassArg&quot;&gt;4&lt;/li&gt;
2237 /// &lt;li class=&quot;itemClassArg&quot;&gt;5&lt;/li&gt;
2238 /// &lt;li class=&quot;itemClassArg&quot;&gt;object&lt;/li&gt;
2239 /// &lt;/listTag&gt;
2240 /// </code>
2241 /// </para>
2242 /// <para>Items in <paramref name="elements"/> are converted to string through
2243 /// <see cref="Object.ToString"/>.</para>
2244 /// </remarks>
2245 /// <example>This example shows how to use <b>BuildList</b>:
2246 /// <code>
2247 /// BuildList("ol", elements, styleClass, itemClass);
2248 /// </code>
2249 /// </example>
2250 private static String BuildList(String tag, ICollection elements, String styleClass, String itemClass)
2252 StringBuilder sb = new StringBuilder();
2253 StringWriter sbWriter = new StringWriter(sb);
2254 HtmlTextWriter writer = new HtmlTextWriter(sbWriter);
2256 writer.WriteBeginTag(tag);
2258 if (styleClass != null)
2260 writer.WriteAttribute("class", styleClass);
2263 writer.Write(HtmlTextWriter.TagRightChar);
2264 writer.WriteLine();
2266 foreach(object item in elements)
2268 if (item == null) continue;
2270 writer.WriteLine(BuildListItem(item.ToString(), itemClass));
2273 writer.WriteEndTag(tag);
2274 writer.WriteLine();
2276 return sbWriter.ToString();
2279 /// <summary>
2280 /// Generates a list item <b>li</b> tag.
2281 /// <code>
2282 /// &lt;li class=&quot;itemClassArg&quot;&gt;object&lt;/li&gt;
2283 /// </code>
2284 /// </summary>
2285 /// <param name="item">Item text.</param>
2286 /// <param name="itemClass">Item CSS class name.</param>
2287 /// <returns>HTML string with a single <b>li</b> tag.</returns>
2288 /// <remarks>This method should be used to assist list generation.
2289 /// <para>
2290 /// Calling <c>BuildListItem( "object", "itemClassArg" )</c> results in:
2291 /// <code>
2292 /// &lt;li class=&quot;itemClassArg&quot;&gt;object&lt;/li&gt;
2293 /// </code>
2294 /// </para>
2295 /// </remarks>
2296 /// <example>This example shows how to use <b>BuildListItem</b>:
2297 /// <code>
2298 /// BuildListItem(item.ToString(), itemClass);
2299 /// </code>
2300 /// </example>
2301 private static String BuildListItem(String item, String itemClass)
2303 if (itemClass == null)
2305 return String.Format("<li>{0}</li>", item);
2307 else
2309 return String.Format("<li class=\"{0}\">{1}</li>", itemClass, item);
2313 #endregion