Search code examples
c#asp.nethtmlcssstylesheet

style sheet problems from code behind


Hi im trying to set the css style for this but I dont know whats happening I have this code from asp (code behind)

System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div.Style["float"] = "left";
Image img = new Image();
img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg";
img.AlternateText = "Test image";
div.Controls.Add(img);
test1.Controls.Add(div);

System.Web.UI.HtmlControls.HtmlGenericControl div1 = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div1.InnerText = String.Format("{0}", reader.GetString(0));

div1.Style["float"] = "left";
test1.Controls.Add(div1);

System.Web.UI.HtmlControls.HtmlGenericControl div2 = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div2.Style["clear"] = "both";
test1.Controls.Add(div2);

which does this:

enter image description here

My css was originaly this:

div#test1 {
}
div#div
{
  width:90%; 
  z-index:1; 
  padding:27.5px; 
  border-top: thin solid #736F6E;
  border-bottom: thin solid #736F6E;
  color:#ffffff;
  margin:0 auto;
  white-space: pre;
  white-space: pre-wrap;
  white-space: pre-line;
  word-wrap: break-word;
}

But I dont know how to change the css now to reflect the changes in my code so that I can apply style to it.


Solution

  • You can always apply css to your div elements by

    test1.Attributes["class"] = "yourCSS";
    

    and include the css in your aspx file. You can also try

     test1.Attributes.Add("class", "yourCSS")