Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Javascript Confirmation box not working from code behing

Javascript Confirmation box not working from code behing

Scheduled Pinned Locked Moved ASP.NET
javascripthelpsysadminquestion
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    meeram395
    wrote on last edited by
    #1

    I have a checkbox in my page which is autopostback true. If the user clicks on the checkbox, I want to display a confirmation text box in javascript, and if the user clicks on Ok, then only the postback should occur, otherwise not. Even though the js is working and confirmation box displays, no postback is happening. Below is my code: Javascript code:

    function ReportConfirmation() {

            if (confirm("Proceeding with Output File Generation ? ")) {
                
               return true;
            }
            else {
                     
                return false;
            }
        }
    

    <asp:CheckBox runat="server" ID="chkGenerateRpt" AutoPostBack="true" OnCheckedChanged="chkGenerateRpt_CheckedChanged" Text="Generate Reports"/>

    Code-Behind in PageLoad (outside the IsPostBack):

    chkGenerateRpt.Attributes.Add("OnClick", "javascript:return ReportConfirmation(); ");

    protected void chkGenerateRpt_CheckedChanged(object sender, EventArgs e)
    {
    if (chkGenerateRpt.Checked)
    {
    // do some stuff
    }
    }

    If i remove return from the pageload code above, the page will work irrespetive of whether the user clicks on cancel or Ok button in confirmation box? Could anybody please help in finding out the error? Thanks in advance

    Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

    A 1 Reply Last reply
    0
    • M meeram395

      I have a checkbox in my page which is autopostback true. If the user clicks on the checkbox, I want to display a confirmation text box in javascript, and if the user clicks on Ok, then only the postback should occur, otherwise not. Even though the js is working and confirmation box displays, no postback is happening. Below is my code: Javascript code:

      function ReportConfirmation() {

              if (confirm("Proceeding with Output File Generation ? ")) {
                  
                 return true;
              }
              else {
                       
                  return false;
              }
          }
      

      <asp:CheckBox runat="server" ID="chkGenerateRpt" AutoPostBack="true" OnCheckedChanged="chkGenerateRpt_CheckedChanged" Text="Generate Reports"/>

      Code-Behind in PageLoad (outside the IsPostBack):

      chkGenerateRpt.Attributes.Add("OnClick", "javascript:return ReportConfirmation(); ");

      protected void chkGenerateRpt_CheckedChanged(object sender, EventArgs e)
      {
      if (chkGenerateRpt.Checked)
      {
      // do some stuff
      }
      }

      If i remove return from the pageload code above, the page will work irrespetive of whether the user clicks on cancel or Ok button in confirmation box? Could anybody please help in finding out the error? Thanks in advance

      Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

      A Offline
      A Offline
      Ankur m
      wrote on last edited by
      #2

      I checked the page source and found that: if you enable AutoPostBack for a check box, an onclick handler is automatically attached to the page. Now when you add another onclick event to the page both of them are concatenated and looks like this -

      <input id="chkGenerateRpt" type="checkbox" name="chkGenerateRpt" onclick="javascript:return ReportConfirmation(); ;setTimeout('__doPostBack(\'chkGenerateRpt\',\'\')', 0)" /><label for="chkGenerateRpt">Generate Reports</label>

      See the bold part. Thus it is not working. Now how we can overcome this problem: This thread describes the possible solutions - http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net-web-controls/4449/Checkbox-AutoPostBack-JavaScript[^] I will paste the solution here as well - 1. Manualy create a new checkBox class which derived from the asp.net's CheckBox control and override the render method so as to insert our own script before the build in "AutoPostBack" script (__doPostBack) 2. Use the InputCheckBox control in the System.Web.UI.HtmlControls namespace and we can add a serverchange event for it and also handler its clientside "onclick" script event. But we need to manually post back the page when user click it at clientside, for example:

      <input type="checkbox" id="chkClient" runat="server" value="..."
      name="chkClient" onclick="alert('hello');document.forms[0].submit();"
      onserverchange="chkServer_CheckedChanged">

      in page's codebehind we have :

      protected void chkServer_CheckedChanged(object sender, System.EventArgs e)
      {
      Response.Write("<br>CheckChanged at: " + DateTime.UtcNow.ToString());
      }

      I hope this clears your doubt and solves your issue! :thumbsup: Cheers! Ankur

      ..Go Green..

      M 1 Reply Last reply
      0
      • A Ankur m

        I checked the page source and found that: if you enable AutoPostBack for a check box, an onclick handler is automatically attached to the page. Now when you add another onclick event to the page both of them are concatenated and looks like this -

        <input id="chkGenerateRpt" type="checkbox" name="chkGenerateRpt" onclick="javascript:return ReportConfirmation(); ;setTimeout('__doPostBack(\'chkGenerateRpt\',\'\')', 0)" /><label for="chkGenerateRpt">Generate Reports</label>

        See the bold part. Thus it is not working. Now how we can overcome this problem: This thread describes the possible solutions - http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net-web-controls/4449/Checkbox-AutoPostBack-JavaScript[^] I will paste the solution here as well - 1. Manualy create a new checkBox class which derived from the asp.net's CheckBox control and override the render method so as to insert our own script before the build in "AutoPostBack" script (__doPostBack) 2. Use the InputCheckBox control in the System.Web.UI.HtmlControls namespace and we can add a serverchange event for it and also handler its clientside "onclick" script event. But we need to manually post back the page when user click it at clientside, for example:

        <input type="checkbox" id="chkClient" runat="server" value="..."
        name="chkClient" onclick="alert('hello');document.forms[0].submit();"
        onserverchange="chkServer_CheckedChanged">

        in page's codebehind we have :

        protected void chkServer_CheckedChanged(object sender, System.EventArgs e)
        {
        Response.Write("<br>CheckChanged at: " + DateTime.UtcNow.ToString());
        }

        I hope this clears your doubt and solves your issue! :thumbsup: Cheers! Ankur

        ..Go Green..

        M Offline
        M Offline
        meeram395
        wrote on last edited by
        #3

        Thank you Ankur. I think the second one is the best option. Thanks a lot !!! :rose:

        Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

        A 1 Reply Last reply
        0
        • M meeram395

          Thank you Ankur. I think the second one is the best option. Thanks a lot !!! :rose:

          Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

          A Offline
          A Offline
          Ankur m
          wrote on last edited by
          #4

          I am glad that it helped you. Cheers! Ankur

          ..Go Green..

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups