0

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?
c#

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

Css

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

}

5
  • check the source of page if the div has same id..if you have masterpage the id will be changed.. Commented Dec 19, 2013 at 17:51
  • 1
    Use css class instead of id Commented Dec 19, 2013 at 17:51
  • First, get rid of 'runas' - it won't add anything. 2nd, what does the source to the page look like when the browser renders it? Commented Dec 19, 2013 at 17:52
  • 1
    Also, remove the runat=server, as this isn't a server control. Commented Dec 19, 2013 at 17:52
  • 1
    Won't a label Html encode the value you try to store into it? Commented Dec 19, 2013 at 17:52

4 Answers 4

3

I would use a class instead of an ID. Also, you don't need to make it a div, as your Label will render as a span. You simply need to set the CssClass and Text properties.


C#

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



CSS

.someClass
{
    position: absolute;
    top: 20px;  
    left: 20px;
    width: 253px;
    height: 190px;
}
Sign up to request clarification or add additional context in comments.

2 Comments

label will render as a span. adding a div inside span is not semantic. instead of label, use literal control.
@Ravimallya - That's true. I'll just have him use the CssClass and remove the div markup to the label itself.
1

you can't use label control to display html content , use literal control or panel like below

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

Comments

1

I think for your purposes, you might be better with 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

Comments

0

If you are using a single webform, then please make sure that you do not have multiple ids. 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:

#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. for ex:

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

In your master page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.