如何对click事件使用标签控件 [英] How to use label control for click event
本文介绍了如何对click事件使用标签控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有可能在asp.net中使用标签控件进行点击事件。如果没有,任何人都可以帮助指导javascript代码。
在某些页面中,我已经使用按钮控件进行点击事件。
问候。
解决方案
您可以通过使用用户控件和委托来完成此操作:
public delegate void MyClick( object
sender,System.EventArgs e);
public event MyClick cClick;
private void UserControl1_Click(对象
sender,System.EventArgs e)
{
if (cClick!= null )
cClick(sender,e);
}
private void label1_Click( object sender,
System.EventArgs e)
{
UserControl1_Click(sender,e);
}
private void label2_Click( object sender,System.EventArgs
e)
{
UserControl1_Click(sender,e);
}
为什么要使用标签来模拟按钮的行为?最简单的方法是创建一个css类并将其应用于按钮,使其看起来像标签(没有边框和背景颜色等)。
试试这个:
< script 类型 = javascript >
window.onload = function()
{
document.getElementById( lbl)。attachEvent(' onclick',function()
{
alert( 你点击了这个标签);
});
}
< / script > 跨度>
I would like to know if there is any possibility of using a label control for click event in asp.net. If not, can anyone help guiding with javascript code.
In certain pages I have already used button control for the click event.
Regards.
解决方案
You can do this by using user control and delegate like below:
public delegate void MyClick (object sender, System.EventArgs e); public event MyClick cClick ; private void UserControl1_Click(object sender, System.EventArgs e) { if (cClick != null) cClick (sender,e); } private void label1_Click(object sender, System.EventArgs e) { UserControl1_Click( sender, e); } private void label2_Click(object sender, System.EventArgs e) { UserControl1_Click( sender, e); }
Why do you want to mock the behavior of button using label? The simplest thing would be to create a css class and apply it to the button to make it look like label(without border and background color etc.)
Try this :
<script type="javascript"> window.onload = function() { document.getElementById("lbl").attachEvent('onclick', function() { alert("You clicked this label"); }); } </script>
这篇关于如何对click事件使用标签控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文