使用C#将自定义li添加到ul

问题描述 投票:1回答:1

我正在尝试从C#后端构建无序列表。这是我要实现的结构:

 <li><a href="url.com"><img src="../images/most/most05.jpg" alt="" /><br />LinkName</a></li>

使用此代码:

foreach (Product prod in productList)
                {
                    HtmlGenericControl li = new HtmlGenericControl("li");
                    products.Controls.Add(li);
                    string productURL = SEOHelper.GetProductUrl(prod);
                    HtmlGenericControl anchor = new HtmlGenericControl("a");
                anchor.Attributes.Add("href", productURL);
                    HtmlGenericControl image = new HtmlGenericControl("img");


                    var productPicture = prod.DefaultProductPicture;
                    if (productPicture != null)
                    {

                        image.Attributes.Add("src", PictureManager.GetPictureUrl(productPicture.Picture, 110, true));
                    }
                    else
                    {

                        image.Attributes.Add("src", PictureManager.GetPictureUrl(productPicture.Picture, 110, true));
                    }

                  anchor.InnerText = prod.Name;                    
                    li.Controls.Add(image);
                    li.Controls.Add(anchor);
                }  

我正在获取此结构:

 <li><img src="http://localhost:22621/images/thumbs/0000724_110.jpg"></img><a href="url.com">LinkName</a></li>

如何调整代码以实现我想要的?

c# asp.net listview html-lists htmlgenericcontrol
1个回答
1
投票

而不是将图像添加到LI,您需要将其添加到锚点。

而且,使HtmlGenericControl发出自关闭标签的唯一方法是重写其实现并进行修复。

总而言之,要么以您想要的方式生成文本并发出该文本,要么查看这些项目的常规.net控件。

© www.soinside.com 2019 - 2024. All rights reserved.