从后面的代码动态生成项目符号列表

时间:2012-11-15 09:54:55

标签: c# asp.net bulletedlist

拜托,能帮帮我,告诉我如何动态生成这段代码:

<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>

来自我的代码?

非常感谢

3 个答案:

答案 0 :(得分:3)

我建议使用Repeater,以便尽可能自定义您的控件。 BulletedList控件有限。无论如何,这是一次尝试:

<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);
        // ...
    }
}

答案 1 :(得分:1)

答案 2 :(得分:-1)

使用此代码方法:

private string getDynamicHTML(int count)
{
    string message = "<ul>";
    for (int i = 0; i < count; i++)
    {
        message = message + "<li>";
        message = message + "<a href='../adGallery/images/pictures/" + i +".jpg'>";
        message = message + "<img src='../adGallery/images/pictures/thumbs/" + i + ".jpg' title='A title for 10.jpg' alt='This is a nice, and incredibly descriptive, description of the image 10.jpg' class='image1'/></a>";
        message = message + "</li>";
    }
    return message + "</ul>";
}

并将标签放在所需位置,并将结果绑定到该标签

Label1.Text=getDynamicHTML(10);

这将呈现为必需的HTML