触发asp:按钮的点击事件,通过点击另一个asp:按钮

问题描述:

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

+2

为什么不把你的button2的逻辑放在一个单独的方法中,所以你可以从button1调用它。 – bschultz 2012-03-06 16:54:11

好了,你可以把同一个事件两个按钮,这样的事情:

<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标签上设置事件。

+0

谢谢你,先生,我现在正在工作..我第一次抱歉没有得到它。新手在这里。 – 2012-03-06 17:25:09