日期:2014-05-18  浏览次数:20403 次

ASP.NET2.0(C#)中如何实现多个控件事件共享,比如说多个Button控件共享一个事件!急等高手!
ASP.NET2.0(C#)中如何实现多个控件事件共享,比如说多个Button控件共享一个事件,点击任何一个,都触发该事件!

急等高手!

------解决方案--------------------
void Page_Load(Object sender, EventArgs e)
{
Button1.Click += new EventHandler(this.GreetingBtn_Click);
Button2.Click += new EventHandler(this.GreetingBtn_Click);
Button3.Click += new EventHandler(this.GreetingBtn_Click);
}

void GreetingBtn_Click(Object sender,
EventArgs e)
{
// When the button is clicked,
// change the button text, and disable it.

Button clickedButton = (Button)sender;
clickedButton.Text = "...button clicked... ";
clickedButton.Enabled = false;

// Display the greeting label text.
GreetingLabel.Visible = true;
}

------解决方案--------------------
你可以分别在按钮的单击事件中调用另外一个方法


protected void button1_Click(Object sender, EventArgs e)
{
shareFunction(parameters);
}

protected void button2_Click(Object sender, EventArgs e)
{
shareFunction(parameters);
}

protected void shareFunction(parameters)
{
//相关处理
}
------解决方案--------------------
只要选择相同的事件处理程序即可
------解决方案--------------------
呵呵,ls够明了了.
------解决方案--------------------
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "AspButton.aspx.cs " Inherits= "Default6 " %>

<!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= "Button1 " CommandName= "btn1 " OnCommand= "aaa " runat= "server " Text= "Button " />
<asp:Button ID= "Button2 " CommandName= "btn2 " OnCommand= "aaa " CommandArgument= "this is btn2 " runat= "server " Text= "Button " /> </div>
</form>
</body>
</html>

cs文件

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 Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void aaa(object sender, CommandEventArgs e)
{
if (e.CommandArgument.Equals( " ")) //if (e.CommandArgument== " ")
Response.Write( "该按钮的CommandName是 "+e.CommandName+ " 该按钮没有CommandArgument参数 ");
else
Response.Write( "该按钮的CommandName是 " + e.CommandName + " 该按钮的CommandArgument是 "+e.CommandArgument);
}
}
------解决方案--------------------
protect void OnClick(object sender,CommandEventArgs e)
{
Button btn=(Button)sender;