简体   繁体   English

如何更改不带ID的CSS类的属性?

[英]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.

相关问题 如何在没有实例化的情况下读取 class 属性? - How to read class Properties without instantiation? 如何进行分类以使属性无法更改 - How to make class such that properties will not be able to change 如何获取没有自定义类类型的一个类的属性 - How get properties of one class without custom class types 在比较它们的 int id 属性时如何强制执行类类型 - How to enforce class types when comparing their int id properties 如何从类中获取名称为“Id”的所有 Guid 属性及其值? - How to get all Guid properties and their values with the name "Id" from class? 如何更改List中的Control的属性 <UIControl> 不使用Loop? - How to change properties of a Control that is in List<UIControl> without using Loop? 如何在不更改其属性的情况下在Controller中制作不可变的BusinessLayer? - How to make an immutable BusinessLayer in Controller without change its properties? 如何在不更改COM +类ID的情况下将方法添加到类 - How to add methods to a class without changing the COM+ Class ID 如何在C#中将其他属性添加到类而不继承 - How to add Additional Properties to a Class without inheritance in C# 如何在不使用.net中的属性的情况下映射dynamodb中的类属性 - How to map a class properties in dynamodb without using attribute in .net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM