2“静态”div之间的“动态”div [CSS] [ASP.NET]

时间:2013-11-26 08:34:36

标签: c# asp.net css class html

我有一些问题需要帮助。

我有以下代码:

<div class="UpperClass"></div>

<div class="LowerClass"></div>

使用以下简单的CSS:

.UpperClass{
background-color: red;
width: 50px;
height: 50px;
}


.MiddleClass{
background-color: green;
width: 50px;
height: 50px;
}

.LowerClass{
background-color: blue;
width: 50px;
height: 50px;
}

现在,在代码背后我动态添加中间类div:

int count = 1; //Could be anything

foreach (var item in count)
{
    Label placeholder = new Label();
    StringBuilder sbExample = new StringBuilder();
    sbExample.append("<div class='MiddleClass'></div>");
    HtmlString text = new HtmlString(sbExample.ToString());
    placeholder.Text = text.ToString();
    this.Controls.Add(placeholder);
}

现在,所有3个div都显示在页面上,但是带有middleclass的div落后于大写,我想要的是高级第一个,然后是所有middleclass div,低于低级div,所有这些都很好地排序在下面彼此。我该怎么做?

提前致谢。

修改

另外,如果我想要一个包装器,我怎么能这样放置它:

<div class="wrapper">
    <div class="UpperClass">
    </div>
        //Middle class here
    <div class="LowerClass">
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

在前端有这个:

<div class="UpperClass"></div>

<%= PopulateMiddleClass() %>

<div class="LowerClass"></div>

并在后端

String PopulateMiddleClass()
{
    String ret = "";
    //loop through and populate string
    return ret;
}

或者

<div class="UpperClass"></div>

<asp:PlaceHolder ID="MyPlaceHolder" runat="server"></asp:PlaceHolder>

<div class="LowerClass"></div>

然后在后端

Page_Load(object sender, EventArgs e)
{
    Literal literal = new Literal();
    literal.Text = "<div class='MiddleClass'></div>";
    MyPlaceHolder.Controls.Add(literal); // you could loop through and keep adding controls or you could compile them all into the one literal.
}

希望这能让你顺利上路。

答案 1 :(得分:1)

您可以使用 runat =“server”属性创建一个div,并使用它,如下所示:

设计页面

<div class="wrapper">
    <div class="UpperClass">
    </div>
    <div runat="server" id="divMiddle">
    </div>
    <div class="LowerClass"></div>
</div>

代码背后:

int count = 1; //Could be anything

for (int i = 0; i < count; i++ )
{
    Label placeholder = new Label();
    StringBuilder sbExample = new StringBuilder();
    sbExample.Append("<div class='MiddleClass'></div>");
    HtmlString text = new HtmlString(sbExample.ToString());
    placeholder.Text = text.ToString();
    this.divMiddle.Controls.Add(placeholder);
}