通过单击另一个asp:按钮触发asp:按钮单击事件

时间:2012-03-06 16:52:06

标签: asp.net .net vb.net events

我有两个asp:按钮..说按钮1和按钮2,当我点击button1时,我想做的是点击按钮2的点击事件..有没有办法通过代码后面触发asp:按钮上的点击事件?请帮忙,新手在这里。

1 个答案:

答案 0 :(得分:0)

好吧,你可以为这两个按钮添加相同的事件,如下所示:

<asp:Button ID="btn1" runat="server" Text="Button 1" CommandName="Save" />
<asp:Button ID="btn2" runat="server" Text="Button 2" CommandName="Cancel" />

并在代码后面(vb.net):

Protected Sub btn_event(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click, btn2.Click

   Dim btn As Button = CType(sender, Button)

   'now you have the button instance on sender object, and you can check the ID property or CommandName property do solve what you want to do!

End Sub

C#代码:

protected void btn_event(object sender, EventArgs e) {

    Button btn = (Button)sender;

    //now you have the button instance on sender object, and you can check the ID property or CommandName property do solve what you want to do!

}

如果你正在使用C#,请记得设置事件点击asp:button标签。