简体   繁体   English

从javascript函数调用C#方法

[英]Call C# method from javascript function

I need to call the c# method from javascript. 我需要从javascript调用c#方法。 I need the method to be called only when the javascript button is clicked ie (btnclick()). 我只需要单击javascript按钮即(btnclick())才能调用该方法。 But Login method gets executed before the onclick of button. 但是Login方法在按钮的onclick之前执行。

aspx file aspx文件

    <script type="text/javascript">
    function  btnclick()
    {
    <%=Login() %>;     //calling the c# method
    };
    $(document).ready(function ()
    {

        $(".username").focus(function () {
            $(".user-icon").css("left", "-48px");
        });
    </script>
    <body>
    //contains other elements like username and password text boxes
    <input type="submit" id="submit" name="submit" value="Login" class="button" onclick="btnclick()"/>
    </body>

aspx.cs file aspx.cs文件

The following c# method have to be called from javascript 必须从javascript调用以下c#方法

 protected void Login()
{
//contains method for authentication
}

Is there any other way to call c# method from javascript. 还有其他方法可以从javascript调用c#方法。

Your question exposes your lack of knowledge about how websites work. 您的问题暴露出您对网站工作原理的知识不足。 You simply cannot execute C# function form javascript code like: 您根本无法执行C#函数形式的javascript代码,例如:

function  btnclick()
{
<%=Login() %>;     //calling the c# method
};

because javascript is running in the browser and the C# on the server. 因为javascript在浏览器中运行,而C#在服务器上运行。 The reason that your C# Login method was called is that your button is configured to submit the form and the form I suppose is pointing to your server. 调用C#登录方法的原因是您的按钮已配置为提交表单,而我想该表单指向您的服务器。

Have you tried adding an api controller and putting the method in that? 您是否尝试过添加api控制器并将方法放入其中? I really like using ProxyApi (it's as simple as adding a NuGet) and it generates clean javascript proxies for you. 我真的很喜欢使用ProxyApi(就像添加NuGet一样简单),它会为您生成干净的javascript代理。 You'll get it up and running in just a few minutes: 您将在几分钟之内启动并运行它:

  • Add an ApiController and add the Login method. 添加一个ApiController并添加Login方法。
  • Add ProxyApi through NuGet and reference it in javascript: [script src="~/api/proxies"][/script] 通过NuGet添加ProxyApi并在javascript中引用它:[script src =“〜/ api / proxies”] [/ script]
  • Call your C# method in javascript like this: 像这样在javascript中调用您的C#方法:

    $.proxies.myapi.login(some params).done( function(data) { // done! }); $ .proxies.myapi.login(一些参数).done(函数(数据){//完成!});

see: https://github.com/stevegreatrex/ProxyApi 参见: https : //github.com/stevegreatrex/ProxyApi

You can define c# method as webmethod. 您可以将c#方法定义为webmethod。

Try It: 试试吧:

[WebMethod]
protected void Login()
{
   //contains method for authentication
}

Thanks. 谢谢。

<script language="javascript" type="text/javascript">
function jsFunction()
{
    document.getElementById('<%=lbSubmit.ClientID%>').click();
}
</script>





<asp:LinkButton  ID="lbSubmit" runat="server" Text="Update Files" 
onclick="cshapbottun_Click" ></asp:LinkButton>



C# code
protected void cshapbottun_Click(object sender, EventArgs e)
{
//    DoSomething;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM