Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a code behind routine from an <a href

Tags:

asp.net

vb.net

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>

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>
like image 307
dinotom Avatar asked Sep 25 '11 19:09

dinotom


1 Answers

You can use the LinkButton control instead and subscribe to the Click event.

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"/>

Code behind (VB.NET):

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

Code behind (C#):

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

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>

Code behind (VB.NET):

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

Code behind (C#):

  void HtmlAnchor_Click(Object sender, EventArgs e)
  {
     // your code here
  }
like image 199
Oded Avatar answered Nov 15 '22 21:11

Oded