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 avoid postback on button click event in asp.net/javascript?

how to avoid postback on button click event in asp.net/javascript?

Scheduled Pinned Locked Moved ASP.NET
questioncsharpjavascriptasp-net
12 Posts 4 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.
  • D dreddy7

    I have a print button on my aspx page which prints only a particular

    section of the page. The javascript for this print is coded in vb.net. When I click on print and refresh the page, the print button click method is getting called once again. I do not want the button click to respond to the event on every refresh. How can I do this? Thank you

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #2

    Not sure if it explains your situaton, but the answer to the broad question is, if you handle OnClientClick on your button with javascript that returns false, your server click will not occur.

    Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

    D 1 Reply Last reply
    0
    • C Christian Graus

      Not sure if it explains your situaton, but the answer to the broad question is, if you handle OnClientClick on your button with javascript that returns false, your server click will not occur.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      D Offline
      D Offline
      dreddy7
      wrote on last edited by
      #3

      I didnot get you. Can you elaborate please? thank u

      C 1 Reply Last reply
      0
      • D dreddy7

        I didnot get you. Can you elaborate please? thank u

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #4

        You can add this code to any button OnClickClick="return false();" and the click event will never fire. Replacing this with a call to a method that works out if it should postback, and returns true or false, will allow you to control your ASP.NET button from javascript.

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        D 1 Reply Last reply
        0
        • C Christian Graus

          You can add this code to any button OnClickClick="return false();" and the click event will never fire. Replacing this with a call to a method that works out if it should postback, and returns true or false, will allow you to control your ASP.NET button from javascript.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          D Offline
          D Offline
          dreddy7
          wrote on last edited by
          #5

          On refresh, the page is re submitting the onclick event for print. It opens a dialog: "The page cannot be refreshed without resending the information.Click retry to send information again. or cancel to return to the page u were trying to view". On click of both retry or cancel ,the page still runs the code in print click. Can you help me? I tried using return false in onclientclick event.

          C 1 Reply Last reply
          0
          • D dreddy7

            On refresh, the page is re submitting the onclick event for print. It opens a dialog: "The page cannot be refreshed without resending the information.Click retry to send information again. or cancel to return to the page u were trying to view". On click of both retry or cancel ,the page still runs the code in print click. Can you help me? I tried using return false in onclientclick event.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #6

            Ah... OK. Now I see the issue. A refresh will indeed reprocess the postback. Why do you need to do a postback to do printing ? I thought that was done in javascript ?

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            C 1 Reply Last reply
            0
            • C Christian Graus

              Ah... OK. Now I see the issue. A refresh will indeed reprocess the postback. Why do you need to do a postback to do printing ? I thought that was done in javascript ?

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              C Offline
              C Offline
              ChrisKo 0
              wrote on last edited by
              #7

              My guess is that he's using an ASP:Button control for the print button and using the OnClientClick property for the javascript. I think a postback will happen in this instance and that might be what's causing the problem. I'm not sure how that script will affect the postback if you return false; as Christian mentioned. Maybe if you use

              button.Attributes.Add("OnClick","Javascript:PrintMethod();return false;");

              instead of the OnClientClick property it will behave differently. I don't have time to test this out right now, but maybe that would work?

              C 1 Reply Last reply
              0
              • C ChrisKo 0

                My guess is that he's using an ASP:Button control for the print button and using the OnClientClick property for the javascript. I think a postback will happen in this instance and that might be what's causing the problem. I'm not sure how that script will affect the postback if you return false; as Christian mentioned. Maybe if you use

                button.Attributes.Add("OnClick","Javascript:PrintMethod();return false;");

                instead of the OnClientClick property it will behave differently. I don't have time to test this out right now, but maybe that would work?

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #8

                If he adds a ClientClick only, then the button should not post back, but if he returns false, it definately would not post back.

                Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                L 1 Reply Last reply
                0
                • C Christian Graus

                  If he adds a ClientClick only, then the button should not post back, but if he returns false, it definately would not post back.

                  Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                  L Offline
                  L Offline
                  Lucky Sheikh
                  wrote on last edited by
                  #9

                  hey guy wht r u doing man if u want to avoid postback just type at form ispostbackpage=false simply byeeeee hope u understand lucky

                  D 1 Reply Last reply
                  0
                  • L Lucky Sheikh

                    hey guy wht r u doing man if u want to avoid postback just type at form ispostbackpage=false simply byeeeee hope u understand lucky

                    D Offline
                    D Offline
                    dreddy7
                    wrote on last edited by
                    #10

                    isPostBack is a readonly property right? so I am not able to do that. And actually I am using master page and content pages in my project. One of my content page has a table which needs to be printed on print button click. I have used asp:button control for this. And in my print_click event in vb, i have written the code like this: Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click Dim printScript As String = "" & _ "var foo = document.getElementById( 'plansheet' ); var bar = open('about:blank', 'myPopup', 'print'); bar.document.body.innerHTML = foo.innerHTML; bar.print();" ClientScript.RegisterStartupScript(Me.GetType(), "PrintScript", printScript.ToString()) End Sub The 'plansheet' is the id of my html table. This code opens a separate blank page with table alone in it and pops up the print dialog. When i use/cancel print in this dialog and go back to the original page and when i click F5 or refresh, the print is getting triggered once again and is openeing up the blankpage and printdialog once again. Hope I am clear in explaining my scenario. Thank you

                    D 1 Reply Last reply
                    0
                    • D dreddy7

                      isPostBack is a readonly property right? so I am not able to do that. And actually I am using master page and content pages in my project. One of my content page has a table which needs to be printed on print button click. I have used asp:button control for this. And in my print_click event in vb, i have written the code like this: Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click Dim printScript As String = "" & _ "var foo = document.getElementById( 'plansheet' ); var bar = open('about:blank', 'myPopup', 'print'); bar.document.body.innerHTML = foo.innerHTML; bar.print();" ClientScript.RegisterStartupScript(Me.GetType(), "PrintScript", printScript.ToString()) End Sub The 'plansheet' is the id of my html table. This code opens a separate blank page with table alone in it and pops up the print dialog. When i use/cancel print in this dialog and go back to the original page and when i click F5 or refresh, the print is getting triggered once again and is openeing up the blankpage and printdialog once again. Hope I am clear in explaining my scenario. Thank you

                      D Offline
                      D Offline
                      dreddy7
                      wrote on last edited by
                      #11

                      Hey guys , I got it!!!!!!!!!!!!:) Well I thought I cannot place javascript inside a content page as it will not be having a tag. But now, I have used a html button for print and used javascript right below the tag. This is not triggering the print on refresh! Thank you one and all for helping me out! cheers:)

                      L 1 Reply Last reply
                      0
                      • D dreddy7

                        Hey guys , I got it!!!!!!!!!!!!:) Well I thought I cannot place javascript inside a content page as it will not be having a tag. But now, I have used a html button for print and used javascript right below the tag. This is not triggering the print on refresh! Thank you one and all for helping me out! cheers:)

                        L Offline
                        L Offline
                        Lucky Sheikh
                        wrote on last edited by
                        #12

                        hey just add a button in ur report then at ur form write code in reportviewer me.hide on click event it will autmatically and when u want me.show simply first of all u have to invisible the reportviewer hope u understand byeeeeeeeeeee lucky

                        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