Javascript conditionally stopping _doPostBack

Last post 04-26-2007 4:09 AM by chamelion. 4 replies.

Sort Posts:

  • Hmm [^o)] Javascript conditionally stopping _doPostBack

    04-26-2007, 1:35 AM
    • Member
      4 point Member
    • chamelion
    • Member since 01-08-2007, 8:04 PM
    • Smart Software
    • Posts 15

    This may seem like a silly question but I have not been working with ASP.NET for long and, but after reading several resources, I still cannot work this out.

    I have a standard HTML Input button being run as a server control:

    <input id="Button1" type="button" value="button" runat="server" onclick="if (!confirm('Do you wish to continue?')){return false;};" onserverclick="Button1_ServerClick" />

    When the user clicks the button, using a Javascript confirm box it should ask them to confirm whether they wish to continue or not. This is fine, it is working. If the user Clicks Ok, then the onserverclick event handler is run, however if the user clicks Cancel, it should return false and the page should not PostBack and onserverclick should not run.

    The problem is however that no matter which option the user clicks, the onserverclick event handler is still running also.

    Can anyone shed some light onto this for me as I have a lot of validations that I need to perform at the client side before communicating with the server?

    Cheers

  • Re: Javascript conditionally stopping _doPostBack

    04-26-2007, 2:41 AM
    Answer
    • All-Star
      37,391 point All-Star
    • Haissam
    • Member since 10-05-2006, 6:25 AM
    • Beirut - Lebanon
    • Posts 5,632

    Try the below code

    Javascript:

      <script language="javascript">
      function Confirmation()
      {
        var con = confirm('are u sure you want to exit');
        if(con ==1)
        {
         return true;
        }
        else
        return false
      }
      </script>

    Page_Load


     Button1.Attributes.Add("onclick","return Confirmation();"); 

    HC

    Haissam Abdul Malak
    MCAD.NET
    | Blog |
  • Hmm [^o)] Re: Javascript conditionally stopping _doPostBack

    04-26-2007, 2:59 AM
    • Member
      4 point Member
    • chamelion
    • Member since 01-08-2007, 8:04 PM
    • Smart Software
    • Posts 15

    Thanks Haissam,

    Thats taken the problem to the other end of the scale now. The required serverside event no long fires if the user clicks Cancel, however it also no longer fires if the user clicks Ok.

    My current code

    In the header

    <script language="javascript">

    function Confirmation()
    {
    var con = confirm('are u sure you want to exit');
    if(con == 1)
    {
    return true;
    }
    else
    return false
    }

    </script>

    Page_Load

    Button1.Attributes.Add("onclick", "return Confirmation();"); 

    HTML 

    <input id="Button1" type="button" value="button" runat="server" onserverclick="Button1_ServerClick" />  
     Any other ideas?

    Cheers

  • Re: Javascript conditionally stopping _doPostBack

    04-26-2007, 3:37 AM
    Answer
    • All-Star
      37,391 point All-Star
    • Haissam
    • Member since 10-05-2006, 6:25 AM
    • Beirut - Lebanon
    • Posts 5,632

    It's working on my side. try to use Button Webserver control instead of the HTML

       <asp:button id="Button1" style="Z-INDEX: 108; LEFT: 320px; POSITION: absolute; TOP: 192px" runat="server"
        Text="Button"></asp:button> 

    And add the Button1_Click event to just test it.

      private void Button1_Click(object sender, System.EventArgs e)
      {
       Response.Write("Hello World");

      }

    HC

     

     

    Haissam Abdul Malak
    MCAD.NET
    | Blog |
  • Re: Javascript conditionally stopping _doPostBack

    04-26-2007, 4:09 AM
    • Member
      4 point Member
    • chamelion
    • Member since 01-08-2007, 8:04 PM
    • Smart Software
    • Posts 15

    That has it working now.

    Thanks for your help :)

    Cheers

Page 1 of 1 (5 items)