3

There seems to be a problem with the ASP textbox control when set to multiline and serving the page as xhtml. The project I am working on uses content negotiation to serve asp pages as application/xhtml+xml to browsers which support it. The problem is when asp textbox renders a textarea to the page, it explicitly prepends a newline to the text. Reflection of the textbox's render method looks like the following:

if (TextMode == TextBoxMode.MultiLine)
HttpUtility.HtmlEncode(Environment.NewLine + this.Text, (TextWriter) writer);

When firefox and opera are served this with xhtml content type, they interpret the newline as part of the text in the textarea and so I get extra newlines at the beginning of my text areas.

I could subclass textbox and override render, but that seems like a bit of overkill to correct something like this. Is there another way to correct this? And does anyone know why asp textbox does this anyway?

1 Answers1

1

An alternative to subclassing is to use control adapters, or to write the <textarea by hand and get ASP.NET to generate the control name attribute for you.

I suspect ASP.NET WebForms does this simply because of a short-sight. The future is MVC anyway, so don't expect this to be changed any time soon. I suspect the original purpose of the newline is to give the textarea a "value" rather than nothing (thus making the textarea "successful" in HTML forms parlance).

This isn't the only odd behaviour you'll see in ASP.NET. The atrocious HTML formatting of <head runat="server"> is also on the list.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • I think we will go with the control adapter approach as it will apply to all of our existing textareas. The downside is that all of the rendering still has to be reimplemented, but I don't see much of a way around that. Never heard of control adapters before, thanks for pointing them out. – Dave Cawley Aug 01 '12 at 13:33