开发者

best way to set multiple html attribute without making a usercontrol or using literal control?

开发者 https://www.devze.com 2023-03-11 19:45 出处:网络
Trying to find the best way to dynamically add a direction attribute to each of these html elements. I know how to obtain the direction with

Trying to find the best way to dynamically add a direction attribute to each of these html elements. I know how to obtain the direction with

var dir = CultureInfo.CurrentCulture.TextInfo.IsRightToLeft ? "rtl" : "ltr";

but I need to find a graceful way to dynamically add it to the following html

<!--[if lt IE 7]><html class="ie6" lang="en"><![endif]-->;
<!--[if IE 7]><html class="ie7" lang="en"><![endif]-->
<!--[if IE 8]><html class="ie8" lang="en"><![endif]-->
<!--[if gt IE 8]><!--><html lang="en"><!--<![endif]-->

If I put this into a User Control and assign each html element an id, then set each control's direction attribute, I get runtime errors for not having a closing html tag in the user control.

Is there any simple way for me to set the dir attribute for each of these html elements without doing it through a user control? I'm not even able to place a <asp:Literal> control within each html tag, and it is beginning to frustrate me. Thanks

edit: the end result should be

<!--[if lt IE 7]><html class="ie6" lang="en" dir="ltr"><![endif]-->;
<!--[if IE 7]><html class="ie7" lang="en" dir="ltr">&开发者_JAVA百科lt;![endif]-->
<!--[if IE 8]><html class="ie8" lang="en" dir="ltr"><![endif]-->
<!--[if gt IE 8]><!--><html lang="en" dir="ltr"><!--<![endif]-->


You could use the <asp:Literal> control to output the entire result. You can generate the entire excerpt in code-behind and then feed it to the literal control.

Or, you could create a public function:

public string Direction() {
    return (CultureInfo.CurrentCulture.TextInfo.IsRightToLeft) ? "rtl" : "ltr";
}

And call it from within your aspx page:

<!--[if lt IE 7]><html class="ie6" lang="en" <%=Direction()%>><![endif]-->;
<!--[if IE 7]><html class="ie7" lang="en" <%=Direction()%>><![endif]-->
<!--[if IE 8]><html class="ie8" lang="en" <%=Direction()%>><![endif]-->
<!--[if gt IE 8]><!--><html lang="en" <%=Direction()%>><!--<![endif]-->


Put a runat=server in the tag:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" id="html_tag" runat="server">

then add any of the conditional attributes you need in your code behind, such as:

html_tag.Attributes.Add("dir", "ltr"); 


This can also be set using CSS as well, and if you're setting multiple items, it may be to your advantage to do it this way. Have your items use an additional class, like:

Somewhere else on the page (preferable above), put a Literal on the page and make it's text emit an inline style:

Literal.Text = String.Concat("<style> .useLTR {", CultureInfo.CurrentCulture.TextInfo.IsRightToLeft ? "direction: rtl;" : String.Empty, "} </style>");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号