答案: 第三种:前面两种都是<input type="button"的html控件,那么如果是服务器按钮呢?当然也可以,在后台添加服务器按钮的onclick 属性。
前台third.ASPx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="third.aspx.cs" Inherits="third" %>
<html>
<head>
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="Select1">
<option selected="selected" value=1>老鼠徒弟</option>
<option value=2>吴旗娃师傅</option>
</select>
<asp:Button ID="Button1" runat="server" Text="这是个服务器按钮" /></div>
<div id="div1" />
<script type="text/javascript">
function Re(ret)
{
document.getElementById("div1").innerHTML = ret;
alert(ret);
}
</script>
</form>
</body>
</html>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 third : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
//第四个参数为null,因为你不可能再在js中给他传参数了
string str = Page.ClientScript.GetCallbackEventReference(this,"document.getElementById('Select1')._
options[document.getElementById('Select1').selectedIndex].text","Re",null);
//return false是为了防止提交窗体
Button1.Attributes.Add("onclick",str+";return false;");
}
#region ICallbackEventHandler Members
public string RaiseCallbackEvent(string eventArgument)
{
if (eventArgument == "老鼠徒弟")
{
return "老鼠徒弟:人生如鼠,不在仓就在厕!";
}
else
{
return "吴旗娃师傅:自信自强,乐观向上";
}
}
#endregion
}
小技巧,当你写完System.Web.UI.ICallbackEventHandler后,把鼠标移上去,那么System前面会有个小图表,点他会自动写好那个RaiseCallbackEvent代码,效果如下;
上一个:ASP.NET 2.0中使用webpart系列控件(1)
下一个:ASP.NET2.0实现无刷新客户端回调(2)