【问题标题】:trigger asp:button's click event by clicking another asp:button通过单击另一个 asp:button 触发 asp:button 的单击事件
【发布时间】:2012-03-06 16:52:05
【问题描述】:

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

【问题讨论】:

  • 为什么不把 button2 的逻辑放在一个单独的方法中,这样你也可以从 button1 调用它。

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


【解决方案1】:

好吧,您可以为两个按钮设置相同的事件,如下所示:

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

【讨论】:

  • 谢谢您,先生,我现在可以正常工作了.. 起初我没有得到它,抱歉。新手在这里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 2011-05-17
  • 2021-12-21
  • 2011-04-03
  • 1970-01-01
相关资源
最近更新 更多