简体   繁体   中英

Calling to c# function from javascript function

I have a javascript function and c# fanction. I need to call to the c# function from the javascript function, but I don't know how...

Can someone help me?

Thank you!

The javascript function-

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

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

$.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. Then create a div tag and using javascript do this:

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

All that's left to do is go to the C# part of Delete.aspx page and catch the incoming data:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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