如何事件处理程序添加到动态控制在asp.net中做回发?除了使用Javascript,这可能吗? [英] How to add event handlers to dynamic control in asp.net to do postback? Other than using Javascript, is it possible?
本文介绍了如何事件处理程序添加到动态控制在asp.net中做回发?除了使用Javascript,这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何添加事件处理程序来动态控制(按钮)在asp.net中做回发?除了使用Javascript,这可能吗?
How to add event handlers to dynamic control( a button) in asp.net to do postback? Other than using Javascript, is it possible?
推荐答案
是的,这是可能的。
因此,举例来说,在你的的Page_Load
你可以创建按钮。
So, for example, in your Page_Load
you can create the button.
此示例是使用VB
这对回传重新创建,所以不要把它包在如果(没有的IsPostBack)
- 否则将无法正常工作
This has to be recreated on postback, so DO NOT wrap it in an If (Not isPostBack)
- otherwise it will not work
Dim btn As Button = New Button() With {.Text = "Click Me", .ID = "MyId"}
AddHandler btn.Click, AddressOf MyBtnClick ' This is the method to call
然后处理click这里:
and then you handle the click in here:
Private Sub MyBtnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = CType(sender, Button) ' Gets the button that fired the method
' Do your code here
End Sub
这是在C#
Button btn = new Button {Text = "Click Me",ID = "MyId"};
btn.Click += new EventHanlder(MyBtnClick);
和被调用方法
private void MyBtnClick(object sender, EventArgs e)
{
Button btn = (Button)sender; // Gets the button that fired the method
// Do your code here
}
这篇关于如何事件处理程序添加到动态控制在asp.net中做回发?除了使用Javascript,这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文