admin管理员组

文章数量:1401818

How to call server side function using HTML anchor tag.

Here I showed some example of my code.

HTML

<html xmlns=""> 
    <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
} 

How to call server side function using HTML anchor tag.

Here I showed some example of my code.

HTML

<html xmlns="http://www.w3/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
} 
Share Improve this question asked Aug 25, 2015 at 15:01 user5264652user5264652 1
  • You need to use AJAX to do this – muddyfish Commented Aug 25, 2015 at 15:06
Add a ment  | 

2 Answers 2

Reset to default 4

Try like this, create one submit button and hide that

HTML

<html xmlns="http://www.w3/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>

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
}

本文标签: javascripthow to call a c function in anchor tag click eventStack Overflow