GOOGLE API - 翻译, 后台c# 调用前台js代码, asp.net后台调用js代码


 

 

 

前段:

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

<!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>
  <title>simple translator</title>
  <script type="text/javascript" src="https://www.google.com/jsapi?key=AIzaSyBn_f4BjfZU1AoJxqe5Bs-Bd_j-HMweksA"></script>   
</head>
<script type="text/javascript">     
    google.load("language", "1");
    function translate_for_me(  ){
        var text = document.getElementById("text_box").value;
            google.language.translate(text, 'ch', 'en', callback_function);
    }
    function callback_function(result) {
            var translated = document.getElementById("translation");
            if (result.translation) {
                translated.innerHTML = result.translation;
            } else {
                translated.innerHTML = "无翻译结果";
            }
   }
</script>
<body>
    <form id="form1" runat="server">
        <input type="text" id='text_box' runat="server"/>
        <input type='button' onclick="translate_for_me()" value='translate_js'/>
        <asp:Button ID="Button3" runat="server" Text="translate_c#" onclick="Button3_Click" />
        </br>
        <div id="translation"></div>
    </form>
</body>
</html>

 

后台:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
          
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(Page, this.GetType(), "myscript", "translate_for_me('ce');", true);
    }
}

posted on 2011-05-19 16:57  amojry  阅读(685)  评论(0编辑  收藏  举报