Return to Snippet

Revision: 50772
at September 2, 2011 02:05 by sommertim


Initial Code
public static void ShowCheckBoxList<T>(this ViewPage page, IList<string> list, string name, Expression<Func<T, object>> valueProperty, Expression<Func<T, object>> displayProperty, List<string> selectedValue, System.Web.UI.WebControls.Orientation orientation)
    {
        HtmlTextWriter writer = new HtmlTextWriter(page.Response.Output);
        if (writer != null)
        {
            for (int i = 0; i < list.Count; i++)
            {
                string value = list[i];
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "CHECKBOX");
                writer.AddAttribute(HtmlTextWriterAttribute.Id, name + "_" + i);
                writer.AddAttribute(HtmlTextWriterAttribute.Name, name, true);
                writer.AddAttribute(HtmlTextWriterAttribute.Value, value, true);
                if (selectedValue != null &&selectedValue.Contains(value))
                    writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");

                writer.RenderBeginTag(HtmlTextWriterTag.Input);
                writer.Write(value);
                writer.RenderEndTag();

                if (orientation == System.Web.UI.WebControls.Orientation.Vertical)
                {
                    writer.Write("<br/>");
                }
            }
        }
    }

Initial URL

                                

Initial Description
Code snippet that allows developers to use a generic radiobuttonlist in the MVC Framework

Initial Title
MVC Checkboxlist

Initial Tags
extension

Initial Language
C#