Click here to Skip to main content
12,953,865 members (45,210 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
<a href="#"  runat="server"  önclick="<% getPKWhales();%>" style="height:7px;width:40px;border:outset">PK</a>


The function

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 22-Feb-12 22:43pm
Updated 26-Feb-12 0:13am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
Monjurul Habib 26-Feb-12 6:13am
   
5!
SAKryukov 26-Feb-12 12:39pm
   
Thank you, Monjurul.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

<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>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Here is Client Side Code To Call C# funcation using anchor tag

<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
[System.Web.Services.WebMethod]
   public static string MyCSharpMethod(string cSharpeParam)
   {
       return cSharpeParam;
   }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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");
    }
  Permalink  
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,429
CHill60 3,490
Maciej Los 3,103
ppolymorphe 2,020
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 29 Oct 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100