Click here to Skip to main content
11,707,470 members (70,761 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
<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
Edited 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 at 26-Feb-12 6:13am
   
5!
SAKryukov at 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 at 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
0 Mika Wendelius 220
1 Maciej Los 195
2 OriginalGriff 148
3 Sergey Alexandrovich Kryukov 145
4 chainerlt 110
0 OriginalGriff 9,198
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,921
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 6 Jun 2013
Copyright © CodeProject, 1999-2015
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