6

我的间距怎么了?当标签包含文本时,间距很好,但它们是空的,它们在每个标签之间都有空白。当标签包含空文本时,为什么要添加额外的空间?

在标签中使用文本(我希望它看起来像):

标签中没有文字(我不希望它看起来像这样):

在后面的 C# 代码上:

        // On page load
        for (int i = 1; i < 10; i++)
        {
            string ID = i.ToString();
            PopulateLastNameLabel(ID);
        }


        protected void PopulateLastNameLabel(string ID)
        {
            Label lbl = new Label();
            lbl.Width = 70;
            lbl.Height = 20;
            lbl.Text = "";
            lbl.BackColor = System.Drawing.Color.Red;
            lbl.ID = "lastname_" + ID;
            pnlLastNameLabel.Controls.Add(lbl);
        }

在 ASP.NET 方面:

<asp:Table ID="tblDisplayTable" runat="server">
    <asp:TableRow>
    <asp:TableCell>
    <asp:Panel ID="pnlPrizeNumberLabel" runat="server" Width="80px"></asp:Panel>
    </asp:TableCell>
    <asp:TableCell HorizontalAlign="Center" VerticalAlign="Middle">
    <asp:Panel ID="pnlPrizeDropDownList" runat="server" Width="130px"></asp:Panel>
    </asp:TableCell>
    <asp:TableCell>
    <asp:Panel ID="pnlNickNameLabel" runat="server" Width="70px"></asp:Panel>
    </asp:TableCell>
    <asp:TableCell>
    <asp:Panel ID="pnlPrizeNicknameTextBox" runat="server" Width="125px"></asp:Panel>
    </asp:TableCell>
    <asp:TableCell>
    <asp:Panel ID="pnlFirstNameLabel" runat="server" Width="70px"></asp:Panel>
    </asp:TableCell>
    <asp:TableCell>
    <asp:Panel ID="pnlLastNameLabel" runat="server" Width="70px"></asp:Panel>
    </asp:TableCell>
    <asp:TableCell>
    <asp:Panel ID="pnlEmailAddressLabel" runat="server" Width="140px"></asp:Panel>
    </asp:TableCell>
    <asp:TableCell>
    <asp:Panel ID="pnlAddButton" runat="server" Width="40px"></asp:Panel>
    </asp:TableCell>
    </asp:TableRow>
    </asp:Table>
4

1 回答 1

2

如果我正确理解您的问题,您需要以下 css(从后面的代码中删除高度)。

每个控件的问题都在自己的表格单元格中,因此它们不会正确水平对齐。因此,您需要为每个控件显式设置高度。

<style type="text/css">
    td div { margin-right: 5px; }
    td div input {border: 1px solid #828282; height: 21px; }
    td div span { height: 25px; line-height: 25px; }
</style>

<asp:Table ID="tblDisplayTable" runat="server" CellPadding="0" CellSpacing="0">
    <asp:TableRow>
        <asp:TableCell>
            <asp:Panel ID="pnlPrizeNumberLabel" runat="server" Width="80px">
            </asp:Panel>
        </asp:TableCell>
        <asp:TableCell HorizontalAlign="Center" VerticalAlign="Middle">
            <asp:Panel ID="pnlPrizeDropDownList" runat="server" Width="130px">
            </asp:Panel>
        </asp:TableCell>
        <asp:TableCell>
            <asp:Panel ID="pnlNickNameLabel" runat="server" Width="70px">
            </asp:Panel>
        </asp:TableCell>
        <asp:TableCell>
            <asp:Panel ID="pnlPrizeNicknameTextBox" runat="server" Width="125px">
            </asp:Panel>
        </asp:TableCell>
        <asp:TableCell>
            <asp:Panel ID="pnlFirstNameLabel" runat="server" Width="70px">
            </asp:Panel>
        </asp:TableCell>
        <asp:TableCell>
            <asp:Panel ID="pnlLastNameLabel" runat="server" Width="70px">
            </asp:Panel>
        </asp:TableCell>
        <asp:TableCell>
            <asp:Panel ID="pnlEmailAddressLabel" runat="server" Width="140px">
            </asp:Panel>
        </asp:TableCell>
        <asp:TableCell>
            <asp:Panel ID="pnlAddButton" runat="server" Width="40px">
            </asp:Panel>
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 1; i < 10; i++)
    {
        string ID = i.ToString();
        TextBoxLabel(ID);
        PopulatePrizeNicknameLabel(ID);
        PopulateFirstNameLabel(ID);
        PopulateLastNameLabel(ID);
    }
}

protected void TextBoxLabel(string ID)
{
    TextBox lbl = new TextBox();
    lbl.Width = 65;
    lbl.Text = "";
    lbl.ID = "TextBox_" + ID;
    lbl.Text = ID;
    pnlNickNameLabel.Controls.Add(lbl);
}

protected void PopulatePrizeNicknameLabel(string ID)
{
    Label lbl = new Label();
    lbl.Width = 125;
    lbl.Text = "";
    lbl.BackColor = System.Drawing.Color.Green;
    lbl.ID = "PrizeNickname_" + ID;
    lbl.Text = ID;
    pnlPrizeNicknameTextBox.Controls.Add(lbl);
}

protected void PopulateLastNameLabel(string ID)
{
    Label lbl = new Label();
    lbl.Width = 70;
    lbl.Text = "";
    lbl.BackColor = System.Drawing.Color.Red;
    lbl.ID = "lastname_" + ID;
    lbl.Text = ID;
    pnlLastNameLabel.Controls.Add(lbl);
}

protected void PopulateFirstNameLabel(string ID)
{
    Label lbl = new Label();
    lbl.Width = 70;
    lbl.Text = "";
    lbl.BackColor = System.Drawing.Color.Blue;
    lbl.ID = "firstname_" + ID;
    lbl.Text = ID;
    pnlFirstNameLabel.Controls.Add(lbl);
}

在此处输入图像描述

我更喜欢一次创建一行而不是一次创建一列控件,但这不是 OP。

于 2013-04-01T19:03:56.583 回答