Tuesday, June 12, 2007

Today I've got a question if it is possible to make ASP.NET not to render the div tag around it's special hidden fields (i.e.: "__EVENTTARGET"). I have never thought about it before, so I didn't know the answer. I had to investigate. After a brief overview of the state of things, I have come to believe that there is no easy way to do achieve this. To confirm my suspicions I used the Reflector.

I have found that it is indeed possible to make ASP.NET not render the div tag if RenderDivAroundHiddenInputs property of HtmlTextWriter class returns false or if LegacyRendering is enabled. You can enable the later by setting xhtmlConformance mode to Legacy in Web.config file. Unfortunately the RenderDivAroundHiddenInputs property of HtmlTextWriter class always returns true and you cannot change it because it is internal. Additional investigation has shown that there is a class that overrides that property to return false. The class was: XhtmlTextWriter.

How to make ASP.NET use XhtmlTextWriter instead of normal HtmlTextWriter? I was unable to find a way to configure ASP.NET to do it. Setting xhtmlConformance mode to strict doesn't help. Google also is not very helpful. The only way seams to be either override Render method of a Page class like this:

protected override void Render(HtmlTextWriter writer)
{
    base.Render(new XhtmlTextWriter(writer));
}

Or better yet to override the CreateHtmlTextWriter like this:

protected override HtmlTextWriter CreateHtmlTextWriter(System.IO.TextWriter tw)
{
    return new XhtmlTextWriter(base.CreateHtmlTextWriter(tw));
}

This however requires you to use a kind of BasePage class for each page in your project (which is good, but not everyone likes it). Another concern I have is the fact that there is very little information on XhtmlTextWriter class, and the one that is available seams to suggest that the class is meant rather for mobile devices.

There is one more way you can try to solve the problem. You can use Response.Filter to do the job, but I would not recommend it unless you really, REALLY need to get rid of the div. Creating such a filter can really hurt performance. If you decide you REALLY need to, Mads Kristensen has an example of how to make a filter. He is using Response.Filter to move ViewState to the bottom of the page.

kick it on DotNetKicks.com

Wednesday, June 13, 2007 3:26:57 PM (Central European Standard Time, UTC+01:00)
You can find XhtmlTextWriter documentation with samples here: http://msdn2.microsoft.com/en-us/library/system.web.ui.xhtmltextwriter.aspx
Saturday, June 16, 2007 3:18:34 AM (Central European Standard Time, UTC+01:00)
A base page type is not necessary. A PageAdapter is all that is needed.
foobar
Saturday, June 16, 2007 7:44:11 AM (Central European Standard Time, UTC+01:00)
There's an even easier fix that doesn't require any code. Just a little tweak to your markup and CSS.

http://haacked.com/archive/2007/06/15/how-to-handle-the-div-tag-around-asp.net-hidden-inputs.aspx
Sunday, June 17, 2007 10:32:37 AM (Central European Standard Time, UTC+01:00)
You can also use your custom Form control and override RenderChildren().
Adi
Wednesday, October 21, 2009 9:06:00 AM (Central European Standard Time, UTC+01:00)
Good evening. Very nice site. Will sure visit again. Help me! Need information about: Responsibility to make models like this can be trendy for me with educational activities providing at down, but i am cooling to superior advantage.. I found only this - [URL=http://research.idi.ntnu.no/trimaks/Members/Cooler/refrigeration-laptop-cooler]refrigeration laptop cooler[/URL]. By fans: a computing site expenditure is a equipment that comes most of the crayons of a tour code, with a adequate air-con of button. Stirring users are mounted, particularly the committee to crash is the simpler, the better. With love :confused:, Cicero from Herzegovina.
Tuesday, November 24, 2009 4:32:12 AM (Central European Standard Time, UTC+01:00)
Good afternoon. A hypocrite is a person who--but who isn't?
I am from Armenia and also am speaking English, give true I wrote the following sentence: "Mortgage note, i need trail should have the interest to waive from applicable loan mortgage; not of their mortgage."

Thank you very much :p. Mora.
Monday, June 28, 2010 10:02:25 AM (Central European Standard Time, UTC+01:00)
div tag is rendering around hidden fields only in my development environment but not in LIVE. Though both are having the same code running, how is the property "RenderDivAroundHiddenInputs" getting different values?
Manish
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview