Monday, July 26, 2010

How to build a readonly checkbox list - Asp.net

public partial class TestPage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        ImageCheckBoxList chkList = new ImageCheckBoxList();
        for (int i = 0; i < 10; i++)
            chkList.Items.Add(new ListItem("Item " + i) { Selected = i % 2 == 0 });
        this.form1.Controls.Add(chkList);
    }
}
public class ImageCheckBoxList : CheckBoxList
{
    public bool IsEditable { getset; }
    public override void RenderControl(HtmlTextWriter writer)
    {
        if (this.IsEditable)
            base.RenderControl(writer);
        else
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table class=\"ImageCheckBoxList\">");
            foreach (ListItem item in this.Items)
            {
                sb.Append("<tr>");
                sb.AppendFormat("<td><img src=\"{0}\" alt=\"{1}Checked\" /></td>",
                    ResolveUrl(string.Format("~/Images/{0}Checked.png", 
                                            item.Selected ? string.Empty : "Not")),
                    item.Selected ? string.Empty : "Not");
                sb.AppendFormat("<td><label>{0}</label></td>", item.Text);
                sb.Append("</tr>");
            }
            sb.Append("</table>");
            writer.Write(sb.ToString());
        }
 
    }
}

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...