如何在href onclick事件中调用c#函数 [英] how to call a c# function in a href onclick event

查看:101
本文介绍了如何在href onclick事件中调用c#函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





可以在href标签的onclick事件中调用上面的getPKWhale()函数,

在我的项目中没有工作,我想做什么,调用该函数



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

推荐答案

此事件在客户端处理。 C#表示服务器端。所以,原则上你不能在另一种方法中调用一种方法;整个想法意味着不了解浏览器如何使用HTTP服务器或什么是通话。



但是,您可以从JavaScript发送HTTP请求并使用HTTP获取HTTP响应Ajax。



请参阅:

http://en.wikipedia.org/wiki/Ajax_%28programming%29 [ ^ ],

http://www.asp .net / ajax [ ^ ],

http://ajax.net-tutorials.com/ [ ^ ]。



您可以使用jQuery实现(或者Ajax的包装库:

http://en.wikipedia.org/wiki/JQuery [ ^ ],

http://jquery.com/ [ ^ ],

http://api.jquery.com/category/ajax/ [ ^ ]。



< dd> -SA
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


您无法通过javascript访问代码。 javascript将在客户端而不是服务器上执行。你可以使用ajax做你想做的事。请参阅以下链接:



Ajax




--------------------

问候



H.Maadani
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


<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>


这篇关于如何在href onclick事件中调用c#函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆