事件源转换成button按钮进行相关处理.
前台代码:
代码
<div>
<asp:Button ID="btn1" runat="server" Text="张三" CommandName="1"
onclick="btn1_Click" />
<asp:Button ID="btn2" runat="server" Text="李四" CommandName="2"
onclick="btn1_Click" />
<asp:Button ID="btn3" runat="server" Text="王五" CommandName="3"
onclick="btn1_Click" />
</div>
<asp:Button ID="btn1" runat="server" Text="张三" CommandName="1"
onclick="btn1_Click" />
<asp:Button ID="btn2" runat="server" Text="李四" CommandName="2"
onclick="btn1_Click" />
<asp:Button ID="btn3" runat="server" Text="王五" CommandName="3"
onclick="btn1_Click" />
</div>
后置代码:
代码
protected void btn1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;//事件源对象转为Button
string str=btn.CommandName;
switch (str)
{
case "1":
Response.Write("张三");
break;
case "2":
Response.Write("李四");
break;
case "3":
Response.Write("王五");
break;
default:
break;
}
}
{
Button btn = (Button)sender;//事件源对象转为Button
string str=btn.CommandName;
switch (str)
{
case "1":
Response.Write("张三");
break;
case "2":
Response.Write("李四");
break;
case "3":
Response.Write("王五");
break;
default:
break;
}
}