如何获取页面名称并在asp.net中添加一些字符串?

时间:2013-01-06 09:52:18

标签: c# jquery asp.net

我的问题:我有两页

1- page.aspx for English language mode

用于阿拉伯语言模式的

2- page-ar.aspx

我点击了LinkeButton我通过了会话;

Session["lang"] = "ar";

Session["lang"] = "en";

我需要获取page.aspx名称并将此字符串“-ar”添加到转到阿拉伯语模式 或者从page-ar.aspx中删除“-ar”以转到英文模式

考虑一下,也许在pageurl中有一些查询字符串。

4 个答案:

答案 0 :(得分:2)

您可以在代码隐藏页面中使用以下代码(例如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>

答案 1 :(得分:2)

这是 Mr / sheKhar 帮助和我自己的搜索之后的答案

我有两个按钮

一个用于阿拉伯语,另一个用于英语模式

当用户点击英语按钮

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

}

希望这段代码可以帮助某人:)

答案 2 :(得分:1)

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";    
 } 

在上面的函数中传递会话值,它将起作用。

注意: - 我没有测试过。

答案 3 :(得分:1)

如果您刚开始设计系统,我建议您使用资源 .resx 文件。

要使用资源制作UI多语言,请查看以下文章:http://support.microsoft.com/kb/917414

对于从数据库中提取的数据,您必须使用大量的if语句。

If (arabic) {
     Select arabic data
}
else
{
     Select english data
}

如果您考虑使用resx文件,我可以为您提供更多信息。

使用这个解决方案,没有字符串操作,并且查询字符串将很好,一切都会起作用。