如何正确理解ASP.NET中的Sender?
如何正确理解ASP.NET中的Sender?
举个例子:
<input type="submit" id="a1" value="闪" OnServerClick="Submit_Click" runat="server">
<input type="submit" id="a2" value="亮" OnServerClick="Submit_Click" runat="server">
<input type="submit" id="a3" value="日" OnServerClick="Submit_Click" runat="server">
<input type="submit" id="a4" value="子" OnServerClick="Submit_Click" runat="server">
这是四个Submit 按钮,我们知道它触发的事件是同一个事件,但要怎么来区分呢?看看下面就知道了:
public void Submit_Click(Object sender, EventArgs E)
{
switch (((Control)sender).ID)
{
case "a1" :showmsg.Text=""宋颜浩; break;
case "a2" :showmsg.Text=""李雁冰; break;
case "a3" :showmsg.Text=""费文华; break;
case "a4" :showmsg.Text=""吴晓桃; break;
}
}
</script>
<asp:Label id=showmsg runat=server />