简体   繁体   English

如何在c#中将CSS添加到动态创建的div中?

[英]How can i add css to a dynamically created div in c#?

i am using css to configure this div's properties using it's id, but it aint working, maybe because i created it in c# in the code behind, how can i fix that? 我正在使用css使用它的id配置此div的属性,但是它不能正常工作,也许是因为我在后面的代码中用c#创建了它,我该如何解决?
c# C#

StringBuilder sb = new StringBuilder();
sb.Append(@"<div id=""img1"" runat=""server"">Vidal</div>");
Label1.Text = sb.ToString();

Css CSS

#img1{
position: absolute;
top: 20px;
left: 20px;
width: 253px;height: 190px;

} }

I would use a class instead of an ID. 我将使用类而不是ID。 Also, you don't need to make it a div, as your Label will render as a span. 另外,您无需将其设为div,因为您的Label将呈现为跨度。 You simply need to set the CssClass and Text properties. 您只需要设置CssClassText属性。


C# C#

Label1.CssClass = "someClass";
Label1.Text = "Vidal";



CSS CSS

.someClass
{
    position: absolute;
    top: 20px;  
    left: 20px;
    width: 253px;
    height: 190px;
}

you can't use label control to display html content , use literal control or panel like below 您不能使用标签控件来显示html内容,不能使用文字控件或下面的面板

Panel div = new Panel();
div.ID = "img1";   
div.Controls.Add(new Literal{Text = "Vidal"});    
this.Controls.Add(div);

I think for your purposes, you might be better with asp:literal control: 我认为出于您的目的,使用asp:literal control可能会更好:

StringBuilder sb = new StringBuilder();
sb.Append("<div class=\"image-css-selector">Vidal</div>");
Literal1.Text = sb.ToString();

A label control will have it's own markup and that might affect your html code 标签控件将具有自己的标记,这可能会影响您的html代码

If you are using a single webform, then please make sure that you do not have multiple ids. 如果您使用的是单个Web表单,请确保您没有多个ID。 If you are using a master page, and you really need a runat="server attribute to the div then you need to change the css like this: 如果您使用的是母版页,并且确实需要对div使用runat="server属性”,则需要像这样更改css:

#ContentPlaceHolder_img1{
position: absolute;
top: 20px;
left: 20px;
width: 253px;height: 190px;

Where ContentPlaceHolder is the name of the ID that you have gave for your content placeholder. 其中ContentPlaceHolder是您为内容占位符提供的ID的名称。 for ex: 例如:

    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
     </asp:ContentPlaceHolder>

In your master page. 在您的母版页中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中动态创建DIV,向其添加子元素,然后隐藏DIV(及其上的所有内容)? - How can I dynamically create a DIV in C#, add child elements to it, and then hide the DIV (and everything on it)? 如何将事件添加到通过单击按钮动态创建的PictureBox中? (C#) - How can I add events to a PictureBox dynamically created by clicking button? (C#) 如何向动态创建的(C#)元素添加“必需”属性? - How can I add a “Required” attribute to a dynamically-created (C#) element? C#如何取消选中动态创建的单选按钮? - C# how can I uncheck dynamically created radio buttons? 如何在C#中向div动态添加数据表? - How to Add Datatable Dynamically to div in C#? 如何在C#中为类动态添加字段? - How can I dynamically add a field to a class in C# 我可以在加载事件的C#中的div标签中动态添加HTML吗? - Can I dynamically add HTML within a div tag from C# on load event? 在C#中将CSS分配给动态创建的Label - Assigning CSS to dynamically created Label in C# 如何检查在C#中动态创建的复选框 - How can I check for the checkboxes that I've dynamically created in C# 我可以将动态创建的c#表转换为html字符串吗? - Can I convert a dynamically created c# table to a html string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM