gpt4 book ai didi

c# - 如何在 c sharp 中以编程方式将 html 标签 dir 属性从 rtl 设置为 ltr,反之亦然

转载 作者:可可西里 更新时间:2023-11-01 12:52:04 26 4
gpt4 key购买 nike

我正在尝试在 asp.net 上构建一个多语言网站,我需要在 c sharp 中以编程方式将 html 标记 dir 属性从 rtl 设置为 ltr,反之亦然。现在我有 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);

}
}

最佳答案

在你后面的代码中:

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);

}

关于c# - 如何在 c sharp 中以编程方式将 html 标签 dir 属性从 rtl 设置为 ltr,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10549312/

26 4 0