从 <a href 运行代码隐藏例程

问题描述:

我有一个看起来像这个 html 中的按钮的链接

I have a link that looks like a button from this html

<p class="link-styleContact"><a href="#"><span>Email Contact Form</span></a></p>

我可以通过将例程名称添加到 href 来在单击该文件时运行代码隐藏文件吗?如下图

can I run a code behind file when this is clicked on by adding the routine name to the href? like below

<p class="link-styleContact"><a href="ContactFormClicked" 
    runat="server"><span>Email Contact Form</span></a></p>

您可以使用 LinkBut​​ton 控件并订阅 Click 事件.

它将在浏览器上显示为一个链接,您可以在事件处理程序中使用您的代码.

It will appear as a link on the browser and you can have your code in the event handler.

aspx:

<asp:LinkButton id="myLink" Text="Hi" OnClick="LinkButton_Click" runat="server"/>

背后的代码(VB.NET):

Code behind (VB.NET):

Sub LinkButton_Click(sender As Object, e As EventArgs) 
   ' Your code here
End Sub

背后的代码(C#):

void LinkButton_Click(Object sender, EventArgs e) 
{
   // your code here
}

或者,您可以使用 HtmlAnchor 控制并设置 ServerClick 事件处理程序.这基本上是带有 runat="server" 属性的 a 元素:


Alternatively, you can use the HtmlAnchor control and set the ServerClick event handler. This is basically the a element with a runat="server" attribute:

aspx:

<a id="AnchorButton" onserverclick="HtmlAnchor_Click" runat="server">
     Click Here
</a>

背后的代码(VB.NET):

Code behind (VB.NET):

Sub HtmlAnchor_Click(sender As Object, e As EventArgs)
   ' your code here
End Sub

背后的代码(C#):

  void HtmlAnchor_Click(Object sender, EventArgs e)
  {
     // your code here
  }