开发者

implementing runat in HtmlTextWriterAttribute

开发者 https://www.devze.com 2023-02-02 02:14 出处:网络
Hi dear friends, i have web custom control this render function of Window class protected override void RenderContents(HtmlTextWriter wr)

Hi dear friends, i have web custom control

this render function of Window class

    protected override void RenderContents(HtmlTextWriter wr)
    {
        wr.AddAttribute("runat", "server",true);
        wr.AddAttribute("id", this.UniqueID, false);
        wr.RenderBeginTag(HtmlTextWriterTag.Div);
        wr.RenderEndTag();
        wr.WriteLine();
        base.RenderContents(wr);
    }


Use on web page:

(cc1:Window开发者_JAVA百科 ID="Window1" runat="server" ) (div) runat="server" id="aaaa"(/div)


Browser sourse result:

(span id="Window1")(div runat="server" id="Window1") (/div) span) (div) id="aaaa"(/div)


You never once in your question actually state a question. Anyhow, I think you want your custom control to render like a <div runat="server">.

public class Window : WebControl {
    public Window() : base(HtmlTextWriterTag.Div) {}

    protected override void AddAttributesToRender(HtmlTextWriter writer) {
        writer.AddAttribute(HtmlTextWriterAttribute.Id, UniqueID);
    }
}
  1. You should call the constructor on WebControl which accepts a HtmlTextWriterTag.
  2. You should override AddAttributesToRender (instead of RenderContents) to add attributes to your element.
  3. runat="server" is only used during parsing of your html code, and have no use in your controls.


I am not sure if your example of use is just typo and formatting errors when posting, or if it is actually this way you use it (exept I understand that you posted <...> as (...), which is not my issue here).


You should start with implementing your control class as suggested by Simon Svensson.

Then, if you want your div with id="aaaa" be a html tag inside your controls div tag, you should use it like this (note the ending </cc1:Window>):

Use on web page:

<cc1:Window ID="Window1" runat="server">
  <div id="aaaa">Text content here</div>
</cc1:Window>

Browser source result:

<div id="Window1">
  <div id="aaaa">Text content here</div>
</div>

As Simon said, runat="server" is mainly used for server controls, and has no meaning for the rendered HTML controls.

0

精彩评论

暂无评论...
验证码 换一张
取 消