When writing a custom control it always rendered as an HTML span element. How can I change it for example to a div?
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- How to store image outside of the website's ro
- 'System.Threading.ThreadAbortException' in
- Request.PathInfo issues and XSS attacks
- How to dynamically load partial view Via jquery aj
相关文章
- asp.net HiddenField控件扩展问题
- asp.net HiddenField控件扩展问题
- Asp.Net网站无法写入错误日志,测试站点可以,正是站点不行
- asp.net mvc 重定向到vue hash字符串丢失
- FormsAuthenticationTicket expires too soon
- “Dynamic operations can only be performed in homog
- What is the best way to create a lock from a web a
- Add to htmlAttributes for custom ActionLink helper
I normally have my own base class that all my composite controls inherit from. One of the properties I add to this is a ContainerElement. Publicly exposed, the developer can choose what ever outer element they want. Internally it sets the TagKey property which governs this rendering on the base control. All the following to your control/base class.
You just need to set HTMLContainerElement which will have the inteli-help of all the items in the HtmlTextWriterTag enumeration.
Derive your control from WebControl as follows:
That is, use the base class constructor that accepts the tag to use.
If you derive from CompositeControl there is no constructor that takes a tag type. You can override TagKey (I haven't tried it), but a more flexible option is to override the RenderBeginTag method and make it do what you want. The base class renders a "span" opening element, but you don't have to call the base class method. You don't have to call anything if you don't want anything rendered (in which case also override RenderEndTag and don't call anything there either). For example,
This code produces
which is exactly what I needed for this particular composite control of mine, a div with a class to wrap a ReportViewer control. I include the ID just to make the output easier to spot.