0

I'm trying to figure out how I would call a C# function from a javascript confirm box. i.e. 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.

Patrick Keane
  • 663
  • 3
  • 19
  • 41
  • 1
    Send a JSON request to the server to invoke the function. There are several things wrong with your code. First of all, you're writing javascript as a string which will become increasingly harder for you to maintain. Second, redirection and clearing the form both can be performed via javascript on the client side. – Benjamin Gruenbaum Jan 31 '13 at 20:43
  • 1
    use ajax and web service – Dan Hunex Jan 31 '13 at 20:47
  • 1
    For the redirect and the form clear, you do not need to call the code behind, do it with simple javascript. – Aristos Jan 31 '13 at 20:48
  • I should of also noted that when the user clicks a button on the page, an SqlConnection is made and details inserted into a table, then after the `connection.Close();` comes the code above. I wasn't sure how else to invoke the JavaScript from the C#. – Patrick Keane Jan 31 '13 at 20:49
  • For more of an explanation you might review [this](http://stackoverflow.com/questions/11946530/creating-an-xml-file-using-javascript/11946959#11946959) –  Jan 31 '13 at 20:50

3 Answers3

3

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

Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
1

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

ucefkh
  • 2,509
  • 2
  • 22
  • 18
0

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.

Virus
  • 61
  • 1
  • 7