[英]How to change the properties of a class css without ID?
I am making a user control with properties that can change 我正在使用具有可以更改的属性的用户控件
I have a HTML (.aspx): 我有一个HTML(.aspx):
<div>
<ul>
<li id="li_id" runat="server" class="myStyle" visible="false"></li>
</ul>
</div>
(My first li is false that the other will be added dynamically and want to make sure there is at least one reference css class) (我的第一个li是错误的,另一个将动态添加,并且要确保至少有一个引用css类)
my codebehind: 我的代码背后:
for (int i = 0; i < fotos.Count(); i++)
{
if (File.Exists(Server.MapPath((fotos[i]))))
{
content_slider_inside.InnerHtml += @"<li id='img" + i.ToString() + "'><IMG SRC='" + fotos[i] + "' HEIGHT='490PX' /></li>";
navigation.InnerHtml += "<li class='myStyle' runat='server'><a href='#img" + (i+1).ToString() + "'>" + (i+1).ToString() + "</a></li>";
}
}
My CSS (.css) 我的CSS(.css)
.myStyle
{
/*empty*/
}
How i add my style properties without using the ID code? 如何不使用ID代码添加样式属性? (since it is generated dinicamente) for example:
(因为它是由dinicamente生成的),例如:
myStyle["background-color"]="red";
for all generated dynamically change li css property 对于所有生成的动态更改li css属性
I'm NOT interested to solve it with javascript or jquery 我不感兴趣用javascript或jquery解决它
thank you so much 非常感谢
Using "class" attribute and CSS is good way of styling elements. 使用“ class”属性和CSS是样式元素的好方法。 It is not clear what you trying to do "dynamically", but maybe simply changing you CSS is enough (and you don't really need invisible element if the only reason is to mention class for CSS):
尚不清楚您尝试“动态”执行的操作,但是仅更改CSS就足够了(并且,如果唯一的原因是提及CSS类,则实际上不需要隐式元素):
.myStyle
{
background-color:"red";
}
If you want to style them individually you can inline in HTML directly: 如果要单独设置样式,可以直接在HTML中内联:
navigation.InnerHtml += "<li style='background-color:red;' ...
Note: it would be easier if you build control tree instead of using InnerHtml property. 注意:如果您构建控件树而不是使用InnerHtml属性,则会更容易。 At least don't specify
runat='server'
since there is no value in it when using InnerHtml property. 至少不要指定
runat='server'
因为使用InnerHtml属性时其中没有任何值。
您可以使用各种各样的CSS选择器来做到这一点http://www.w3.org/TR/CSS2/selector.html
two answers to my problem: 我的问题的两个答案:
as mentioned by the user Alexei Levenkov generating generic HTML controls HtmlGenericControl 正如用户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());
}
}
and then I can manually access their styles through his property 然后我可以通过他的媒体资源手动访问他们的样式
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();
}
}
}
To use the hover dynamically add one label style 若要使用悬停功能动态添加一种标签样式
<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>
I'm Happy! 我很高兴!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.