如何使标签可点击并调用c#方法

时间:2015-05-13 08:56:19

标签: c# asp.net label

是否可以将onClick事件添加到asp:标签并让它调用c#方法而不是js? 类似的东西:

<asp:Label
            ID="lblTest"
            runat="server"
            Text=""
            ToolTip="Amount of errors this person is processing"
            Style="cursor: help;"
            OnClick="lbl_Click"
            />

在服务器端:

protected void lbl_Click(object sender, EventArgs e)
{
lblTest.Text = "Clicked"
}

1 个答案:

答案 0 :(得分:2)

您可以使用CSS创建链接按钮并使其看起来像标签:

<asp:LinkButton ID="LinkButton1" 
    runat="server" 
    CssClass="myclass" 
    OnClick="LinkButton1_Click">
MyButton
</asp:LinkButton>

和CSS

a.myclass{ color: #000000; text-decoration: none; }
a.myclass:hover { text-decoration: none; }

然后将其称为

public void LinkButton1_Click()
{
    lblTest.Text = "Clicked"
}