Is it possible to force a WebControl to render as a <div> instead of a <span>?
-
24-06-2021 - |
سؤال
How can I force the WebControl
to render as a <div>
? Currently, it renders as a <span>
and our UI guy prefers it be a <div>
. To accommodate him, I'd like to see if this is possible and if so, how it's possible.
المحلول
Yes, it is definitely possible. Asp.net gives you full control over the output rendered.
Take a look at Control Adapters
The asp.net team has released many adapters for CSS friendly rendering for controls like Menu, TreeView, and FormView. Take a look at CSS friend control adapters
You can always build your own to customize the rendering as needed by your UI guy.
نصائح أخرى
You can override the RenderBeginTag method of WebControl:
public override void RenderBeginTag(HtmlTextWriter writer)
{
writer.RenderBeginTag("div");
}
There's also a RenderEndTag that you can override, which might not be necessary in this case:
public override void RenderEndTag(HtmlTextWriter writer)
{
writer.RenderBeginTag();
}
Probably better to override TagKey
like so:
protected override HtmlTextWriterTag TagKey => HtmlTextWriterTag.Div;
Then any attributes the WebControl
would render continue to be placed on opening tag.