前段:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ">
<html xmlns=">
<head> <title>simple translator</title> <script type="text/javascript" src="></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' οnclick="translate_for_me()" value='translate_js'/> <asp:Button ID="Button3" runat="server" Text="translate_c#" οnclick="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); }}