服务器控件行为怪异 [英] Server control behaving oddly

查看:164
本文介绍了服务器控件行为怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我写了一个服务器控件,它通常工作正常。然而,当我在突出显示的行补充,它增加了不是一个而是两个< BR /> 元素,这不是我所追求的。

I have a server control that I have written, which generally works fine. However when I add in the highlighted line, it adds in not one but two <br /> elements, which is not what I am after.

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

这是我在做什么任何想法错了吗?

Any thoughts on what I am doing wrong?

ETA:

我通过添加使用jQuery的BR,而我使用反正有解决的情况。但我看到的行为肯定是错误的。如果我添加一个元素,它应该添加元素,而不是两倍元素。

I have resolved the situation by adding the br using jquery, which I am using there anyway. But the behaviour I saw is surely wrong. If I add an element, it should add that element, not twice that element.

推荐答案

HtmlGenericControl 将生成的开始和结束标记&LT; BR&GT; &LT; / BR&GT;

HtmlGenericControl will generate the with the opening and closing tags <br> and </br>

相反,您可以使用新LiteralControl(&LT; BR /&gt;中)。这应该做你的愿望是什么

instead you could use new LiteralControl("<br/>") which should do what you desire.

修改

要解决这个问题,你需要自己实现的 HtmlGenericControl 并延长它不具备开始和结束标记此类案件有关。

To get around this you will need your own implementation of the HtmlGenericControl and extend it for such cases which don't have opening and closing tags associated.

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

在这里找到

这篇关于服务器控件行为怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆