如何在asp.net中获取页面名称并添加一些字符串
本文关键字:添加 字符串 asp net 获取 | 更新日期: 2023-09-27 17:50:46
我的问题是:我有两页
1-page.aspx用于英语模式
2-page-ar.aspx用于阿拉伯语模式
我有LinkeButton
点击我通过会话;
Session["lang"] = "ar";
或
Session["lang"] = "en";
我需要获得page.aspx名称并将此字符串"-ar"添加到阿拉伯语模式或从page-ar.aspx中删除"-ar"以转到英语模式
考虑到你可能在pageurl中有一些查询字符串。
您可以在代码隐藏页(例如page_Load(中使用以下代码:
protected string LinkUrl;
protected void Page_Load(object sender, EventArgs e)
{
var language = (string) Session["lang"] ?? "en";
LinkUrl = (language == "ar")
? Page.ResolveUrl("~/page-ar.aspx")
: Page.ResolveUrl("~/page.aspx");
}
在页面标记上,您可以放置这样的链接:
<a href="<%= LinkUrl %>">Language Demo</a>
这是Mr/seKhar帮助和我自己的搜索之后的答案
我有两个按钮的
一个用于阿拉伯语,另一个用于英语模式
当用户点击英文按钮时
protected void english_Click(object sender, EventArgs e)
{
string Path = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo Info = new System.IO.FileInfo(Path);
string pageName = Info.Name;
if (Session["lang"].ToString() == "ar")
{
string enlink = pageName.Substring(0, pageName.Length - 8) + ".aspx";
Session["lang"] = "en";
var page = (Page)HttpContext.Current.CurrentHandler;
string QueryString = page.ClientQueryString;
if (!(string.IsNullOrEmpty(QueryString)))
{
Response.Redirect(enlink + "?" + QueryString);
}
else
{
Response.Redirect(enlink);
}
}
}
并且当用户点击阿拉伯按钮时
protected void arabic_Click(object sender, EventArgs e)
{
string Path = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo Info = new System.IO.FileInfo(Path);
string pageName = Info.Name;
if (Session["lang"].ToString() == "en")
{
string arlink= pageName.Substring(0, pageName.Length - 5) + "-ar.aspx";
Session["lang"] = "ar";
//
var page = (Page)HttpContext.Current.CurrentHandler;
string QueryString = page.ClientQueryString; // this code get The Query String
if (!(string.IsNullOrEmpty(QueryString)))
{
Response.Redirect(arlink +"?"+ QueryString);
}
else
{
Response.Redirect(arlink);
}
}
}
希望这段代码能帮助到某人:(
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx
string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx
string host = HttpContext.Current.Request.Url.Host;
// localhost
您可以创建一个函数,该函数可以返回当前页面名称,如下所示
public string GetCurrentPageName()
{
string Path = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo Info = new System.IO.FileInfo(Path);
string pageName = Info.Name;
return pageName;
}
现在终于可以创建一个函数并传递会话值,如下所示
public string GetCurrentPageName(string fileName)
{
string Path = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo Info = new System.IO.FileInfo(Path);
string pageName = Info.Name;
if (fileName == "ar")
return pageName.Substring(0, pageName.Length - 7) + ".aspx";
else
return pageName.Substring(0, pageName.Length - 5) + "_ar.aspx";
}
在上面的函数中传递会话值,它就会工作。
注意:-我还没有测试过。
如果您正在开始设计系统,我建议您使用resource.resx文件。
要使UI多语言与资源查看本文:http://support.microsoft.com/kb/917414
对于从数据库中提取的数据,必须使用大量if语句。
If (arabic) {
Select arabic data
}
else
{
Select english data
}
如果你考虑使用resx文件,我可以帮助你提供更多信息。
有了这个解决方案,就没有字符串操作,查询字符串也会很好,一切都会正常工作。