简体   繁体   中英

How I would call a C# function from javascript?

I'm trying to figure out how I would call a C# function from a javascript confirm box. ie If user selects 'OK' a function is called, and if user selects 'Cancel' another function is called. All the code is located in the back page, and looks as follows:

Response.Write(@"
    <script language='javascript'>
    var msg=confirm('Your new document has been created.\nPress OK to go there now, or Cancle to create another document.');
    if (msg==true) {<%=redirect()%>;}
    else {<%=clearForm()%>;}
    </script>
");

protected void redirect(object sender, EventArgs e)
{
    Response.Redirect("myPage.aspx");
}

protected void clearForm(object sender, EventArgs e)
{
    //More code here//
}

Note that all the code within the Response.Redirect is all on one line, I just split it up here for simplicity! Anyways, this does not work, and I cant find a solution. I've tried various different things within the if statement

My first idea was not to give the user an option, and to simply use:

Response.Write(@"<script language='javascript'>alert('Your new document has been created.');</script>");
Response.Redirect("TaskPanel.aspx");

But when I tried this, the page did not wait for the user to click OK before redirecting, and hence made it pointless.

You are getting confused between server-side and client-side code. The C# code you posed will run on your server and write out the JavaScript code on the page:

<script language='javascript'>
    var msg=confirm('Your new document has been created.\nPress OK to go there now, or Cancle to create another document.');
    if (msg==true) {<%=redirect()%>;}
    else {<%=clearForm()%>;}
</script>

The JavaScript you write out will be run on the client-side browswer.

However, I would imagine you will get a JavaScript error, since it will attempt to write out <%=redirect()%>; and <%=clearForm()%>; which is ASPX scriptlet code that should have been run on the server-side (but is actually appearing as a String in the JavaScript on the client).

This is just an explanation/answer to why you cannot call a C# function directly from JavaScript.

For your specific problem, you do not need to call back to the server to do a redirect or clear form.

Both of these can be done in JavaScript.

The redirect can be done with JavaScript window.location="myPage.aspx" or self.location="myPage.aspx"

The clear form can be done with JavaScript form.reset()

Yes its simple just : put you callback function in a file

After that you should bind click on OK you call the function with : AJAX with any javascript Framework to make simple

OK Cancel

$("#OK,#Cancel").click(function(){

 var whichFct = $(this).attr('fct');      var urlToFile = "web/app/link/fct/1";
 if(whichFct == 2)  urlToFile = "web/app/link/fct/2"

   $.post(urlToFile,function() {
            //do sthlike hiding the form ...
       });
});

Its just simple than this

pass a javascript function on buttonclick.. then write following code in .aspx page:

<script language='javascript'>
function func_name()
{
var msg=confirm('Your new document has been created.\nPress OK to go there now, or Cancle to create another document.')
if (msg==true) {<%=redirect()%>}
else {<%=clearForm()%>}
}
</script>

do similarly for other button as well.

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