如何为15个linkbutton单击事件实现相同的代码 [英] How to implement same code for 15 linkbutton click events

查看:69
本文介绍了如何为15个linkbutton单击事件实现相同的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想以水平方式将一棵树绑定到2个级别
为此,我使用了15个静态链接按钮
当我单击任何链接按钮时,我需要显示该特定用户的树.


我想为这15个不同的linkbutton单击事件实现相同的功能.

是否可以实现而无需在

解决方案

后面的代码中编写15个click事件,并将其粘贴到用户定义的函数中并在所有linkbutton click事件中调用它

事件只是一个指向处理它的方法的委托(它可能有更多的作用,但是这一部分很重要).这样就可以使多个事件指向(或监听)同一个方法.
以VB中的以下代码为例:

  AddHandler  LinkLabel1.Click, AddressOf  LinkLabel_Click
 AddHandler  LinkLabel2.Click, AddressOf  LinkLabel_Click
 AddHandler  LinkLabel3.Click, AddressOf  LinkLabel_Click

私有  LinkLabel_Click( ByVal 发​​件人目标 对象 ByVal  e ' 您的代码在此处,通过投射发件人来获得被单击的LinkLabel.
结束  


这类似于C#中的以下代码

 LinkLabel1.Click + = LinkLabel_Click;
LinkLabel2.Click + = LinkLabel_Click;
LinkLabel3.Click + = LinkLabel_Click;

私有 无效 LinkLabel_Click(对象发​​件人,EventArgs e){
   // 您的代码在此处,通过投射发件人来获得被单击的LinkLabel.
} 


另外,在VB中,您可以使用Handles关键字,如下所示:

 私有  LinkLabel_Click( ByVal 发​​件人 As  对象 ByVal  e  As  EventArgs)句柄 LinkLabel1.Click,LinkLabel2.Click,LinkLabel3.Click
   ' 您的代码在此处,通过投射发件人来获得被单击的LinkLabel.
结束  


我从未使用过ASP.NET,但我可以肯定的是,它的工作方式相同:)


Hi,

I want to bind a tree in horizontal manner upto 2 levels
for that i am using 15 static link buttons
when i click on any link button then i need to display tree of that particular user.


I want to implement same functionality for those 15 different linkbutton click events.

Is it possible to implement it without writing 15 click events in code behind

解决方案

paste ur code inside a user defined function and call it in all linkbutton click event


An Event is simply a delegate pointing to a Method that handles it (there might be a bit more to it, but this part is important). This makes it possible to have multiple Events point (or listen) to the same Method.
Take for instance the following code in VB:

AddHandler LinkLabel1.Click, AddressOf LinkLabel_Click
AddHandler LinkLabel2.Click, AddressOf LinkLabel_Click
AddHandler LinkLabel3.Click, AddressOf LinkLabel_Click

Private Sub LinkLabel_Click(ByVal sender As Object, ByVal e As EventArgs)
   ' Your code here, get the LinkLabel that was clicked by casting the sender.
End Sub


This is similiar to the following code in C#

LinkLabel1.Click += LinkLabel_Click;
LinkLabel2.Click += LinkLabel_Click;
LinkLabel3.Click += LinkLabel_Click;

private void LinkLabel_Click(Object sender, EventArgs e){
   // Your code here, get the LinkLabel that was clicked by casting the sender.
}


Alternatively, in VB, you could use the Handles keyword like follows:

Private Sub LinkLabel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles LinkLabel1.Click, LinkLabel2.Click, LinkLabel3.Click
   ' Your code here, get the LinkLabel that was clicked by casting the sender.
End Sub


I have never worked with ASP.NET, but I am pretty sure this works the same way :)


这篇关于如何为15个linkbutton单击事件实现相同的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆