有没有一种方法来添加一个onclick事件到ASP.NET标签服务器控件? [英] Is there a way to add an onclick event to an ASP.NET Label server control?
问题描述
我想要做这样的事情:
<asp:Label ID="lblMyLabel" onclick="lblMyLabel_Click" runat="server">My Label</asp:Label>
我知道在Javascript中我可以这样做:
I know that in Javascript I can do:
<span onclick="foo();">My Label</span>
所以我不知道为什么我不能这样做与标签对象。
So I'm wondering why I can't do that with a Label object.
推荐答案
您可以使用属性添加的onclick客户端回调。
You can use Attributes to add onclick client side callback.
我不知道,你可以在span标签做到这一点,但如果它的工作原理,您可以添加的onClick由 lblMyLabel.Attributes.Add(onclick事件,富();) ;
I didn't know you can do this on span tags, but if it works you can add 'onclick' by lblMyLabel.Attributes.Add("onclick", "foo();");
但富();
将需要一个客户端的JavaScript函数
But foo();
would need to be a client side javascript function.
System.Web.UI.WebControls.Label
不产生OnClick服务器事件。你可以看看,如果你想用上面的例子服务器回调使用AJAX。
System.Web.UI.WebControls.Label
does NOT have OnClick server event. You could look into using AJAX if you want server callback with example above.
您也可以使用的LinkButton
像其他的发言权。你可以把它看起来不像一个链接使用CSS,如果它只是下划线你担心。
You could also use LinkButton
like other say. You can make it not look like a link by using CSS, if it is just that underline you are concerned about.
ASPX:
<asp:LinkButton ID="LinkButton1" runat="server"
CssClass="imjusttext" OnClick="LinkButton1_Click">
LinkButton
</asp:LinkButton>
CSS:
a.imjusttext{ color: #000000; text-decoration: none; }
a.imjusttext:hover { text-decoration: none; }
这篇关于有没有一种方法来添加一个onclick事件到ASP.NET标签服务器控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!