3

我正在尝试在 asp.net 上构建一个多语言网站,我需要将 html 标记 dir 属性从 rtl 设置为 ltr,反之亦然,以编程方式在 c 语言中进行设置。现在我有 3 种语言英语、希伯来语和俄语,我需要在它们之间切换。每次我从 ltr 更改为 rtl 语言以及从 rtl 更改为 ltr 语言时,我都需要更改网站的方向,这里我的母版页示例请帮助我

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs"     Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="CSS/Site-RTL.css" rel="stylesheet" type="text/css" />
</head>
<body id="html" runat="server">
<form id="theForm" runat="server">
<div>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder" runat="server">
    </asp:ContentPlaceHolder>
</div>
<div style="margin-top: 20px;">
    <asp:LinkButton ID="btnSetEnglish" runat="server" Text="English" CommandArgument="en-GB"
        OnClick="RequestLanguageChange_Click"></asp:LinkButton>&nbsp;&nbsp;
    <asp:LinkButton ID="btnSetHebrew" runat="server" Text="Hebrew" CommandArgument="he-IL"
        OnClick="RequestLanguageChange_Click"></asp:LinkButton>&nbsp;&nbsp;
    <asp:LinkButton ID="btnSetRussian" runat="server" Text="Russian" CommandArgument="ru-RU"
        OnClick="RequestLanguageChange_Click"></asp:LinkButton>&nbsp;&nbsp;
</div>
<div>
    <asp:SiteMapDataSource ID="smdsMaster" runat="server" OnDataBinding="Page_Load" />
    <asp:Menu ID="MenuMaster" runat="server" CssClass="menu" DataSourceID="smdsMaster"
        Orientation="Horizontal" StaticDisplayLevels="2" StaticSubMenuIndent="16px" MaximumDynamicDisplayLevels="1">
        <DataBindings>
            <asp:MenuItemBinding DataMember="SiteMapNode" NavigateUrlField="Url" />
        </DataBindings>
    </asp:Menu>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
    </asp:ContentPlaceHolder>
</div>
</form>

这里是母版页后面的代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Localization.Classes;

public partial class MasterPage : System.Web.UI.MasterPage
{

protected void Page_Load(object sender, EventArgs e)
{


}

protected void RequestLanguageChange_Click(object sender, EventArgs e)
{
    LinkButton senderLink = sender as LinkButton;

    //store requested language as new culture in the session
    if (senderLink.CommandArgument == "he-IL")
    {
        // some code
    }
    else
    {
        // some code
    }
    Session[Global.SESSION_KEY_CULTURE] = senderLink.CommandArgument;

    //reload last requested page with new culture
    Server.Transfer(Request.Path);

}
}
4

1 回答 1

4

在您后面的代码中:

this.html.Attributes.Add("dir", "ltr");

如果这不起作用,另一种方法是将属性直接放在标签上并使用属性来填充它:

<body dir="<%:Dir%>">

public string Dir { get; set; }

// Set Dir in the on click event

protected void RequestLanguageChange_Click(object sender, EventArgs e)
{
    LinkButton senderLink = sender as LinkButton;

    //store requested language as new culture in the session
    if (senderLink.CommandArgument == "he-IL")
    {
        Dir = "rtl";
    }
    else
    {
        Dir = "ltr";
    }

    Session[Global.SESSION_KEY_CULTURE] = senderLink.CommandArgument;

    //reload last requested page with new culture
    Server.Transfer(Request.Path);

}
于 2012-05-11T10:06:48.603 回答