Dynamically add a stylized tag to an ASP.Net web page

I have the following code that adds a label and gridview to an asp.net page:

  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);

Two questions:

  • Since there will be several and an odd number of Label + Grid pairs on the page, I am going through the code above, is this the best way to add controls to the page?

  • Can't I create a shortcut? How do you do it? Looking at the generated HTML code, the shortcut is SPAN.

Thanks in advance,

Jim

+3
source share
2 answers

l.CssClass will have an effect if you put the class name from the style in it. For instance:

<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>


If you just want to add a style directly, you can do the following:

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

, . !


PS:
<asp:Literal>
<asp:Label> <SPAN>
<asp:Panel> <DIV>



EDITED on 2010.12.09 -

+4

RED BOLD:

lblMyLabel.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
lblMyLabel.Style["font-weight"] = "bold";

BLACK NORMAL:

lblMyLabel.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
lblMyLabel.Style["font-weight"] = "normal";
+1

Source: https://habr.com/ru/post/1779374/


All Articles