Scott Hanselman

Programmatically modifying DIR=LTR for Right-To-Left Languages in ASP.NET

March 26, '03 Comments [5] Posted in Web Services | ASP.NET
Sponsored By

Has anyone given any thought on how to programmatically modify/add the dir= attribute to the HTML or BODY tags when the Thread's Culture in ASP.NET is a Right To Left language?

This doesn't work reliablity, as ASP.NET may batch up controls into an HtmlLiteralControl...often, not always, the body (or html) tag can't be found.  This depends on how ASP.NET decided to parse the control tree for a particular page.  Additionally, there doesn't seem to be anything in CultureInfo that would help me decide what languages are right to left...

If I can reliably get ahold of the HTML control and BODY tags and add this attribute, I'm good.

/* Doesn't work reliably */

HtmlGenericControl bodytag = (HtmlGenericControl)FindControl("body");
if (Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName == "ar")
   bodytag.Attributes["dir"] = "rtl";
else
   bodytag.Attributes["dir"] = "rtl";

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Thursday, March 27, 2003 10:59:09 AM UTC
Have you tried applying the runat and ID tags to the body (and/or head) tag itself? You shouldn't need to do a Page.FindControl then...

John St. Clair
Thursday, March 27, 2003 7:57:21 PM UTC
Maybe you could put some code in your page to emit/register some client-side javascript to set this attribute as you please. Something like:
Sergio Pereira
Thursday, March 27, 2003 9:00:30 PM UTC
Both of these are great ideas...the 1st is good, but requires a runat=server for every page on the site. I like it though.

Thursday, April 03, 2003 12:52:26 AM UTC
To add attributes to <body> I do a bit of butching by overriding OnPreRender for page and using regular expressions. Works quite well (you can add event handler instead but overriding was easier for me as I have base class for all pages).

Thursday, April 03, 2003 12:54:21 AM UTC
Wow! Cut'n'paste code from Visual Studio, obviously, was not a very bright idea :-P
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.