动态添加样式标签到ASP.Net网页

BID*_*per 3 html c# asp.net

我有以下代码,它将标签和gridview添加到asp.net页面:

  GridView grd = CreateGridView(kvp.Key.Text);
  Label l = new Label();
  l.Text = "some text";
  l.CssClass = "this has no effect";
  placeHolderResults.Controls.Add(l);
  placeHolderResults.Controls.Add(grd);
Run Code Online (Sandbox Code Playgroud)

真的有两个问题:

  1. 由于页面将有多个且未知数量的Label + Grid对,我正在循环上面的代码,这是将控件添加到页面的最佳方法吗?

  2. 我不能打造标签样式?你怎么做呢?查看创建的HTML,标签结果是SPAN.

提前致谢,

吉姆

Sko*_*ioh 5

l.CssClass只有在将类名从样式中放入其中时才会生效.例如:

<style type="text/css">
   .boldText {text-weight: bold}
</style>

// then the following should work
l.CssClass = "boldText";

// this will generate: <span class="boldText">your text</span>
Run Code Online (Sandbox Code Playgroud)


如果您只想直接添加样式,则可以执行以下操作:

l.Attributes.Add("style", "color:Red;font-weight:bold;");
// this will generate <span style="color:Red;font-weight:bold">your text</span>
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.玩得开心!


PS:
<asp:Literal>始终生成纯文本
<asp:Label>生成生成<SPAN>
<asp:Panel><DIV>



编辑于2010.12.09 - 根据Jim的评论修复了示例代码中的错误