开发者

Adding a <br /> after a label only if label is visible. C#

I have a chunk of code that on page load with populates some of or all of the following labels. It should have two labels per line ( needs a line break after each xData label). The problem I am having is that since the number of labels with data and set to visable on page load changes, the br / tags cause spacing issues when not all labels are visible.

<div id="Status">
    <asp:Label ID="1" runat="server" Text="1:" Width=开发者_StackOverflow中文版"125px" Visible="false" />
    <asp:Label ID="1Data" runat="server" Text="" Visible="false" />
    <asp:Label ID="2" runat="server" Text="2:" Width="125px" Visible="false" />
    <asp:Label ID="2Data" runat="server" Text="" Visible="false" />
    <asp:Label ID="3" runat="server" Text="3:" Width="125px" Visible="false" />
    <asp:Label ID="3Data" runat="server" Text="" Visible="false" />
</div>

I would like to be able to add the line breaks after each "xData" label in the code behind when the labels are filled and set to visible.

I have tried adding "\n" to the label text and\or Environment.NewLine with no luck.

Thanks for any help


The easy way...

<div id="Status">
    <div id="Status1" runat="server" Visible="false">
        <asp:Label ID="1" runat="server" Text="1:" Width="125px" />
        <asp:Label ID="1Data" runat="server" Text="" />
    </div>
    <div id="Status2" runat="server" Visible="false">
        <asp:Label ID="2" runat="server" Text="2:" Width="125px" />
        <asp:Label ID="2Data" runat="server" Text="" />
    </div>
    <div id="Status3" runat="server" Visible="false">
        <asp:Label ID="3" runat="server" Text="2:" Width="125px" />
        <asp:Label ID="3Data" runat="server" Text="" />
    </div>
</div>

The right way...

<div id="Status">
    <asp:Label CssClass="statusLabel" ID="1" runat="server" Text="1:" Width="125px" Visible="false" />
    <asp:Label ID="1Data" runat="server" Text="" Visible="false" />
    <asp:Label CssClass="statusLabel" ID="2" runat="server" Text="2:" Width="125px" Visible="false" />
    <asp:Label ID="2Data" runat="server" Text="" Visible="false" />
    <asp:Label CssClass="statusLabel" ID="3" runat="server" Text="3:" Width="125px" Visible="false" />
    <asp:Label ID="3Data" runat="server" Text="" Visible="false" />
</div>

/* CSS */
#Status span {
  display: block;
}
#Status .statusLabel {
  clear: both;
  float: left;
}


I think you could do it a couple of different ways.

One option would be what @Greg points out in his comment to your post.

Another possible option would be enclosing each label in its own <div> tag with runat="server" and then make these <div>s visible when needed. The <div> should create its own line break because of the nature of a <div>


asp:Label resolves to a span in html. If you want each one to have its own line, add the css style "display:block". Usually, you can do this by setting CssClass and put display:block in that class


If you want this way, you need to use Literal control for each BR tag so that you can set it to visible/invisible based on the visibility of corresponding Label control.


Why not just add a CSS class with a display: block rule to those labels?

This is presentational, after all.


Try wrapping the labels around a <div> instead of putting a <br /> at the end. In my experience, empty <div>'s don't create that empty space.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜