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:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...