0

I have the below function in asp.net

function Confirm() {

        var confirm_value = document.createElement("INPUT");

        confirm_value.type = "hidden";

        confirm_value.name = "confirm_value";

        if (confirm("Do you want to save data?")) {

            confirm_value.value = "Yes";

        } else {

            confirm_value.value = "No";

        }

        document.forms[0].appendChild(confirm_value);

    }

Now, I need to call this function confirm in the code file. Note: I have the design in page: *.aspx while I have the code in aspx.cs. How can I call it in aspx.cs while I write this function in aspx file.

Turnip
  • 35,836
  • 15
  • 89
  • 111
user2103335
  • 63
  • 4
  • 13
  • When you want to call this function e.g. on some button click or on form load? – Adil Feb 19 '15 at 12:49
  • `Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", "")` – codingbiz Feb 19 '15 at 12:52
  • possible duplicate of [How to call javascript function from code-behind](http://stackoverflow.com/questions/4848678/how-to-call-javascript-function-from-code-behind) – Izzy Feb 19 '15 at 12:58

3 Answers3

0

From code behindC# you can register (java)script block to run on page like following:

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","Confirm()",true);

or with JQuery after pageload:

$( document ).ready(function() {  
    Confirm();
});
Edi G.
  • 2,432
  • 7
  • 24
  • 33
0

You can use like this.

string myScript = "<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
myScript += "$('#DvEmptyPush').show()";
myScript += "</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", myScript, false);
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
0

You made a big confusion here. I suggest you to try to understand the client-side programing and server-side programing concepts. Here are some videos (there are with php, but is the same with asp.net) that will help you understand why what you ask here has no sense:

http://www.killerphp.com/videos/serverside_programming_languages/serverside_programming_languages.html

https://www.youtube.com/watch?v=DcBB2Fp8WNI

https://teamtreehouse.com/library/build-a-simple-php-application/getting-started-with-php/serverside-versus-clientside-2

The javascript code is execuded on the client(browser). The aspx.cs/c# code is executed on the server. You use .aspx and .aspx.cs files on the server to write dynamically html, javascript, etc... on the Response. The Response that can contains html, javascript and other stuff gets to the client(browser) where is interpreted(html) or executed(javascript).

That javascript function can be called only on the client, not on the server - on the server you can only write javascript.

Boies Ioan
  • 892
  • 8
  • 12