结合MS AJAX将资源文件编译到动态链接库

本文讲述了创建一个将js和资源文件编译进动态连接库,并实现资源本地化的一个例子。
代码:EmbeddedResource.rar
下面是实现的具体步骤:

一、创建类库项目LocalizingScriptResources

二、添加System.Web System.Web.Extensions 命名控件引用。

三、添加一个Jscript文件。

四、将如下代码加入到js文件中:

  function CheckAnswer()

{

 

    var firstInt = $get('firstNumber').innerText;

    var secondInt = $get('secondNumber').innerText;

    var userAnswer = $get('userAnswer');

 

    if (parseInt(firstInt)+parseInt(secondInt) ==userAnswer.value)

    {

        alert(Answer.Correct);

        return true;

    }

    else

    {

         alert(Answer.Incorrect);

         return false;

    }

 }

 

五、右键js文件的属性,在高级里将“生成操作”设置成“嵌入的资源”。

 

六、添加类ClientVerification 代码如下:

using System;

using System.Collections.Generic;

using System.Text;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Resources;

 

 

namespace LocalizingScriptResources

{

    public class ClientVerification : Control

    {

        private Button _button;

        private Label _firstLabel;

        private Label _secondLabel;

        private TextBox _answer;

        private int _firstInt;

        private int _secondInt;

 

        protected override void CreateChildControls()

        {

            Random random = new Random();

            _firstInt = random.Next(0, 20);

            _secondInt = random.Next(0, 20);

 

            ResourceManager rm = new ResourceManager("LocalizingScriptResources.VerificationResources", this.GetType().Assembly);

            Controls.Clear();

 

            _firstLabel = new Label();

            _firstLabel.ID = "firstNumber";

            _firstLabel.Text = _firstInt.ToString();

 

            _secondLabel = new Label();

            _secondLabel.ID = "secondNumber";

            _secondLabel.Text = _secondInt.ToString();

 

            _answer = new TextBox();

            _answer.ID = "userAnswer";

 

            _button = new Button();

            _button.ID = "Button";

            _button.Text = rm.GetString("Verify");

            _button.OnClientClick = "return CheckAnswer();";

 

            Controls.Add(_firstLabel);

            Controls.Add(new LiteralControl(" + "));

            Controls.Add(_secondLabel);

            Controls.Add(new LiteralControl(" = "));

            Controls.Add(_answer);

            Controls.Add(_button);

        }

    }

}

 

上边的代码创建了一个 ASP.NET控件。这个控件包含两个文本框、一个Label控件,和一个按钮。Label控件用来显示两个随机的数字,往文本框里输入这两个数字的和,点击按钮就会调用CheckAnswer函数。

 

七、向项目中添加一个资源文件VerificationResources.resx

 

八、添加三个字符串资源,如下:

 

名称                                    

Correct              Yes, your answer is correct

Incorrect           No, your answer is incorrect

Verify                 Verify Answer

 

九、重复七、八的步骤,添加两个资源文件VerificationResources.it.resxVerificationResources.it.resxVerificationResources.zh-cn.resx,给这几个键添加不同语言的值。

 

名称                                    

Correct              Si, la risposta e’ corretta.

Incorrect           No, la risposta e’ sbagliata.

Verify                 Verificare la risposta

 

 

Correct              答案正确。

Incorrect           答案错误。

Verify                 检查答案

 

十、向AssemblyInfo文件中添加如下行。

[assembly: System.Web.UI.WebResource("LocalizingScriptResources.CheckAnswer.js", "application/x-javascript")]

[assembly: System.Web.UI.ScriptResource("LocalizingScriptResources.CheckAnswer.js", "LocalizingScriptResources.VerificationResources", "Answer")]

         由于在js函数中我们引用了Answer所以这里就只能是Answer,当然也可以叫做别的。

十一、编译程序。

 

 

测试程序:

         一、在解决方案中添加一个AJAX-enabled  WEB应用程序,添加项目引用。或新建一个AJAX-enabled   WEB应用程序,将上一个项目生成的动态连接库复制到新项目的BIN下,然后添加引用。

         二、将如下代码覆盖Default.aspx:

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register TagPrefix="Samples" Namespace="LocalizingScriptResources" Assembly="LocalizingScriptResources" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>Client Localization Example</title>

</head>

<body>

    <form id="form1" runat="server">

        <asp:ScriptManager ID="ScriptManager1" EnableScriptLocalization="true" runat="server">

        <Scripts>

            <asp:ScriptReference Assembly="LocalizingScriptResources" Name="LocalizingScriptResources.CheckAnswer.js" />

        </Scripts>

        </asp:ScriptManager>

        <asp:DropDownList runat="server" AutoPostBack="true" ID="selectLanguage" OnSelectedIndexChanged="selectLanguage_SelectedIndexChanged">

            <asp:ListItem Text="English" Value="en"></asp:ListItem>

            <asp:ListItem Text="Italian" Value="it"></asp:ListItem>

            <asp:ListItem Text="中文中国" Value="zh-CN"></asp:ListItem>

        </asp:DropDownList>&nbsp;<br /><br />

        <div>

        <Samples:ClientVerification ID="xx" runat="server" ></Samples:ClientVerification>

        </div>

    </form>

</body>

</html>

 

 

后台代码如下:

using System;

using System.Data;

using System.Configuration;

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;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (IsPostBack)

        {

            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture(selectLanguage.SelectedValue);

        }

        else

        {

            selectLanguage.Items.FindByValue(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString()).Selected = true;

        }

    }

 

    protected void selectLanguage_SelectedIndexChanged(object sender, EventArgs e)

    {

        System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture(selectLanguage.SelectedValue);

    }

}

 

编译项目,看下运行结果。

你可能感兴趣的:(Ajax)