2011-09-23 79 views
2

我有我寫的服務器控件,它通常工作正常。但是,當我加入突出顯示的行時,它不會添加一個,而是兩個<br />元素,這不是我所追求的。服務器控件行爲奇怪

mounting=new DropDownLabel(); 
mounting.ID="mountTypeList"; 
mounting.Attributes.Add("class", "mounting"); 
mounting.Values=Configuration.MountTypes.GetConfiguration().Options; 
mounting.Enabled=Utilities.UserType == UserType.Admin; 
mounting.Value=value.Reference; 
td1.Controls.Add(mounting); 
**td1.Controls.Add(new HtmlGenericControl("br"));** 
var span=new HtmlGenericControl("span"); 
span.Attributes.Add("class", "mountDescription"); 
span.ID="mountDescription"; 
td1.Controls.Add(span); 

對我在做什麼有什麼想法嗎?

ETA:

我已經通過增加使用jQuery的BR,這我使用反正有解決的情況。但我看到的行爲肯定是錯誤的。如果我添加一個元素,它應該添加該元素,而不是該元素的兩倍。

回答

4

HtmlGenericControl將與開始和結束標記<br></br>

產生的,而不是你可以使用new LiteralControl("<br/>")應該做你的願望。

編輯

爲了得到這個你身邊需要自己實現HtmlGenericControl,並延長它不具備相關的開始和結束標記這樣的情況。

public class HtmlGenericSelfClosing : HtmlGenericControl 
{ 
    public HtmlGenericSelfClosing() 
     : base() 
    { 
    } 

    public HtmlGenericSelfClosing(string tag) 
     : base(tag) 
    { 
    } 

    protected override void Render(HtmlTextWriter writer) 
    { 
     writer.Write(HtmlTextWriter.TagLeftChar + this.TagName); 
     Attributes.Render(writer); 
     writer.Write(HtmlTextWriter.SelfClosingTagEnd); 
    } 

    public override ControlCollection Controls 
    { 
     get { throw new Exception("Self-closing tag cannot have child controls"); } 
    } 

    public override string InnerHtml 
    { 
     get { return String.Empty; } 
     set { throw new Exception("Self-closing tag cannot have inner content"); } 
    } 

    public override string InnerText 
    { 
     get { return String.Empty; } 
     set { throw new Exception("Self-closing tag cannot have inner content"); } 
    } 
} 

Found Here

+0

都能跟得上 - 這只是增加了與它BR跨度。生成的代碼只有正確的標籤,只有兩次。 –

+0

謝謝,我遇到了同樣的問題,您的第二個解決方案解決了它。 – Marlon

+0

Thanks,'div.Controls.Add(new LiteralControl(「
」);'爲我工作,在這個問題也類似的答案; http://stackoverflow.com/q/3095228/1847645 –