asp.net - 服务器控制行为异常

标签 asp.net custom-server-controls

我有一个我编写的服务器控件,通常工作正常。但是,当我添加突出显示的行时,它添加的不是一个而是两个 <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);

对我做错了什么有什么想法吗?

预计到达时间:

我已经通过使用 jquery 添加 br 解决了这个问题,无论如何我都在那里使用它。但我看到的行为肯定是错误的。如果我添加一个元素,它应该添加该元素,而不是该元素的两倍。

最佳答案

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

关于asp.net - 服务器控制行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7526985/

相关文章:

asp.net - JQuery BlockUI 与 UpdatePanel View 状态问题

asp.net - 继承自asp :validationsummary

javascript - asp net 中的确认是或否消息

c# - 绑定(bind)时 JSON 属性区分大小写

asp.net - 如何在asp-Repeater项目模板中显示模式?

ASP.NET 2.0 将样式动态添加到控件中的页面

asp.net - 我应该购买 Obout 控件吗?

c# - 自定义控件 : Scripts not loaded when control is hidden on pageload

asp.net - 是否可以强制 WebControl 呈现为 <div> 而不是 <span>?

javascript - 没有列表的组单选按钮