/// <summary> /// Creates a select list with the values in a collection. /// </summary> /// <param name="name">Name of the SELECT-tag</param> /// <param name="id">Id of the SELECT-tag</param> /// <param name="collection">collection used to generate options.</param> /// <param name="getIdTitle">delegate used to return id and title from objects.</param> /// <param name="selectedValue">value that should be marked as selected.</param> /// <param name="firstEmpty">First row should contain an empty value.</param> /// <returns>string containtain a SELECT-tag.</returns> /// <seealso cref="GetIdTitle"/> /// <example> /// <code> /// // Class that is going to be used in a SELECT-tag. /// public class User /// { /// private readonly string _realName; /// private readonly int _id; /// public User(int id, string realName) /// { /// _id = id; /// _realName = realName; /// } /// public string RealName /// { /// get { return _realName; } /// } /// /// public int Id /// { /// get { return _id; } /// } /// } /// /// // Using an inline delegate to generate the select list /// public void UserInlineDelegate() /// { /// List<User> items = new List<User>(); /// items.Add(new User(1, "adam")); /// items.Add(new User(2, "bertial")); /// items.Add(new User(3, "david")); /// string htmlSelect = Select("users", "users", items, delegate(object o, out object id, out object value) /// { /// User user = (User)o; /// id = user.Id; /// value = user.RealName; /// }, 2, true); /// } /// /// // Using an method as delegate to generate the select list. /// public void UseExternalDelegate() /// { /// List<User> items = new List<User>(); /// items.Add(new User(1, "adam")); /// items.Add(new User(2, "bertial")); /// items.Add(new User(3, "david")); /// string htmlSelect = Select("users", "users", items, UserOptions, 1, true); /// } /// /// // delegate returning id and title /// public static void UserOptions(object o, out object id, out object title) /// { /// User user = (User)o; /// id = user.Id; /// value = user.RealName; /// } /// </code> /// </example> public static string Select(string name, string id, IEnumerable collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { if (name == null) { throw new ArgumentNullException("name"); } if (id == null) { throw new ArgumentNullException("id"); } if (collection == null) { throw new ArgumentNullException("collection"); } if (getIdTitle == null) { throw new ArgumentNullException("getIdTitle"); } StringBuilder sb = new StringBuilder(); sb.AppendLine("<select name=\"" + name + "\" id=\"" + id + "\">"); Options(sb, collection, getIdTitle, selectedValue, firstEmpty); sb.AppendLine("</select>"); return(sb.ToString()); }
/// <summary> /// Creates a select list with the values in a collection. /// </summary> /// <param name="name">Name of the SELECT-tag</param> /// <param name="id">Id of the SELECT-tag</param> /// <param name="collection">collection used to generate options.</param> /// <param name="getIdTitle">delegate used to return id and title from objects.</param> /// <param name="selectedValue">value that should be marked as selected.</param> /// <param name="firstEmpty">First row should contain an empty value.</param> /// <param name="htmlAttributes">name, value collection of extra html attributes.</param> /// <returns>string containtain a SELECT-tag.</returns> /// <seealso cref="GetIdTitle"/> public static string Select(string name, string id, IEnumerable collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty, params string[] htmlAttributes) { if (name == null) { throw new ArgumentNullException("name"); } if (id == null) { throw new ArgumentNullException("id"); } if (collection == null) { throw new ArgumentNullException("collection"); } if (getIdTitle == null) { throw new ArgumentNullException("getIdTitle"); } if (htmlAttributes != null && (htmlAttributes.Length % 2) != 0) { throw new ArgumentException("Invalid html attribute list."); } StringBuilder sb = new StringBuilder(); sb.Append("<select name=\"" + name + "\" id=\"" + id + "\""); WebHelper.GenerateHtmlAttributes(sb, htmlAttributes); sb.AppendLine(">"); Options(sb, collection, getIdTitle, selectedValue, firstEmpty); sb.AppendLine("</select>"); return(sb.ToString()); }
/// <exception cref="ArgumentNullException"><c>sb</c> is null.</exception> private static void Options( StringBuilder sb, IEnumerable collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { if (sb == null) { throw new ArgumentNullException("sb"); } if (collection == null) { throw new ArgumentNullException("collection"); } if (getIdTitle == null) { throw new ArgumentNullException("getIdTitle"); } if (firstEmpty) { sb.AppendLine("<option value=\"\"> </option>"); } foreach (object o in collection) { object value; string title; getIdTitle(o, out value, out title); sb.Append("<option value=\""); if (value != null) { sb.Append(value); } sb.Append("\""); if (value != null && selectedValue != null && value.Equals(selectedValue)) { sb.Append(" selected=\"selected\""); } sb.Append(">"); if (title == null) { sb.AppendLine(" </option>"); } else { sb.Append(title); sb.AppendLine("</option>"); } } }
/// <summary> /// Generate a list of HTML options /// </summary> /// <param name="collection">collection used to generate options.</param> /// <param name="getIdTitle">delegate used to return id and title from objects.</param> /// <param name="selectedValue">value that should be marked as selected.</param> /// <param name="firstEmpty">First row should contain an empty value.</param> /// <returns></returns> public static string Options(IEnumerable collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { if (collection == null) { throw new ArgumentNullException("collection"); } if (getIdTitle == null) { throw new ArgumentNullException("getIdTitle"); } StringBuilder sb = new StringBuilder(); Options(sb, collection, getIdTitle, selectedValue, firstEmpty); return(sb.ToString()); }
public static string Select( string name, string id, ICollection collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { StringBuilder sb = new StringBuilder(); sb.AppendLine("<select name=\"" + name + "\" id=\"" + id + "\">"); if (firstEmpty) { sb.AppendLine("<option value=\"\"> </option>"); } foreach (object o in collection) { object value; string title; getIdTitle(o, out value, out title); sb.Append("<option value=\""); if (value != null) { sb.Append(value); } sb.Append("\""); if (value != null && value.Equals(selectedValue)) { sb.Append(" selected=\"selected\""); } sb.Append(">"); if (title == null) { sb.AppendLine(" </option>"); } else { sb.Append(value); sb.AppendLine("</option>"); } } sb.AppendLine("</select>"); return(sb.ToString()); }
/// <summary> /// Creates a select list with the values in a collection. /// </summary> /// <param name="name">Name of the SELECT-tag</param> /// <param name="collection">collection used to generate options.</param> /// <param name="getIdTitle">delegate used to return id and title from objects.</param> /// <param name="selectedValue">value that should be marked as selected.</param> /// <param name="firstEmpty">First row should contain an empty value.</param> /// <returns>string containtain a SELECT-tag.</returns> /// <seealso cref="GetIdTitle"/> public static string Select(string name, IEnumerable collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { return Select(name, name, collection, getIdTitle, selectedValue, firstEmpty); }
private static void Options(StringBuilder sb, IEnumerable collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { if (sb == null) throw new ArgumentNullException("sb"); if (collection == null) throw new ArgumentNullException("collection"); if (getIdTitle == null) throw new ArgumentNullException("getIdTitle"); if (firstEmpty) sb.AppendLine("<option value=\"\"> </option>"); foreach (object o in collection) { object value; string title; getIdTitle(o, out value, out title); sb.Append("<option value=\""); if (value != null) sb.Append(value); sb.Append("\""); if (value != null && selectedValue != null && value.Equals(selectedValue)) sb.Append(" selected=\"selected\""); sb.Append(">"); if (title == null) sb.AppendLine(" </option>"); else { sb.Append(title); sb.AppendLine("</option>"); } } }
/// <summary> /// Generate a list of HTML options /// </summary> /// <param name="collection">collection used to generate options.</param> /// <param name="getIdTitle">delegate used to return id and title from objects.</param> /// <param name="selectedValue">value that should be marked as selected.</param> /// <param name="firstEmpty">First row should contain an empty value.</param> /// <returns></returns> public static string Options(IEnumerable collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { if (collection == null) throw new ArgumentNullException("collection"); if (getIdTitle == null) throw new ArgumentNullException("getIdTitle"); StringBuilder sb = new StringBuilder(); Options(sb, collection, getIdTitle, selectedValue, firstEmpty); return sb.ToString(); }
/// <summary> /// Creates a select list with the values in a collection. /// </summary> /// <param name="name">Name of the SELECT-tag</param> /// <param name="id">Id of the SELECT-tag</param> /// <param name="collection">collection used to generate options.</param> /// <param name="getIdTitle">delegate used to return id and title from objects.</param> /// <param name="selectedValue">value that should be marked as selected.</param> /// <param name="firstEmpty">First row should contain an empty value.</param> /// <param name="htmlAttributes">name, value collection of extra html attributes.</param> /// <returns>string containtain a SELECT-tag.</returns> /// <seealso cref="GetIdTitle"/> public static string Select(string name, string id, IEnumerable collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty, params string[] htmlAttributes) { if (name == null) throw new ArgumentNullException("name"); if (id == null) throw new ArgumentNullException("id"); if (collection == null) throw new ArgumentNullException("collection"); if (getIdTitle == null) throw new ArgumentNullException("getIdTitle"); if (htmlAttributes != null && (htmlAttributes.Length % 2) != 0) throw new ArgumentException("Invalid html attribute list."); StringBuilder sb = new StringBuilder(); sb.Append("<select name=\"" + name + "\" id=\"" + id + "\""); WebHelper.GenerateHtmlAttributes(sb, htmlAttributes); sb.AppendLine(">"); Options(sb, collection, getIdTitle, selectedValue, firstEmpty); sb.AppendLine("</select>"); return sb.ToString(); }
/// <summary> /// Creates a select list with the values in a collection. /// </summary> /// <param name="name">Name of the SELECT-tag</param> /// <param name="id">Id of the SELECT-tag</param> /// <param name="collection">collection used to generate options.</param> /// <param name="getIdTitle">delegate used to return id and title from objects.</param> /// <param name="selectedValue">value that should be marked as selected.</param> /// <param name="firstEmpty">First row should contain an empty value.</param> /// <returns>string containtain a SELECT-tag.</returns> /// <seealso cref="GetIdTitle"/> /// <example> /// <code> /// // Class that is going to be used in a SELECT-tag. /// public class User /// { /// private readonly string _realName; /// private readonly int _id; /// public User(int id, string realName) /// { /// _id = id; /// _realName = realName; /// } /// public string RealName /// { /// get { return _realName; } /// } /// /// public int Id /// { /// get { return _id; } /// } /// } /// /// // Using an inline delegate to generate the select list /// public void UserInlineDelegate() /// { /// List<User> items = new List<User>(); /// items.Add(new User(1, "adam")); /// items.Add(new User(2, "bertial")); /// items.Add(new User(3, "david")); /// string htmlSelect = Select("users", "users", items, delegate(object o, out object id, out object value) /// { /// User user = (User)o; /// id = user.Id; /// value = user.RealName; /// }, 2, true); /// } /// /// // Using an method as delegate to generate the select list. /// public void UseExternalDelegate() /// { /// List<User> items = new List<User>(); /// items.Add(new User(1, "adam")); /// items.Add(new User(2, "bertial")); /// items.Add(new User(3, "david")); /// string htmlSelect = Select("users", "users", items, UserOptions, 1, true); /// } /// /// // delegate returning id and title /// public static void UserOptions(object o, out object id, out object title) /// { /// User user = (User)o; /// id = user.Id; /// value = user.RealName; /// } /// </code> /// </example> public static string Select(string name, string id, IEnumerable collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { if (name == null) throw new ArgumentNullException("name"); if (id == null) throw new ArgumentNullException("id"); if (collection == null) throw new ArgumentNullException("collection"); if (getIdTitle == null) throw new ArgumentNullException("getIdTitle"); StringBuilder sb = new StringBuilder(); sb.AppendLine("<select name=\"" + name + "\" id=\"" + id + "\">"); Options(sb, collection, getIdTitle, selectedValue, firstEmpty); sb.AppendLine("</select>"); return sb.ToString(); }
/// <summary> /// Creates a select list with the values in a collection. /// </summary> /// <param name="name">Name of the SELECT-tag</param> /// <param name="collection">collection used to generate options.</param> /// <param name="getIdTitle">delegate used to return id and title from objects.</param> /// <param name="selectedValue">value that should be marked as selected.</param> /// <param name="firstEmpty">First row should contain an empty value.</param> /// <returns>string containtain a SELECT-tag.</returns> /// <seealso cref="GetIdTitle"/> public static string Select(string name, IEnumerable collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { return(Select(name, name, collection, getIdTitle, selectedValue, firstEmpty)); }
public static string Select(string name, ICollection collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { return(FormHelper.Select(name, name, collection, getIdTitle, selectedValue, firstEmpty)); }
public static string Select( string name, string id, ICollection collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { StringBuilder sb = new StringBuilder(); sb.AppendLine("<select name=\"" + name + "\" id=\"" + id + "\">"); if (firstEmpty) sb.AppendLine("<option value=\"\"> </option>"); foreach (object o in collection) { object value; string title; getIdTitle(o, out value, out title); sb.Append("<option value=\""); if (value != null) sb.Append(value); sb.Append("\""); if (value != null && value.Equals(selectedValue)) sb.Append(" selected=\"selected\""); sb.Append(">"); if (title == null) sb.AppendLine(" </option>"); else { sb.Append(value); sb.AppendLine("</option>"); } } sb.AppendLine("</select>"); return sb.ToString(); }
public static string Select( string name, ICollection collection, GetIdTitle getIdTitle, object selectedValue, bool firstEmpty) { return FormHelper.Select(name, name, collection, getIdTitle, selectedValue, firstEmpty); }