我的问题的两个答案:
正如用户 Alexei Levenkov 所提到的,生成通用 HTML 控件 HtmlGenericControl
for (int i = 0; i < fotos.Count(); i++)
{
if (File.Exists(Server.MapPath((fotos[i]))))
{
content_slider_inside.InnerHtml += @"<li id='img" + (i + 1).ToString() + "'><IMG SRC='" + fotos[i] + "' HEIGHT='490PX' /></li>";
//navigation.InnerHtml += "<li id='item" + (i+1).ToString() + "' runat='server' class='menu_item'><a href='#img" + (i+1).ToString() + "'>" + (i+1).ToString() + "</a></li>";
HtmlGenericControl a = new HtmlGenericControl("a");
a.Attributes["href"] = "#img" + (i+1).ToString();
a.InnerHtml = (i + 1).ToString();
HtmlGenericControl c = new HtmlGenericControl("li");
c.ID = "item" + (i+1).ToString();
c.Attributes["runat"] = "server";
c.Attributes["class"] = "menu_item";
c.Controls.Add(a);
navigation.Controls.Add(c);
id_imgs.Add("img" + (i+1).ToString());
id_items.Add("item" + (i+1).ToString());
}
}
然后我可以通过他的属性手动访问他们的样式
foreach(string i in id_items)
{
HtmlGenericControl liItem = (HtmlGenericControl)navigation.FindControl(i);
if (liItem != null)
{
if (menu_Item_Fondo != null)
liItem.Style["background-color"] = menu_Item_Fondo;
if (menu_Item_Hover_Fondo != null)
{
StringBuilder style = new StringBuilder();
style.AppendLine(".navigation li a:hover { ");
style.AppendLine(String.Format(" color: {0};", menu_Item_Hover_TextColor));
style.AppendLine(String.Format(" background-color: {0};", menu_Item_Hover_Fondo));
style.AppendLine("} ");
style_menu_item.InnerText = style.ToString();
}
}
}
要动态使用悬停添加一种标签样式
<div>
<style id="style_menu_item" runat="server" class="class_style"></style>
<ul>
<li id="li_id" runat="server" class="myStyle" visible="false"></li>
</ul>
</div>
我很高兴!