Dynamically generated bulleted list from code behind

IsraGab picture IsraGab · Nov 15, 2012 · Viewed 10.8k times · Source

Please, could you help me and tell me how can I generated dynamically this piece of code:

<li>
          <a href="../adGallery/images/pictures/1.jpg">
            <img src="../adGallery/images/pictures/thumbs/1.jpg" class="image0"/>
          </a>
        </li>
        <li>
          <a href="../adGallery/images/pictures/2.jpg">
            <img src="../adGallery/images/pictures/thumbs/2.jpg" title="A title for 10.jpg" alt="This is a nice, and incredibly descriptive, description of the image 10.jpg" class="image1"/>
          </a>
        </li>

from my code behind?

Thank you very much

Answer

Tim Schmelter picture Tim Schmelter · Nov 15, 2012

I would suggest to use a Repeater which enables to customize your controls as much as possible. A BulletedList control is limited. Here's an attempt anyway:

<asp:BulletedList id="ImagesBulletedList" 
        BulletStyle="Disc"
        DisplayMode="HyperLink" 
        OnClick="ImageBulletedList_Click"
        runat="server">    
</asp:BulletedList>

codebehind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ListItem listItem = new ListItem("Picture 1", "../adGallery/images/pictures/1.jpg");
        listItem.Attributes.Add("class", "image0");
        ImagesBulletedList.Items.Add(listItem);
        listItem = new ListItem("Picture 2", "../adGallery/images/pictures/2.jpg");
        listItem.Attributes.Add("class", "image1");
        listItem.Attributes.Add("title", "A title for 10.jpg");
        ImagesBulletedList.Items.Add(listItem);
        // ...
    }
}