Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.30/5 (3 votes)
See more:
ASP.NET
<a href="#"  runat="server"  önclick="<% getPKWhales();%>" style="height:7px;width:40px;border:outset">PK</a>


The function

C#
public void getPKWhalse()
    {
        SessionHandler.wMenu = SessionHandler.MenuVariable.WHALES;
        Session["who"] = "pkwhales";
        SessionHandler.RedirectPage("~/form/whales.aspx");
    }


Ccan i call the above getPKWhale() function in onclick event of a href tag,
which is not worked in my project, what else i wants to do , to call that function
Posted
Updated 22-May-20 15:53pm
v3

This event is handled on client side. "C#" means server side. So, you cannot call one method in another in principle; the whole idea means not understanding how browsers work with HTTP server, or what is a call.

However, you can send a HTTP request from JavaScript and get HTTP response using Ajax.

Please see:
http://en.wikipedia.org/wiki/Ajax_%28programming%29[^],
http://www.asp.net/ajax[^],
http://ajax.net-tutorials.com/[^].

You can use jQuery implementation (or a wrapper library) of Ajax:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com/[^],
http://api.jquery.com/category/ajax/[^].

—SA
 
Share this answer
 
Comments
Monjurul Habib 26-Feb-12 6:13am    
5!
Sergey Alexandrovich Kryukov 26-Feb-12 12:39pm    
Thank you, Monjurul.
--SA
You can not access code behind by javascript. javascript will be executed on client, not server. You can use ajax to do what you want. see link below :

Ajax


--------------------
Regards

H.Maadani
 
Share this answer
 
Here is Client Side Code To Call C# funcation using anchor tag

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function checkMe() {
            alert("hello");
            PageMethods.MyCSharpMethod("cSharp", onComplete);
        }
        function onComplete(result, response, content) {
            alert(result);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <div>
        <a href="javascript:;" onclick="checkMe();" runat="server">PK</a>        
    </div>
    </form>
</body>
</html>



C# Function
C#
[System.Web.Services.WebMethod]
   public static string MyCSharpMethod(string cSharpeParam)
   {
       return cSharpeParam;
   }
 
Share this answer
 
<script runat="server"> 
 
    protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
       getPKWhalse();

    } 
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Click Server Button Evnet By JavaScript</title> 
    <script language="javascript" type="text/javascript"> 
        function fireServerButtonEvent(){ 
            document.getElementById("btnSubmit").click(); 
        } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <asp:label id="lblMessage" runat="server" text="" xmlns:asp="#unknown"></asp:label>     
     
    <asp:button id="btnSubmit" runat="server" text="Submit" xmlns:asp="#unknown">
            onclick="btnSubmit_Click" style="display:none" /> 
            <a href="http://www.asp.net" onclick="fireServerButtonEvent()">Go!</a> 
    </asp:button></div> 
     
    </form> 
</body> 
</html>
 
Share this answer
 
XML
the asp page:

<a href=""#"" runat=""server"" onserverclick=""getPKWhales"" style=""height:7px;width:40px;border:outset"&gt;PK&lt;/a&gt;</pre><br" mode="hold" />
the code behind:

public void getPKWhalse(object sender, EventArgs e)
    {
        SessionHandler.wMenu = SessionHandler.MenuVariable.WHALES;
        Session["who"] = "pkwhales";
        SessionHandler.RedirectPage("~/form/whales.aspx");
    }
 
Share this answer
 
Comments
CHill60 6-Jun-13 9:34am    
This post was resolved over a year ago!

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