Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can i call a function(Submit_Click) in default.aspx.cs from anchor tag of a link in default.aspx.

ASP.NET
<a href=default.aspx  runat="server"  önclick=Submit_Click() id="anchor1";
Posted
Updated 18-Mar-12 20:17pm
v2

Here is the solution

http://forums.asp.net/t/999909.aspx/1[^]


No you can't!
Workaround of this will be to pass some argument to page and after trapping that argument at server side..and then calling desired function. Here is the code snippet:

Understanding that you have Do_Insert as server side, implement your code like this:
'finishes server processing, returns to client.
VB
Dim strScript As String = "<script language='JavaScript'>"
strScript += "if(confirm('" & strMessage & "')) {TestServer()} else{return false}"
strScript += "trScript += "script>"
RegisterStartupScript("confirm", strScript)

<pre lang="Javascript">function TestServer()
{
document.Form1.action=’YourPage.aspx?action=TestServer’;
document.Form1.submit();
}


Now this way you are indicating your page that required action to perform. Now in Page_Load event:

VB
private void Page_Load(object sender, System.EventArgs e)
{
If (Request.QueryString["action"] != null)
{
If (Request.QueryString["action"] == "TestServer")
{
TestServer();
}
}

//rest of your code..
}
 
Share this answer
 
Comments
Amu@184 23-Mar-12 3:10am    
I have used QueryString. Thanks
ASP.NET
<a href="default.aspx" runat="server" id="anchor1"> Click here </a>


C#
protected void Page_Load(object sender, EventArgs e)
{
    anchor1.ServerClick +=new EventHandler(anchor1_Click);
}
protected void anchor1_Click(object sender, EventArgs e)
{
    Response.Write("<script>alert('hello')</script>");
}
 
Share this answer
 
Comments
Member 11690990 21-May-15 2:24am    
I have 14 different anchor tags in one web page, then will i have to write the same code 14 times in code behind? pls suggest...
I think If you use a LinkButton then you can achieve this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900