2013-04-01 120 views
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> 
+3

使用 要麼在代碼後面或在html中保留空白? – adaam

回答

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

enter image description here

我喜歡在一個時間,而不是在一次一列創建控件一行,但不是OP。