首页 > 解决方案 > 在 JavaScript 中使用 Asp.Net WebForms 上的资源本地化文本

问题描述

我有基于 c# 的 ASP.NET WebForms 应用程序。

我有图像按钮

<asp:ImageButton ID="divSection_btnAdd" runat="server" 
OnClientClick="return  TConfirm(this,'<%$Resources:Resource, Confirm%>')"/>

问题: 不显示资源值但显示 '<%$Resources:Resource, Confirm%>'

Resources:Resource.Confirm = '你确定要删除这个项目吗?'

如何显示资源键值?

标签: javascriptasp.netwebformsresources

解决方案


请用 :

c# 后面的 1-in 页面代码:

 protected override void Render(HtmlTextWriter writer)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter hWriter = new HtmlTextWriter(sw);
        base.Render(hWriter);
        writer.Write(this.Localize(sb.ToString()));
    }
 private const string ResourceFileName = "Resource";
    private string Localize(string html)
    {
        MatchCollection matches = new Regex(@"Localize\(([^\))]*)\)", RegexOptions.Singleline | RegexOptions.Compiled).Matches(html);
        foreach (System.Text.RegularExpressions.Match match in matches)
        {
            html = html.Replace(match.Value, GetGlobalResourceObject(ResourceFileName, match.Groups[1].Value).ToString());
        }
        return html;
    }

编辑图像按钮:

<asp:ImageButton 
    ID="divSection_btnAdd" 
    runat="server"     
    OnClientClick="return  TConfirm(this,'Localize(Confirm)')"/>

推荐阅读