insert data into database using javascript in asp.net C# After inserting the data into database,i want to create the alert box
insert data into database using javascript in asp.net C# After inserting the data into database,i want to create the alert box
2
Answers
Try using RegisterStartupScript instead. Also, side note, you have a typo in your alert message box. exits != exists 🙂
You can’t use register client script inside of a web method, since there is no round trip and page cycle occurring.
Register client script requires that the page was posted to the server, then code behind runs, and then the WHOLE web page travels back to the client, is re-loaded, then displayed, and THEN your injected script runs.
however, since this is a web method, then that means client side js code is calling and running the web method, and thus that means the client side script can launch/display/have/enjoy code to launch/display any dialog box or whatever you want to display in that client side js code. So, have the web method return some "yes text" or whatever you want to display, and write that code in the client side js code to then display the correct message or dialog based on what the web method returns.
So, to "inject" or "register" some js code into the web page? That requires that the web page has been posted to the server, and that means a post-back of the web page will have to occur.
The web method can’t update controls on the web page, nor inject js scripts, since the page is STILL sitting on the user’s desktop, and never made the trip to the server.
however, as noted, since your js code client side will be calling the webmethod, then it should be a simple matter to have such js code client side display that message based on a return value from the webmethod. In your case, you return "true" as a string if the data was to be inserted, and thus in place of a register script, return a "false" string, and have the calling js code then display some message based on that true or false returned to that calling js code.