简体   繁体   English

从javascript函数调用c#函数

[英]Calling to c# function from javascript function

I have a javascript function and c# fanction.我有一个 javascript 函数和 c# fanction。 I need to call to the c# function from the javascript function, but I don't know how...我需要从 javascript 函数调用 c# 函数,但我不知道如何...

Can someone help me?有人能帮我吗?

Thank you!谢谢!

The javascript function- javascript函数-

<script type="text/javascript" language="javascript">

        function DeleteBook(idimg) {
// idimg is a string
            var userConfirm = window.confirm('Are you sure?');

            if (userConfirm == true) {
                control.Sess(idimg);// The line which is colling to the c# function - doesn't work
                window.open('Delete.aspx');
            }
            else
                return false;
        }
    </script>

The c# function- C# 函数-

protected void Sess(string id)
    {
        Session["forDelete"] = id;
    }

You can create a web method您可以创建一个网络方法

[WebMethod(EnableSession = true)]
public static Application GetApplication(int id)
{
}

and in javascript you then do something like this然后在 javascript 中你做这样的事情

$.ajax(
        {
            type: "POST",
            url: "Applications.aspx/GetApplication",
            contentType: "application/json; charset=utf-8",
            data: "{'id':" + id + "}",
            dataType: "json",
            success: methodToDoSomethingOnSuccess,
            error: function (rhq, textStatus, errorThrown) {
                alert ("some went awry");

            }
        });

you have to create an input of type submit that invokes your C# function using the HTML and make it hidden.您必须创建一个 submit 类型的输入,该输入使用 HTML 调用您的 C# 函数并将其隐藏。 Then create a div tag and using javascript do this:然后创建一个 div 标签并使用 javascript 执行以下操作:

@CSS
.Hidden {
     display:none;
}

@HTML     
<input type="submit" id="SubmitTag" OnClick="C# Functin" class="Hidden" runat="server" />
//if using MVC and Razor 
@using (Html.BeginForm("Action Name", "Controller Name", FormMethod.Post)) {
    <input type="submit" id="SubmitTag" class="Hidden" />
} 

<div id="OnDivClick"> what you want to do in here </div>

@JS
$('#OnDivClick').click(function () {
    $('#SubmitTag').trigger("click");
}); 

Well, there are ways to do this but I believe that you're trying to save something in the Session for the Delete.aspx page to read it.嗯,有很多方法可以做到这一点,但我相信您正在尝试在Session保存一些内容,以便Delete.aspx页面阅读它。 The simplest solution is just post the data in:最简单的解决方案是将数据发布到:

var form = document.createElement("form");
form.setAttribute('method', 'post');
form.setAttribute('action', 'Delete.aspx');
form.setAttribute('target', '_blank');
form.innerHTML = '<input type="hidden" name="forDelete" value="' + idimg + '" />';
document.body.appendChild(form);
form.submit();

This dynamically creates a form and submits it with idimg which will open the page Delete.aspx in a new window.这会动态创建一个表单并使用idimg提交它,这将在新窗口中打开页面Delete.aspx

All that's left to do is go to the C# part of Delete.aspx page and catch the incoming data:剩下要做的就是转到Delete.aspx页面的C#部分并捕获传入的数据:

string idimg = Request.Form["forDelete"];
// Do whatever with it
Session["forDelete"] = idimg; // If you still want to save it in Session

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

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