多个控件公用一个事件处理程序
通过测试sender参数值,可以确定触发事件的具体按钮,如下例:
private void BtnClick(object sender,System.EventArgs e)
{
Button b=(Button)sender;
String buttonID=b.ID;
switch(buttonID)
{
case "btnDoThis":
//DoThis的代码
case "btnDoThat":
//DoThat的代码
}
//所有按钮的通用代码
}
TextBox控件的用法
新建一个TextBoxDemo网站的Default.aspx
<body>
<form id="form1" runat="server">
<div>
<h1>TextBox Demo</h1>
<asp:TextBox ID="txtInput" runat="server" AutoPostBack="true"(自动回发) OnTextChanged="txtInput_TextChanged">
Enter some text
</asp:TextBox>
<br/>
<asp:TextBox ID="txtEcho" runat="server" BackColor="LightGray" ReadOnly="true"/>
</div>
</form>
</body>
|||||||||||||||||||||||||||||||
TextBoxDemo的TextChanged事件处理程序
protected void txtInput_TextChanged(object sender,EventArgs e)
{
txtEcho.Text=txtInput.Text;
}
当更改控件的内容并把光标移出该文本框后,将触发txtInput_TextChanged事件