日期:2014-05-17 浏览次数:20514 次
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ButtonPage.aspx.cs" Inherits="JavaScriptCallServerEvent.ButtonPage" %> <!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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="serverButton" runat="server" onclick="serverButton_Click" Text="服务器按扭" /> <input id="clientBtn" type="button" value="客户端按扭" onclick='javascript:<%=MyClientScript %>;' /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace JavaScriptCallServerEvent { public partial class ButtonPage : System.Web.UI.Page { public string MyClientScript { get; set; } protected void Page_Load(object sender, EventArgs e) { MyClientScript = Page.ClientScript.GetPostBackEventReference(serverButton, "MyButton"); } protected void serverButton_Click(object sender, EventArgs e) { Response.Write("我被点击了!"); } } }
<%@ Page Language="C#" AutoEventWireup="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void serverButton_Click(object sender, EventArgs e) { ScriptManager.RegisterStartupScript(this, this.GetType(), "result", "alert('hi');", true); } public string MyClientScript { get; set; } protected void Page_Load(object sender, EventArgs e) { MyClientScript = Page.ClientScript.GetPostBackEventReference(serverButton, "MyButton"); } protected override void Render(HtmlTextWriter writer) { Page.ClientScript.RegisterForEventValidation(serverButton.UniqueID, "MyButton"); base.Render(writer); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="serverButton" runat="server" onclick="serverButton_Click" Text="服务器按扭" /> <input id="clientBtn" type="button" value="客户端按扭" onclick="javascript:<%=MyClientScript %>;" /> </div> </form> </body> </html>