1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
20 using System
.Collections
;
21 using System
.Reflection
;
23 using Castle
.MonoRail
.Framework
.Internal
;
26 /// Provides usefull common methods to generate HTML tags.
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.
31 public class HtmlHelper
: AbstractHelper
35 /// Initializes a new instance of the <see cref="HtmlHelper"/> class.
37 public HtmlHelper() { }
39 /// Initializes a new instance of the <see cref="HtmlHelper"/> class.
40 /// setting the Controller, Context and ControllerContext.
42 /// <param name="engineContext">The engine context.</param>
43 public HtmlHelper(IEngineContext engineContext
) : base(engineContext
) { }
49 /// Creates a <b>fieldset</b> tag with a legend.
51 /// <fieldset><legend>legendArg</legend>
53 /// <seealso cref="HtmlHelper.EndFieldSet"/>
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><fieldset><legend>legendArg</legend></code>
60 /// <example>This example shows how to use <see cref="FieldSet"/> together with <see cref="EndFieldSet"/>:
62 /// $HtmlHelper.FieldSet( "legendArg" )
64 /// $HtmlHelper.EndFieldSet()
67 public virtual String
FieldSet(String legend
)
69 return String
.Format("<fieldset><legend>{0}</legend>", legend
);
73 /// Creates a closing <b>fieldset</b> tag.
77 /// <seealso cref="HtmlHelper.FieldSet"/>
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></fieldset></code>
84 /// <example>This example shows how to use <see cref="FieldSet"/> together with <see cref="EndFieldSet"/>:
86 /// $HtmlHelper.FieldSet( "legendArg" )
88 /// $HtmlHelper.EndFieldSet()
91 public virtual String
EndFieldSet()
98 #region Form and FormTo
100 ///<overloads>This method has three overloads.</overloads>
102 /// Creates a <b>form</b> tag with "<b>post</b>" method and specified <paramref name="action"/>.
104 /// <form method="post" action="actionArg">
106 /// <seealso cref="HtmlHelper.EndForm"/>
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><form method="post" action="actionArg"></code>
113 /// <example>This example shows how to use <see cref="Form(String)"/> together with <see cref="EndForm"/>:
115 /// $HtmlHelper.Form( "actionArg" )
117 /// $HtmlHelper.EndForm()
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
);
133 return sbWriter
.ToString();
137 /// Creates a <b>form</b> tag with the specified <paramref name="method"/>, <paramref name="action"/> and
138 /// <paramref name="id"/> attributes.
140 /// <form method="methodArg" action="actionArg" id="idArg">
142 /// <seealso cref="HtmlHelper.EndForm"/>
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><form method="methodArg" action="actionArg" id="idArg"></code>
151 /// <example>This example shows how to use <b>Form</b> together with <see cref="EndForm"/>:
153 /// $HtmlHelper.Form( "actionArg", "idArg", "methodArg" )
155 /// $HtmlHelper.EndForm()
158 public virtual String
Form(String action
, String id
, String method
)
160 return Form(action
, id
, method
, null);
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.
167 /// <form method="methodArg" action="actionArg" id="idArg" onsubmit="onSubmitArg">
169 /// <seealso cref="HtmlHelper.EndForm"/>
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><form method="methodArg" action="actionArg" id="idArg" onsubmit="onSubmitArg"></code>
179 /// <example>This example shows how to use <b>Form</b> together with <see cref="EndForm"/>:
181 /// $HtmlHelper.Form( "actionArg", "idArg", "methodArg", "submitHandler()" )
183 /// $HtmlHelper.EndForm()
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
);
202 return sbWriter
.ToString();
206 /// Creates a <b>form</b> tag with the specified <paramref name="action"/> attribute.
208 /// <form action="actionArg">
210 /// <seealso cref="HtmlHelper.EndForm"/>
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
));
221 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkTo(String, String)"/> methods.
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);
231 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkTo(String, String)"/> methods.
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);
242 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkTo(String, String)"/> methods.
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);
254 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkToAttributed(String, String, String, IDictionary)"/> methods.
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
);
266 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkToAttributed(String, String, String, IDictionary)"/> methods.
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
);
279 /// Creates a <b>form</b> tag targeting a URL in the style of the <see cref="LinkToAttributed(String, String, String, IDictionary)"/> methods.
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
));
293 formAction
+= "?id=" + id
;
301 if (attributes
== null)
303 attributes
= DictHelper
.Create("method=" + method
);
307 attributes
["method"] = method
;
310 return Form(formAction
, attributes
);
314 /// Creates a closing <b>form</b> tag.
318 /// <seealso cref="Form(String)"/>
320 /// <returns>HTML string with form closing tag.</returns>
322 /// Calling <c>EndForm()</c> results in:
323 /// <code></form></code>
325 /// <example>This example shows how to use <see cref="Form(String,String,String,String)"/> together with <see cref="EndForm"/>:
327 /// $HtmlHelper.Form( "actionArg", "idArg", "methodArg", "submitHandler()" )
329 /// $HtmlHelper.EndForm()
332 public virtual String
EndForm()
339 #region Link and LinkTo and LinkToWithPost
341 ///<overloads>This method has two overloads.</overloads>
343 /// Creates an anchor (link) to the <paramref name="target"/>
345 /// <a href="/sometarget.html">linkText</a>
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><a href="something.html">something</a></code>
354 /// <example>This example shows how to use <b>Link</b>:
356 /// $HtmlHelper.Link( "mypage.html", "This is a link to my page" )
359 public virtual String
Link(String target
, String linkText
)
361 return LinkTo(target
, linkText
);
365 /// Creates an anchor (link) to the <paramref name="target"/>
367 /// <a href="/sometarget.html">linkText</a>
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><a href="something.html" class="mylinkclass">something</a></code>
377 /// <example>This example shows how to use <b>Link</b>:
379 /// $HtmlHelper.Link( "mypage.html", "This is a link to my page", $DictHelper.CreateDict("class=mylinkclass") )
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>
390 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller.
392 /// <a href="/website/currentController/actionArg.rails">nameArg</a>
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><a href="/websiter/currentController/actionArg.rails">nameArg</a></code>
401 /// <example>This example shows how to use <b>LinkTo</b>:
403 /// $HtmlHelper.LinkTo( "linkName", "requiredAction" )
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>
415 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller.
417 /// <a href="/website/currentController/actionArg.rails">nameArg</a>
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><a href="/websiter/home/index.rails">nameArg</a></code>
426 /// <example>This example shows how to use <b>LinkTo</b>:
428 /// $HtmlHelper.LinkTo( "linkName", DictHelper.CreateDict("controller=home","action=index") )
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
));
444 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/>.
446 /// <a href="/website/controllerArg/actionArg.rails">nameArg</a>
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><a href="/website/controllerArg/actionArg.rails">nameArg</a></code>
457 /// <example>This example shows how to use <b>LinkTo</b>:
459 /// $HtmlHelper.LinkTo( "linkName", {@action:} )
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
);
470 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/>
471 /// passing provided <paramref name="id"/>.
473 /// <a href="/website/controllerArg/actionArg.rails?id=objectId">nameArg</a>
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><a href="/website/controllerArg/actionArg.rails?id=object">nameArg</a></code>
485 /// <see cref="String.Format(String, object)"/> is used to convert <paramref name="id"/> to the actual <see cref="String"/>.</para>
487 /// <example>This example shows how to use <b>LinkTo</b>:
489 /// $HtmlHelper.LinkTo( "linkName", "someController", "requiredAction", objectToRefByID )
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
);
500 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/>
502 /// <a href="/website/controllerArg/actionArg.rails">nameArg</a>
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><a href="/website/controllerArg/actionArg.rails">nameArg</a></code>
513 /// <example>This example shows how to use <b>LinkToAttributed</b>:
515 /// $HtmlHelper.LinkToAttributed( "linkName", "someController", "requiredAction", $DictHelper.CreateDict("class=something") )
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
));
526 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/>
528 /// <a href="/website/controllerArg/actionArg.rails?id=x">nameArg</a>
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><a href="/website/controllerArg/actionArg.rails">nameArg</a></code>
540 /// <example>This example shows how to use <b>LinkToAttributed</b>:
542 /// $HtmlHelper.LinkToAttributed( "linkName", "someController", "requiredAction", $DictHelper.CreateDict("class=something") )
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
));
553 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller that posts
554 /// using a hidden form element.
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);
565 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller that posts
566 /// using a hidden form element.
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
);
578 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller that posts
579 /// using a hidden form element.
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
);
591 /// Creates an anchor (link) to the <paramref name="action"/> on the current controller that posts
592 /// using a hidden form element.
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
);
605 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/> that posts
606 /// using a hidden form element.
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);
619 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/> that posts
620 /// using a hidden form element.
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
);
633 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/> that posts
634 /// using a hidden form element.
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);
648 /// Creates an anchor (link) to the <paramref name="action"/> on the specified <paramref name="controller"/> that posts
649 /// using a hidden form element.
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
);
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.
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
);
693 attributes
["onclick"] = onclickAttribute
;
696 StringBuilder stringBuilder
= new StringBuilder();
700 stringBuilder
.Append(FormToAttributed(controller
, action
, id
, formAttributes
));
701 stringBuilder
.Append(LinkToAttributed(name
, controller
, action
, id
, attributes
));
705 stringBuilder
.Append(FormToAttributed(controller
, action
, formAttributes
));
706 stringBuilder
.Append(LinkToAttributed(name
, controller
, action
, attributes
));
709 stringBuilder
.Append(EndForm());
711 return stringBuilder
.ToString();
719 /// Maps <paramref name="target"/> to website virtual path.
720 /// <code>/website/targetArg</code>
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>
727 /// <example>This example shows how to use <see cref="MapToVirtual"/>:
729 /// $HtmlHelper.MapToVirtual( "targetFolder/targetFile.html" )
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
);
745 ///<overloads>This method has two overloads.</overloads>
747 /// Creates a label for the element indicated with
748 /// <paramref name="forId"/>.
750 /// <label for="forIdArg">labelArg</label>
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><label for="forIdArg">labelArg</label></code>
759 /// <example>This example shows how to use <see cref="LabelFor(String,String)"/>:
761 /// $HtmlHelper.LabelFor( "forIdArg", "labelArg" )
764 public virtual String
LabelFor(String forId
, String label
)
766 return LabelFor(forId
, label
, null);
770 /// Creates a label for the element indicated with
771 /// <paramref name="forId"/>.
773 /// <label key1="value1" key2="value2" for="forIdArg">labelArg</label>
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><label key5="value5" key4="value4" key1="value1" key3="value3" key2="value2" for="forIdArg">labelArg</label></code>
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.
788 /// <example>This example shows how to use <see cref="LabelFor(String,String,IDictionary)"/>:
790 /// $HtmlHelper.LabelFor( "forIdArg", "labelArg", IDictionary )
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");
801 writer
.Write(GetAttributes(attributes
));
802 writer
.WriteAttribute("for", forId
);
803 writer
.Write(HtmlTextWriter
.TagRightChar
);
805 writer
.WriteEndTag("label");
808 return sbWriter
.ToString();
815 ///<overloads>This method has two overloads.</overloads>
817 /// Creates three <b>select</b> tags to input day, month and year.
819 /// <select name="nameArgday" id="nameArgday" > ... </select>
820 /// <select name="nameArgmonth" id="nameArgmonth" > ... </select>
821 /// <select name="nameArgyear" id="nameArgyear" > ... </select>
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:
829 /// <select name="nameArgday" id="nameArgday" > <option>1</option>
830 /// <option>2</option>
832 /// <option>14</option>
833 /// <option selected>15</option>
834 /// <option>16</option>
836 /// <option>30</option>
837 /// <option>31</option>
838 /// </select> <select name="nameArgmonth" id="nameArgmonth" > <option>1</option>
839 /// <option>2</option>
841 /// <option>6</option>
842 /// <option selected>7</option>
843 /// <option>8</option>
845 /// <option>11</option>
846 /// <option>12</option>
847 /// </select> <select name="nameArgyear" id="nameArgyear" > <option>1930</option>
848 /// <option>1931</option>
850 /// <option>2004</option>
851 /// <option selected>2005</option>
852 /// <option>2006</option>
854 /// <option>2029</option>
855 /// </select></code>
856 /// As above example shows the year range is hardcoded between 1930 and 2029.
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.
863 /// <example>This example shows how to use <b>DateTime</b>:
865 /// $HtmlHelper.DateTime( "nameArg", new DateTime( 2005, 07, 15 ) )
868 public virtual String
DateTime(String name
, DateTime
value)
870 return DateTime(name
, value, null);
874 /// Creates three <b>select</b> tags to input day, month and year.
876 /// <select name="nameArgday" id="nameArgday" key1="value1" key3="value3" key2="value2" > ... </select>
877 /// <select name="nameArgmonth" id="nameArgmonth" key1="value1" key3="value3" key2="value2" > ... </select>
878 /// <select name="nameArgyear" id="nameArgyear" key1="value1" key3="value3" key2="value2" > ... </select>
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:
887 /// <select name="nameArgday" id="nameArgday" key1="value1" key2="value2" > <option>1</option>
888 /// <option>2</option>
890 /// <option>14</option>
891 /// <option selected>15</option>
892 /// <option>16</option>
894 /// <option>30</option>
895 /// <option>31</option>
896 /// </select> <select name="nameArgmonth" id="nameArgmonth" key1="value1" key2="value2" > <option>1</option>
897 /// <option>2</option>
899 /// <option>6</option>
900 /// <option selected>7</option>
901 /// <option>8</option>
903 /// <option>11</option>
904 /// <option>12</option>
905 /// </select> <select name="nameArgyear" id="nameArgyear" key1="value1" key2="value2" > <option>1930</option>
906 /// <option>1931</option>
908 /// <option>2004</option>
909 /// <option selected>2005</option>
910 /// <option>2006</option>
912 /// <option>2029</option>
913 /// </select></code>
914 /// As above example shows the year range is hardcoded between 1930 and 2029.
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.
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.
926 /// <example>This example shows how to use <b>DateTime</b>:
928 /// $HtmlHelper.DateTime( "nameArg", new DateTime( 2005, 07, 15 ), IDictionary )
931 public virtual String
DateTime(String name
, DateTime
value, IDictionary attributes
)
933 String
[] days
= new String
[31];
935 for(int i
= 1; i
< 32; i
++)
936 days
[index
++] = i
.ToString();
938 String
[] months
= new String
[12];
940 for(int i
= 1; i
< 13; i
++)
941 months
[index
++] = i
.ToString();
943 String
[] years
= new String
[100];
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());
954 sb
.Append(Select(name
+ "month", attributes
));
955 sb
.Append(CreateOptionsFromPrimitiveArray(months
, value.Month
.ToString()));
956 sb
.Append(EndSelect());
958 sb
.Append(Select(name
+ "year", attributes
));
959 sb
.Append(CreateOptionsFromPrimitiveArray(years
, value.Year
.ToString()));
960 sb
.Append(EndSelect());
962 return sb
.ToString();
970 /// Creates a text area element.
971 /// <code><textarea id="nameArg" name="nameArg" cols="10" rows="10">valueArg</textarea></code>
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><textarea id="nameArg" name="nameArg" cols="10" rows="10">valueArg</textarea></code>
981 /// <example>This example shows how to use <see cref="TextArea"/>:
983 /// $HtmlHelper.TextArea( "nameArg", 10, 20, "Text inside text area." )
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);
996 /// <overloads>This method has three overloads.</overloads>
998 /// Creates an input element of the button type.
999 /// <code><input type="button" value="valueArg" /></code>
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><input type="button" name="valueArg" value="valueArg" /></code>
1006 /// <example>This example shows how to use <b>InputButton</b>:
1008 /// $HtmlHelper.InputButton( "valueArg" )
1011 public virtual String
InputButton(String
value)
1013 return InputButton(value, value);
1017 /// Creates an input element of the button type.
1018 /// <code><input type="button" name="nameArg" id="nameArg" value="valueArg" /></code>
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);
1029 /// Creates an input element of the button type.
1030 /// <code><input type="button" name="nameArg" id="nameArg" value="valueArg" /></code>
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
));
1044 #region InputCheckbox
1046 /// <overloads>This method has three overloads.</overloads>
1048 /// Creates an input element of the checkbox type.
1049 /// <code><input type="checkbox" name="nameArg" id="nameArg" value="valueArg" /></code>
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><input type="checkbox" name="name" id="name" value="1" /></code>
1057 public virtual String
InputCheckbox(String name
, Object
value)
1059 return InputCheckbox(name
, value, null);
1063 /// Creates an input element of the checkbox type.
1064 /// <code><input type="checkbox" name="nameArg" id="nameArg" value="valueArg" /></code>
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;
1076 attributes
= new Hashtable();
1077 attributes
["checked"] = null;
1080 return InputCheckbox(name
, value, attributes
);
1084 /// Creates an input element of the checkbox type.
1085 /// <code><input type="checkbox" name="nameArg" id="nameArg" value="valueArg" /></code>
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
));
1101 /// <overloads>This method has two overloads.</overloads>
1103 /// Creates an input element of the radio type.
1104 /// <code><input type="radio" name="nameArg" value="valueArg" /></code>
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><input type="radio" name="name" value="1" /></code>
1112 public virtual String
InputRadio(String name
, Object
value)
1114 return InputRadio(name
, value, null);
1118 /// Creates an input element of the radio type.
1119 /// <code><input type="radio" name="nameArg" value="valueArg" /></code>
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
));
1135 /// <overloads>This method has two overloads.</overloads>
1137 /// Creates an input element of the file type.
1138 /// <code><input type="file" name="nameArg" /></code>
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><input type="file" name="name" /></code>
1145 public virtual String
InputFile(String name
)
1147 return InputFile(name
, null);
1151 /// Creates an input element of the file type.
1152 /// <code><input type="file" name="nameArg" /></code>
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
));
1167 /// <overloads>This method has four overloads.</overloads>
1169 /// Creates an input element of the text type.
1170 /// <code><input type="text" name="nameArg" id="nameArg" value="valueArg" /></code>
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><input type="text" name="nameArg" id="nameArg" value="valueArg" /></code>
1178 /// <example>This example shows how to use <b>InputText</b>:
1180 /// $HtmlHelper.InputText( "nameArg", "valueArg" )
1183 public virtual String
InputText(String name
, String
value)
1185 return InputText(name
, value, name
);
1189 /// Creates an input element of the text type of specified
1190 /// <paramref name="size"/> and <paramref name="maxlength"/>.
1191 /// <code><input type="text" name="nameArg" id="nameArg" value="valueArg" size="10" maxlength="10" /></code>
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><input type="text" name="nameArg" id="nameArg" value="valueArg" size="10" maxlength="10" /></code>
1201 /// <example>This example shows how to use <b>InputText</b>:
1203 /// $HtmlHelper.InputText( "nameArg", "valueArg", 10, 10 )
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
);
1213 /// Creates an input element of the text type with specified
1214 /// <paramref name="size"/>, <paramref name="maxlength"/> and <paramref name="attributes"/>.
1215 /// <code><input type="text" name="nameArg" id="nameArg" value="valueArg" size="10" maxlength="10" /></code>
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><input type="text" name="nameArg" id="nameArg" value="valueArg" size="10" maxlength="10" key1="value1" key2="value2" /></code>
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.
1231 /// <example>This example shows how to use <b>InputText</b>:
1233 /// $HtmlHelper.InputText( "nameArg", "valueArg", 10, 10, IDictionary )
1236 public virtual String
InputText(String name
, String
value, int size
, int maxlength
, IDictionary attributes
)
1239 String
.Format("<input type=\"text\" name=\"{0}\" id=\"{0}\" value=\"{1}\" size=\"{2}\" maxlength=\"{3}\" {4}/>",
1240 name
, value, size
, maxlength
, GetAttributes(attributes
));
1244 /// Creates an input element of the text type with custom <paramref name="name"/> and <paramref name="id"/>.
1245 /// <code><input type="text" name="nameArg" id="idArg" value="valueArg" /></code>
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><input type="text" name="nameArg" id="idArg" value="valueArg" /></code>
1254 /// <example>This example shows how to use <b>InputText</b>:
1256 /// $HtmlHelper.InputText( "nameArg", "valueArg", "idArg" )
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>
1268 /// Creates a hidden type input element.
1269 /// <code><input type="hidden" name="nameArg" id="nameArg" value="valueArg" /></code>
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
));
1279 #region InputPassword
1282 /// Creates an input element of password type
1284 public virtual String
InputPassword(String name
)
1286 return InputPassword(name
, null);
1290 /// Creates an input element of password type
1292 public virtual String
InputPassword(String name
, String
value)
1294 return InputPassword(name
, value, null);
1298 /// Creates an input element of password type
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
));
1313 /// Creates an input hidden element
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><input type="hidden" name="nameArg" id="nameArg" value="valueArg" /></code>
1321 /// <example>This example shows how to use <b>InputHidden</b>:
1323 /// $HtmlHelper.InputHidden( "nameArg", "valueArg" )
1326 public virtual String
InputHidden(String name
, String
value)
1328 return String
.Format("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\" />", name
, value);
1332 /// Creates a hidden type input element.
1333 /// <code><input type="hidden" name="nameArg" id="nameArg" value="object" /></code>
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><input type="hidden" name="nameArg" id="nameArg" value="object" /></code>
1341 /// <see cref="String"/> for <b>value</b> attribute is retrieved from <paramref name="value"/>
1342 /// via <see cref="object.ToString"/>.
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>
1347 /// <example>This example shows how to use <b>InputHidden</b>:
1349 /// $HtmlHelper.InputHidden( "nameArg", object )
1352 public virtual String
InputHidden(String name
, object value)
1354 return InputHidden(name
, value != null ? value.ToString() : String
.Empty
);
1359 #region SubmitButton
1361 ///<overloads>This method has two overloads.</overloads>
1363 /// Creates a submit button.
1364 /// <code><input type="submit" value="valueArg" /></code>
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><input type="submit" value="valueArg" /></code>
1371 /// <example>This example shows how to use <b>SubmitButton</b>:
1373 /// $HtmlHelper.SubmitButton( "valueArg" )
1376 public virtual String
SubmitButton(String
value)
1378 return SubmitButton(value, null);
1382 /// Creates a submit button.
1383 /// <code><input type="submit" value="valueArg" key1="value1" key2="value2" /></code>
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><input type="submit" value="valueArg" key1="value1" key2="value2" /></code>
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.
1395 /// <example>This example shows how to use <b>SubmitButton</b>:
1397 /// $HtmlHelper.SubmitButton( "valueArg", IDictionary )
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);
1410 writer
.Write(GetAttributes(attributes
));
1411 writer
.Write(HtmlTextWriter
.SelfClosingTagEnd
);
1414 return sbWriter
.ToString();
1421 ///<overloads>This method has two overloads.</overloads>
1423 /// Creates opening <b>select</b> tag.
1424 /// <code><select name="nameArg" id="nameArg"></code>
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><select name="nameArg" id="nameArg"></code>
1431 /// <example>This example shows how to use <b>Select</b> together with <see cref="EndSelect"/>:
1433 /// $HtmlHelper.Select( "nameArg" )
1435 /// $HtmlHelper.EndSelect()
1438 public virtual String
Select(String name
)
1440 return String
.Format("<select name=\"{0}\" id=\"{0}\">", name
);
1444 /// Creates opening <b>select</b> tag.
1445 /// <code><select name="nameArg" id="nameArg" key1="value1" key2="value2" ></code>
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><select name="nameArg" id="nameArg" key1="value1" key2="value2" ></code>
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.
1457 /// <example>This example shows how to use <b>Select</b> together with <see cref="EndSelect"/>:
1459 /// $HtmlHelper.Select( "nameArg", IDictionary )
1461 /// $HtmlHelper.EndSelect()
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
);
1472 /// Creates a closing <b>select</b> tag.
1474 /// <remarks>Calling <c>EndSelect()</c> results in:
1475 /// <code></select></code>
1477 /// <example>This example shows how to use <see cref="Select(String)"/> together with <b>EndSelect</b>:
1479 /// $HtmlHelper.Select( "nameArg" )
1481 /// $HtmlHelper.EndSelect()
1484 public virtual String
EndSelect()
1486 return String
.Format("</select>");
1491 #region Create options
1494 /// Creates an opening <b>optgroup</b> element.
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
);
1504 /// Creates a closing <b>optgroup</b> element.
1506 /// <returns>A closing <b>optgroup</b> element.</returns>
1507 public virtual String
EndOptionGroup()
1509 return String
.Format("</optgroup>");
1513 /// TODO: Document this!
1515 public virtual String
CreateOption(String text
, object value)
1517 return CreateOption(text
, value, null);
1521 /// TODO: Document this!
1524 /// Valid html attributes include: selected and disabled
1526 public virtual String
CreateOption(String text
, object value, IDictionary htmlAttributes
)
1530 return String
.Format("<option {0} value=\"{1}\">{2}</option>", GetAttributes(htmlAttributes
), value, text
);
1534 return String
.Format("<option {0}>{1}</option>", GetAttributes(htmlAttributes
), text
);
1539 /// Creates <b>option</b> elements from <see cref="Array"/>. Marks the
1540 /// option that matches the <paramref name="selected"/> argument (if provided).
1542 /// <option>0</option>
1543 /// <option>1</option>
1545 /// <option>5</option>
1546 /// <option selected>selectedArg</option>
1547 /// <option>object</option>
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:
1555 /// <option>0</option>
1556 /// <option>1</option>
1557 /// <option>2</option>
1558 /// <option>3</option>
1559 /// <option>4</option>
1560 /// <option>5</option>
1561 /// <option selected>selectedArg</option>
1562 /// <option>object</option>
1565 /// Elements in the array are converted to <see cref="String"/> using <see cref="StringBuilder.AppendFormat(String,object)"/>.
1568 /// <example>This example shows how to use <b>CreateOptionsFromPrimitiveArray</b>:
1570 /// $HtmlHelper.CreateOptionsFromPrimitiveArray( Array, "selectedArg" )
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>
1590 /// Creates options elements from an <see cref="Array"/>.
1592 /// <option value="valueProp">textProp</option>
1593 /// <option value="0">textProp2</option>
1594 /// <option value="5">textProp3</option>
1596 /// <seealso cref="CreateOptions(ICollection,String,String)"/>
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
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>
1607 /// <remarks>Calling <c>CreateOptionsFromArray( Array, "textPropertyArg",
1608 /// "valuePropertyArg", object )</c> with specific type objects results in:
1610 /// <option value="valueProp">textProp</option>
1611 /// <option value="0">textProp2</option>
1612 /// <option value="5">textProp3</option>
1614 /// <para>Calling <c>CreateOptionsFromArray( Array, "textPropertyArg",
1615 /// "valuePropertyArg", object )</c> with random type objects results in:
1617 /// <option>0</option>
1618 /// <option>1</option>
1619 /// <option>2</option>
1620 /// <option>3</option>
1621 /// <option>4</option>
1622 /// <option>5</option>
1623 /// <option>object</option>
1624 /// <option>MR.Logic.Controllers.HtmlHelperController+SampleClass</option>
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>
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">
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.
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
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.
1664 /// <para><b>CreateOptionsFromArray</b> relies on <see cref="CreateOptions(ICollection,String,String)"/> to generate
1665 /// all <b>option</b> tags.</para>
1667 /// <example>This example shows how to use <b>CreateOptions</b>:
1669 /// $HtmlHelper.CreateOptionsFromArray( ICollection, "textPropertyArg", "valuePropertyArg" )
1672 public virtual String
CreateOptionsFromArray(Array elems
, String textProperty
, String valueProperty
)
1674 return CreateOptions(elems
, textProperty
, valueProperty
);
1678 /// Creates options elements from an <see cref="Array"/>.
1680 /// <option value="valueProp" selected>textProp</option>
1681 /// <option value="0">textProp2</option>
1682 /// <option value="5">textProp3</option>
1684 /// <seealso cref="CreateOptions(ICollection,String,String,object)"/>
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
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>
1698 /// <remarks>Calling <c>CreateOptionsFromArray( Array, "textPropertyArg",
1699 /// "valuePropertyArg", object )</c> with specific type objects results in:
1701 /// <option value="valueProp" selected>textProp</option>
1702 /// <option value="0">textProp2</option>
1703 /// <option value="5">textProp3</option>
1705 /// <para>Calling <c>CreateOptionsFromArray( Array, "textPropertyArg",
1706 /// "valuePropertyArg", object )</c> with random type objects results in:
1708 /// <option>0</option>
1709 /// <option>1</option>
1710 /// <option>2</option>
1711 /// <option>3</option>
1712 /// <option>4</option>
1713 /// <option>5</option>
1714 /// <option selected>object</option>
1715 /// <option>MR.Logic.Controllers.HtmlHelperController+SampleClass</option>
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>
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">
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.
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
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.
1755 /// <para><b>CreateOptionsFromArray</b> relies on <see cref="CreateOptions(ICollection,String,String,object)"/> to generate
1756 /// all <b>option</b> tags.</para>
1758 /// <example>This example shows how to use <b>CreateOptions</b>:
1760 /// $HtmlHelper.CreateOptionsFromArray( ICollection, "textPropertyArg", "valuePropertyArg", object )
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>
1770 /// Creates options elements from an <see cref="ICollection"/>.
1772 /// <option value="valueProp">textProp</option>
1773 /// <option value="0">textProp2</option>
1774 /// <option value="5">textProp3</option>
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
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>
1786 /// <remarks>Calling <c>CreateOptions( ICollection, "textPropertyArg",
1787 /// "valuePropertyArg", object )</c> with specific type objects results in:
1789 /// <option value="valueProp">textProp</option>
1790 /// <option value="0">textProp2</option>
1791 /// <option value="5">textProp3</option>
1793 /// <para>Calling <c>CreateOptions( ICollection, "textPropertyArg",
1794 /// "valuePropertyArg", object )</c> with random type objects results in:
1796 /// <option>0</option>
1797 /// <option>1</option>
1798 /// <option>2</option>
1799 /// <option>3</option>
1800 /// <option>4</option>
1801 /// <option>5</option>
1802 /// <option>object</option>
1803 /// <option>MR.Logic.Controllers.HtmlHelperController+SampleClass</option>
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>
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">
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.
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
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.
1843 /// <example>This example shows how to use <b>CreateOptions</b>:
1845 /// $HtmlHelper.CreateOptions( ICollection, "textPropertyArg", "valuePropertyArg" )
1848 public virtual String
CreateOptions(ICollection elems
, String textProperty
, String valueProperty
)
1850 return CreateOptions(elems
, textProperty
, valueProperty
, null);
1854 /// Creates options elements from an <see cref="ICollection"/>.
1856 /// <option value="valueProp" selected>textProp</option>
1857 /// <option value="0">textProp2</option>
1858 /// <option value="5">textProp3</option>
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
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>
1873 /// <remarks>Calling <c>CreateOptions( ICollection, "textPropertyArg",
1874 /// "valuePropertyArg", object )</c> with specific type objects results in:
1876 /// <option value="valueProp" selected>textProp</option>
1877 /// <option value="0">textProp2</option>
1878 /// <option value="5">textProp3</option>
1880 /// <para>Calling <c>CreateOptions( ICollection, "textPropertyArg",
1881 /// "valuePropertyArg", object )</c> with random type objects results in:
1883 /// <option>0</option>
1884 /// <option>1</option>
1885 /// <option>2</option>
1886 /// <option>3</option>
1887 /// <option>4</option>
1888 /// <option>5</option>
1889 /// <option selected>object</option>
1890 /// <option>MR.Logic.Controllers.HtmlHelperController+SampleClass</option>
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>
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">
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.
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
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.
1931 /// <example>This example shows how to use <b>CreateOptions</b>:
1933 /// $HtmlHelper.CreateOptions( ICollection, "textPropertyArg", "valuePropertyArg", object )
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();
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
);
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();
2003 /// Determines whether the specified value is selected.
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>
2010 /// <see langword="true"/> if the specified <paramref name="value"/> is selected; otherwise, <see langword="false"/>.
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
)
2020 return value.Equals(selectedValue
);
2024 return Array
.IndexOf((Array
) selectedValue
, value) != -1;
2029 /// Gets the property get method.
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;
2044 elem
.GetType().GetMethod("get_" + property
, BindingFlags
.Instance
| BindingFlags
.Public
| BindingFlags
.IgnoreCase
);
2051 ///<overloads>This method has two overloads.</overloads>
2053 /// Builds an unordered <b>ul</b> list from supplied <see cref="ICollection"/>.
2056 /// <li>0</li>
2058 /// <li>object</li>
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:
2067 /// <li>0</li>
2068 /// <li>1</li>
2069 /// <li>2</li>
2070 /// <li>3</li>
2071 /// <li>4</li>
2072 /// <li>5</li>
2073 /// <li>object</li>
2076 /// <para>Items in <paramref name="elements"/> are converted to string through
2077 /// <see cref="Object.ToString"/>.</para>
2079 /// <example>This example shows how to use <b>BuildUnorderedList</b>:
2081 /// $HtmlHelper.BuildUnorderedList( ICollection )
2084 public virtual String
BuildUnorderedList(ICollection elements
)
2086 return BuildUnorderedList(elements
, null, null);
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.
2093 /// <ol class="styleClassArg">
2094 /// <li class="itemClassArg">0</li>
2096 /// <li class="itemClassArg">object</li>
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:
2106 /// <ol class="styleClassArg">
2107 /// <li class="itemClassArg">0</li>
2108 /// <li class="itemClassArg">1</li>
2109 /// <li class="itemClassArg">2</li>
2110 /// <li class="itemClassArg">3</li>
2111 /// <li class="itemClassArg">4</li>
2112 /// <li class="itemClassArg">5</li>
2113 /// <li class="itemClassArg">object</li>
2116 /// <para>Items in <paramref name="elements"/> are converted to string through
2117 /// <see cref="Object.ToString"/>.</para>
2119 /// <example>This example shows how to use <b>BuildOrderedList</b>:
2121 /// $HtmlHelper.BuildUnorderedList( ICollection, "styleClassArg", "itemClassArg" )
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>
2131 /// Builds an ordered <b>ol</b> list from supplied <see cref="ICollection"/>.
2134 /// <li>0</li>
2136 /// <li>object</li>
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:
2145 /// <li>0</li>
2146 /// <li>1</li>
2147 /// <li>2</li>
2148 /// <li>3</li>
2149 /// <li>4</li>
2150 /// <li>5</li>
2151 /// <li>object</li>
2154 /// <para>Items in <paramref name="elements"/> are converted to string through
2155 /// <see cref="Object.ToString"/>.</para>
2157 /// <example>This example shows how to use <b>BuildOrderedList</b>:
2159 /// $HtmlHelper.BuildOrderedList( ICollection )
2162 public virtual String
BuildOrderedList(ICollection elements
)
2164 return BuildOrderedList(elements
, null, null);
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.
2171 /// <ol class="styleClassArg">
2172 /// <li class="itemClassArg">0</li>
2174 /// <li class="itemClassArg">object</li>
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:
2184 /// <ol class="styleClassArg">
2185 /// <li class="itemClassArg">0</li>
2186 /// <li class="itemClassArg">1</li>
2187 /// <li class="itemClassArg">2</li>
2188 /// <li class="itemClassArg">3</li>
2189 /// <li class="itemClassArg">4</li>
2190 /// <li class="itemClassArg">5</li>
2191 /// <li class="itemClassArg">object</li>
2194 /// <para>Items in <paramref name="elements"/> are converted to string through
2195 /// <see cref="Object.ToString"/>.</para>
2197 /// <example>This example shows how to use <b>BuildOrderedList</b>:
2199 /// $HtmlHelper.BuildOrderedList( ICollection, "styleClassArg", "itemClassArg" )
2202 public virtual String
BuildOrderedList(ICollection elements
, String styleClass
, String itemClass
)
2204 return BuildList("ol", elements
, styleClass
, itemClass
);
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.
2212 /// <listTag class="styleClassArg">
2213 /// <li class="itemClassArg">0</li>
2215 /// <li class="itemClassArg">object</li>
2216 /// </listTag>
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.
2229 /// Calling <c>BuildList( "listTag", ICollection, "styleClassArg", "itemClassArg" )</c> results in:
2231 /// <listTag class="styleClassArg">
2232 /// <li class="itemClassArg">0</li>
2233 /// <li class="itemClassArg">1</li>
2234 /// <li class="itemClassArg">2</li>
2235 /// <li class="itemClassArg">3</li>
2236 /// <li class="itemClassArg">4</li>
2237 /// <li class="itemClassArg">5</li>
2238 /// <li class="itemClassArg">object</li>
2239 /// </listTag>
2242 /// <para>Items in <paramref name="elements"/> are converted to string through
2243 /// <see cref="Object.ToString"/>.</para>
2245 /// <example>This example shows how to use <b>BuildList</b>:
2247 /// BuildList("ol", elements, styleClass, itemClass);
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
);
2266 foreach(object item
in elements
)
2268 if (item
== null) continue;
2270 writer
.WriteLine(BuildListItem(item
.ToString(), itemClass
));
2273 writer
.WriteEndTag(tag
);
2276 return sbWriter
.ToString();
2280 /// Generates a list item <b>li</b> tag.
2282 /// <li class="itemClassArg">object</li>
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.
2290 /// Calling <c>BuildListItem( "object", "itemClassArg" )</c> results in:
2292 /// <li class="itemClassArg">object</li>
2296 /// <example>This example shows how to use <b>BuildListItem</b>:
2298 /// BuildListItem(item.ToString(), itemClass);
2301 private static String
BuildListItem(String item
, String itemClass
)
2303 if (itemClass
== null)
2305 return String
.Format("<li>{0}</li>", item
);
2309 return String
.Format("<li class=\"{0}\">{1}</li>", itemClass
, item
);