user5264652
user5264652

Reputation:

how to call a c# function in anchor tag click event

How to call server side function using HTML anchor tag.

Here I showed some example of my code.

HTML

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
         <title>Update</title> 
    </head> 
<body> 
<form id="form1" runat="server"> 
    <div> 
        <a href="" onclick="btnSubmit_Click()">Update</a> 
    </div>  
</form> 
</body> 
</html>

c#

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
   //Update User Details
} 

Upvotes: 2

Views: 9443

Answers (2)

Salah Akbari
Salah Akbari

Reputation: 39976

You can either add runat="server" and OnServerClick="btnSubmit_Click" to your anchor tag like this:

<a href="" runat="server" OnServerClick="btnSubmit_Click"> Update </a>

Or you can use asp:LinkButton. This has an OnClick attribute that will call the method in your code behind:

<asp:LinkButton ID="LinkButton1" runat="server" 
      OnClick="LinkButton1_OnClick">Update</asp:LinkButton>

protected void LinkButton1_OnClick(object sender, EventArgs e)
{
    //Update User Details
}

Upvotes: 1

Karthikeyan P
Karthikeyan P

Reputation: 1267

Try like this, create one submit button and hide that

HTML

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Update</title> 
    <script language="javascript" type="text/javascript"> 
        function fireServerButtonEvent(){ 
            document.getElementById("btnSubmit").click(); 
        } 
    </script> 
</head> 
<body> 
<form id="form1" runat="server"> 
    <div> 
        <asp:button id="btnSubmit" runat="server" text="Submit" xmlns:asp="#unknown">
            onclick="btnSubmit_Click" style="display:none" /></asp:button>
        <a href="" onclick="fireServerButtonEvent()">Update</a> 
    </div> 
</form> 
</body> 
</html>

Upvotes: 4

Related Questions