简体   繁体   English

在C#中将CSS分配给动态创建的Label

[英]Assigning CSS to dynamically created Label in C#

I'm trying to figure out how to apply CSS to a Label created in C#. 我试图弄清楚如何将CSS应用于在C#中创建的Label。 Everything compiles and runs, it just doesn't seem to be applying the CSS. 一切都可以编译并运行,但似乎并没有应用CSS。 The CSS is in the file linked to in the site master page. CSS位于站点母版页中链接到的文件中。 Everything else in the CSS file is being applied as it should be. CSS文件中的所有其他内容都应按应有的方式应用。

Codebehind: 代码隐藏:

...
Label label = new Label();
SqlCommand command = new SqlCommand("SELECT Q_Text FROM HRA.dbo.Questions WHERE QID = 1");
command.Connection = connection;
reader = command.ExecuteReader();
reader.Read();
label.Text = reader["Q_Text"].ToString();
label.ID = "rblabel";
label.CssClass = "rblabel";
reader.Close();
holder.Controls.Add(label);
...

ASP: ASP:

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <asp:PlaceHolder ID="holder"  runat="server">

    </asp:PlaceHolder>
</asp:Content>

CSS: CSS:

.rblabel {
    text-align:left;
    padding-left: 2em;
    font-size: 4em;
}

EDIT: added the HTML as well as the Control.Add() statement in my code (forgot to include that in my copy/pase). 编辑:在我的代码中添加了HTML以及Control.Add()语句(忘了在我的复制/姿势中包含它)。 This HTML is the etirety of what is put into that PlaceHolder . 此HTML是放置在该PlaceHolder的内容的混乱。

HTML: HTML:

<section class="content-wrapper main-content clear-fix">
    <span id="MainContent_rblabel" class="rblabel">TEST TEST TEST</span>
    <table id="MainContent_ctl00" class="radio">
    <tr>
        <td><input id="MainContent_ctl00_0" type="radio" name="ctl00$MainContent$ctl00"value="1" />
        <label for="MainContent_ctl00_0">Excellent</label></td>
    </tr><tr>
  <td><input id="MainContent_ctl00_1" type="radio" name="ctl00$MainContent$ctl00" value="1" />
         <label for="MainContent_ctl00_1">Good</label></td>
    </tr><tr>
   <td><input id="MainContent_ctl00_2" type="radio" name="ctl00$MainContent$ctl00"value="1" />
          <label for="MainContent_ctl00_2">Fair</label></td>
    </tr><tr>
   <td><input id="MainContent_ctl00_3" type="radio" name="ctl00$MainContent$ctl00"value="1" />
         <label for="MainContent_ctl00_3">Poor</label></td>
    </tr>
</table>
</section>

There are basically two possibilities: 基本上有两种可能性:

  1. Your label may not be rendering with the CLASS attribute set to the desired CSS class. 标签的CLASS属性设置为所需的CSS类时,可能无法呈现。 That you can verify by looking at the rendered HTML. 您可以通过查看呈现的HTML进行验证。 (If the label doesn't appear at all, your code suggests you may not be adding it to the Controls collection of its parent.) (如果标签根本没有出现,则您的代码表明您可能未将其添加到其父级的Controls集合中。)

  2. Everything is rendering right, but it's not appearing correctly in your browser, due to some problem with your CSS or with another stylesheet overriding it. 一切都可以正确渲染,但是由于CSS或其他覆盖它的样式表存在问题,因此无法在浏览器中正确显示。 To debug this, you will need to use Firebug or IE's developer tools. 要对此进行调试,您将需要使用Firebug或IE的开发人员工具。

Good luck! 祝好运!

CssClass property is the proper way to go http://www.w3schools.com/aspnet/prop_webcontrol_style_cssclass.asp CssClass属性是访问http://www.w3schools.com/aspnet/prop_webcontrol_style_cssclass.asp的正确方法

You left out the code where you're adding the label to the page/container etc, which may be where an error lies. 您省略了将标签添加到页面/容器等的代码,这可能是错误所在。

Showing an HTML source output of the page to see if the label your generating actually contains a CLASS attribute with a value of "rblabel" would also be helpful. 显示页面的HTML源输出,以查看生成的标签是否实际上包含值为“ rblabel”的CLASS属性。

If worse comes to worse use a Literal instead of a Label fill in the HTML. 如果情况变得更糟,请在HTML中使用文字代替标签。

It doesn't look like you are actually adding the label where you want it to appear. 看起来您实际上并没有在要显示标签的位置添加标签。 Try something like: 尝试类似:

holder.Controls.Add(label);

in your code-behind. 在您的后台代码中。

A span is an inline element, so padding, width, and height aren't always honored in all browsers. 跨度是一个内联元素,因此并非总是在所有浏览器中都采用padding,width和height。 Try setting it to inline-block . 尝试将其设置为inline-block

.rblabel {
    text-align:left;
    padding-left: 2em;
    font-size: 4em;
    display: inline-block;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM