Running code behind a routine from <a href
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> Is it possible to run the code behind the file when they click on it by adding the name of the subroutine in href? as below
<p class="link-styleContact"><a href="ContactFormClicked" runat="server"><span>Email Contact Form</span></a></p> Instead, you can use LinkButton and sign up for the Click event.
It will appear as a link in 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 for (C #):
void LinkButton_Click(Object sender, EventArgs e) { // your code here } Alternatively, you can use HtmlAnchor and install a ServerClick event ServerClick . This is basically a element with the 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 for (C #):
void HtmlAnchor_Click(Object sender, EventArgs e) { // your code here } If you want to use the <a> tag specifically, then some parameters:
You can use <a href="http://example.com" onclick="return foo()"> , where foo() is a javascript function.
You can also use the onload event to process the page, for example:
<a href="http://example.com?e=foo"> and then in the pageload () event do the following: ...if request.querystring("e") = "foo" then...
Otherwise, as others have suggested, managing <asp:linkbutton> is a good choice.