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. How to Stop PostBack of Asp Button

How to Stop PostBack of Asp Button

Scheduled Pinned Locked Moved ASP.NET
questionsysadmintutorial
5 Posts 3 Posters 0 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.
  • K Offline
    K Offline
    Kuricheti
    wrote on last edited by
    #1

    How do I stop the postback from a asp button? I want to do some validation on the client side when a button is pressed and decide whether to post back to the server or not. Thanks Hari

    T R 2 Replies Last reply
    0
    • K Kuricheti

      How do I stop the postback from a asp button? I want to do some validation on the client side when a button is pressed and decide whether to post back to the server or not. Thanks Hari

      T Offline
      T Offline
      TRK UK
      wrote on last edited by
      #2

      Hi Hari you can do this by adding small attribute(JavaScript function) to Button. on form load event of code behind file .Attributes.Add("OnClick", "return confirm('Continue?');"); or hope this Help Thanks..

      K 1 Reply Last reply
      0
      • K Kuricheti

        How do I stop the postback from a asp button? I want to do some validation on the client side when a button is pressed and decide whether to post back to the server or not. Thanks Hari

        R Offline
        R Offline
        RichardGrimmer
        wrote on last edited by
        #3

        Alternatively, if you're using 2.0, the buttons expose an OnClientClick attribute which fires (on the client side obviusly, so you'll need to give it a javascript function) before OnClick...to stop OnClick, return false from OnClientClick

        "Now I guess I'll sit back and watch people misinterpret what I just said......" Christian Graus At The Soapbox

        K 1 Reply Last reply
        0
        • T TRK UK

          Hi Hari you can do this by adding small attribute(JavaScript function) to Button. on form load event of code behind file .Attributes.Add("OnClick", "return confirm('Continue?');"); or hope this Help Thanks..

          K Offline
          K Offline
          Kuricheti
          wrote on last edited by
          #4

          Hi TRK@UK, Thanks for the response, this is working fine as expected. I tried something more...

          In Page Load

          Object objAjax = Request.QueryString["from"];
          if (objAjax != null)
          {
          Response.Clear();
          Response.Write("1");
          Response.End();
          }

              //Button1.Attributes.Add("OnClick", " if(TextBox1.value=='hari'){return true}else{return false};");
              Button1.Attributes.Add("OnClick", " var finalval = chkText(); if(finalval == false){return false;}"); 
          

          var XmlHttp;   
          function chkText()
          {
            try
              {
                 XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
              }
              catch(e)
              {
                 try
                 {
                    XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                 }
                 catch(e1)
                 {
                    XmlHttp = null;
                 }
              }
                  //Creating object of XMLHTTP in Mozilla and Safari
              if(!XmlHttp && typeof XMLHttpRequest != "undefined")
              {
                 XmlHttp = new XMLHttpRequest();
              }           
              if(XmlHttp)
              {
                    try
                    {
                      XmlHttp.onreadystatechange = HandleResponse;
                      XmlHttp.open("POST", "CallingServerFunctionUsingAjax.aspx?from=Ajax",true);
                      XmlHttp.send(null);                
                      if(XmlHttp.readyState==1)
                      {
                          return false;
                      }
                    }
                 catch(e2)
                 {
                    alert(e2);
                 }
              }      
          

          }

          function HandleResponse()
          {
          try
          {
          if(XmlHttp.readyState == 4)
          {
          if(XmlHttp.status == 200)
          {
          var retval=XmlHttp.responseText;
          switch(retval)
          {
          case "1":
          return false;
          break;

                            default:                                   
                                       break;
                        }
                   }
                 else
               </x-turndown>
          
          1 Reply Last reply
          0
          • R RichardGrimmer

            Alternatively, if you're using 2.0, the buttons expose an OnClientClick attribute which fires (on the client side obviusly, so you'll need to give it a javascript function) before OnClick...to stop OnClick, return false from OnClientClick

            "Now I guess I'll sit back and watch people misinterpret what I just said......" Christian Graus At The Soapbox

            K Offline
            K Offline
            Kuricheti
            wrote on last edited by
            #5

            Hi RichardGrimmer, Great.It did the trick... Thanks for the reply... Thanks Hari

            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