从控件派生的ASP.NET自定义控件中渲染自我关闭标签

从控件派生的ASP.NET自定义控件中渲染自我关闭标签

问题描述:

我正在开发一个Facebook FBML控件库,并希望创建一些像ASP.NET WebControls库一样的FBML控件。我有一个基类默认处理渲染;这是我的渲染方法:

I'm working on a Facebook FBML controls library and would like to create my FBML controls somewhat patterned like the ASP.NET WebControls library. I have a base class that handles rendering by default; here's my render method:


        protected override void Render(HtmlTextWriter writer)
        {
            AddAttributesToRender(writer);

            if (UseXfbmlSemantics)
            {
                writer.RenderBeginTag(ElementName);
                writer.EndRender();
                writer.RenderEndTag();
            }
            else
            {
                writer.RenderBeginTag(ElementName);
                writer.RenderEndTag();
            }
        }

我想要的是渲染被修改基于UseXfbmlSemantics - 如果它是真的,它应该呈现,例如:

What I would like is for the rendering to be modified based on UseXfbmlSemantics - if it's true, it should render, for instance:

<fb:name uid="10300399458"></fb:name>

当它为false时,它应该使用自我关闭标签呈现:

When it's false, it should render with a self-closing tag:

<fb:name uid="10300399458" />

我可以使真实条件工作几乎正确,但自我关闭标签似乎与Render方法不兼容。不幸的是,如果是这样,这也意味着AddAttributesToRender模式也不起作用。它实际产生的是:

I can get the "true" condition to work almost correctly, but the self-closing tag seems to be incompatible with the Render- set of methods. Unfortunately if that's the case it also means that the AddAttributesToRender pattern wouldn't work, either. What it's actually producing is this:


        <fb:name uid="10300399458">

        </fb:name>

如何获取HtmlTextWriter(或者我需要使用哪个HtmlTextWriter),使其呈现自己关闭标签?或者,至少如何使它不会渲染临时空间(以便开始和结束标签紧紧相邻)?

How can I get HtmlTextWriter (or which HtmlTextWriter do I need to use) to make it render a self-closing tag? Or, at the very least, how can I make it not render that interim space (so that the opening and closing tags are immediately next to one another)?

这应该让你走 - 它将呈现为< fb:name uid =00101010101/> 。您也可以覆盖RenderBeginTag,RenderContents,RenderEndTag。根据您正在做的,您可能需要在RenderControl中进行其他一些操作。您还可以查看使用ControlAdapter,这可能会让您更好地分离控制功能VS控件html写入。

This should get you going - it will render as <fb:name uid="00101010101"/>. You could also override the RenderBeginTag, RenderContents, RenderEndTag. Depending on what you are doing there may be some other things going on in RenderControl that you need. You could also look into using a ControlAdapter, this may give you better separation of control functionality VS control html writing.

public  class FbName:System.Web.UI.WebControls.WebControl
{

    protected override string TagName
    {
        get
        {
            return "fb:name";
        }
    }

    public override void RenderControl(HtmlTextWriter writer)
    {  
        RenderBeginTag(writer);// render only the begin tag.
        //base.RenderContents(writer);
        //base.RenderEndTag(writer);
    }

    public override void RenderBeginTag(HtmlTextWriter writer)
    {
        writer.Write("<" + this.TagName);
        writer.WriteAttribute("uid", "00101010101");
        writer.Write("/>");

    }
}

-Jason