3

I need to use a JavaScript Confirm function in my ASPX pages to confirm various actions based on conditions in the C# code behind not directly connected to a button click event. E.g. if it is calculated that number of records > 200, ask 'Do you want to continue?' then based on Yes or No clicked perform relevant actions. I have my JavaScript defined as:

    <script type = "text/javascript">
    function Confirm(val) {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm(val)) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
    }
</script>

And call it from code behind and get the response using RegisterStartupScript and Request.Form like so:

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "confirm_value", String.Format("Confirm('{0}');", msgstr), true);
            string confirmvalue = Request.Form["confirm_value"];

The Confirm box comes up, but the confirmvalue string is always "behind". That is, if I click 'Yes' on the Confirm box, it returns 'No'; but if I stay on the same page and execute the process a second time and click 'No', I get 'Yes' returned; and so on. Question: How do I force postback of confirmvalue so I can access the response in code behind in a timely manner?

  • 1
    possible duplicate of [ASP.NET postback with JavaScript](http://stackoverflow.com/questions/1305954/asp-net-postback-with-javascript) – mason Sep 28 '15 at 17:23
  • @mason. I believe I have a different issue in that I am not using a button activate the script; but rather calling the script directly from within code behind. –  Sep 28 '15 at 18:54
  • You are *not* calling the script directly from code behind. Your code behind is registering a script, which will be embedded into the DOM that is delivered to the client. The answer on that question has a nice section called "What if I don't want to run a click handler, but want to do something else instead?" Read the entire answer, it's very good. You could always embed a dummy button on your page if you needed to. – mason Sep 28 '15 at 18:56

1 Answers1

0

There can possibly be four ways by which you can achieve this with/without post back:-

  1. Using AjaxMethod Attribute on a method in a code-behind file as below:-

    [AjaxMethod(HttpSessionStateRequirement.ReadWrite)] public object DoSomething(int countValue) { //Do Something }

    Then from the ascx file you will be able to call the method by executing the class name dot the method name. Here, in this case the method name is DoSomething.

  2. In case you are using ScriptManager in your ascx file then you can enable page methods invocation by setting EnablePageMethods attribute of the Script Manager as true. But, the method you are invoking should be static. MSDN link

    public static object DoSomething(int countValue) { //Do Something }

    Here, you will be able to call the method directly from your JS.

  3. Using web service as follow:-

    [ScriptService] public class YourService { [WebMethod(EnableSession=true)] public object DoSomething(int countValue) { //Do Something } }

  4. Post back way:-

    if (IsPostBack) { string ControlID = string.Empty; if (!String.IsNullOrEmpty(Request.Form["__EVENTTARGET"])) { ControlID = Request.Form["__EVENTTARGET"]; Control postbackControl = Page.FindControl(ControlID); } }

Bose_geek
  • 498
  • 5
  • 18