代码背后的样式表问题

时间:2011-03-24 12:45:05

标签: c# asp.net html css stylesheet

您好我试图为此设置css样式但我不知道发生了什么我从asp(代码隐藏)的代码

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

这样做:

enter image description here

我的css原来就是这样:

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

但我现在不知道如何更改css以反映我的代码中的更改,以便我可以应用样式。

4 个答案:

答案 0 :(得分:2)

您始终可以通过

将css应用于div元素
test1.Attributes["class"] = "yourCSS";

并在您的aspx文件中包含css。你也可以尝试

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

答案 1 :(得分:0)

您可以尝试test1.className = "nameofyourcss";test1.setAttribute("class", "nameofyourcss");

答案 2 :(得分:0)

我建议您使用Literal而不是生成div和图像。我使用Literal修改了代码。

Literal literal1 = new Literal();
  literal1.Text =
  string.Format("<div style=\"float: left\"><img src='{0}' alt='Test Image'>
  </img></div>",ResolveUrl("~/userdata/2/uploadedimage/batman-for-facebook.jpg"));
  literal1 = new Literal();
  literal1.Text = string.Format("<div style=\"float: left\">{0}</div>", Reader.GetString(0));
  test1.Controls.Add(literal1);
  literal1 = new Literal();
  literal1.Text = "<div style=\"clear: both\">{0}</div>";
  test1.Controls.Add(literal1);

答案 3 :(得分:0)

可能想通过将div包装成一个来尝试这样的事情。那是给出顶部和底部边界。

<style type="text/css">
        #test1 .desc
        {
            padding:27.5px; 
            float:left;
            height: 100px; 
        }
        #test1 .img
        {
            float:left;
            padding:27.5px;
        }
        #test1 > div.main
        {
        border-top: thin solid #736F6E;
        border-bottom: thin solid #736F6E;
        color:#000000;
        margin:0 auto;
        white-space: pre;
        white-space: pre-wrap;
        white-space: pre-line;
        }
    </style>

System.Web.UI.HtmlControls.HtmlGenericControl maindiv = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
            maindiv.Attributes["class"] = "main";

            System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
            div.Attributes["class"] = "img";
            Image img = new Image();
            img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg";
            img.AlternateText = "Test image";
            div.Controls.Add(img);
            maindiv.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.Attributes["class"] = "desc";
            maindiv.Controls.Add(div1);

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