Scott Hanselman

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

March 27, 2003 Comment on this post [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. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
March 27, 2003 14:59
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...

March 27, 2003 23:57
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:
March 28, 2003 1:00
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.

April 03, 2003 4:52
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).

April 03, 2003 4:54
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.