How to prevent postback on button click.-VBForums
Results 1 to 25 of 25

Thread: How to prevent postback on button click.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    149

    How to prevent postback on button click.

    I have this one window which is a webform for a service request. On the form are buttons that will open another window to allow the user to make choices which will then get returned to the opening window.

    The problem is that unless the values are returned to textboxes the returned values will get wiped out when you click a button to open another window. Such as the HTML that I return to a label for display purposes.

    Now the problem is that when the button is clicked it posts back to the server.

    Can anyone tell me how to prevent postback on the button click.

    Thanks.
    Last edited by token; Jan 11th, 2005 at 11:16 AM.

  2. #2
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    Re: How to prevent postback on button click.

    Instead of using an <asp:Button> just use a regular <input type="input"> and put the javascript in its onlick event ?


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    149

    Re: How to prevent postback on button click.

    Well i was using an asp button and setting the onclick event on the page load, but I figured out what I needed to do.

    When I set the attributes of the button, after the whole window.open(blah blah) i have to put 'return false' to prevent the postback.

    Thanks for the reply.

  4. #4
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    Re: How to prevent postback on button click.

    Well yeah you can do it that way... but why?

    Using a normal button would be a lot better and would save you having to use return false.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    149

    Re: How to prevent postback on button click.

    I could try that, I am quite new to vb.net. So I haven't really adopted a standard to how to do things, I am just trying things out and if they work great.

    I will give your suggestion a try.

    Thanks again.

  6. #6
    Lively Member
    Join Date
    Nov 2003
    Location
    Amsterdam
    Posts
    74

    Re: How to prevent postback on button click.

    Quote Originally Posted by TomGibbons
    Instead of using an <asp:Button> just use a regular <input type="input"> and put the javascript in its onlick event ?

    But how can we call any code behind code from the Javascript?
    I have a button (<asp:Button>). The click event is used to create/update a dynamic datasets used for populating several controls.

    I need the button to be <asp:Button> only beacuse at the click event I am implementing quite a few business rules also.

    The click is creating the post back and by dynamic datasets are created all over again. Currently I am managing it by saving the datasets in the sessions and retrieving from the same.

    If there is some way to prevent the button click event to cause the Post back, I dont need to do all those juggling with session.

    Is it possible ?
    Everything I code, is a piece for Museum.

  7. #7
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    Re: How to prevent postback on button click.

    Okay. Well if I understand you correctly, you have some code that is being called at postback when you don't want it to? If this is the case, surround that particular code in an if statement like this
    Code:
    C#
    if ( !IsPostBack )
    {
         // Create Dynamic Data Sets Here
    }
    
    
    VB
    If Not IsPostBack Then
         ' Create Dynamic Data Sets Here
    End If

  8. #8
    Lively Member
    Join Date
    Nov 2003
    Location
    Amsterdam
    Posts
    74

    Re: How to prevent postback on button click.

    Quote Originally Posted by TomGibbons
    Okay. Well if I understand you correctly, you have some code that is being called at postback when you don't want it to? If this is the case, surround that particular code in an if statement like this
    Code:
    C#
    if ( !IsPostBack )
    {
         // Create Dynamic Data Sets Here
    }
    
    
    VB
    If Not IsPostBack Then
         ' Create Dynamic Data Sets Here
    End If
    Thankx for your quick reply...

    My problem is, I have this DataSet which is having all the DataTables required to fill all my different controls. I have declared it as the class member itself. All other different methods need to share the same copy of the DataSet. I am calling

    Code:
    	private void Page_Load(object sender, System.EventArgs e)
    	{
    		if(!IsPostBack)
    		{
    			fillDataSet(); //I am feeling the DataSet the default values.
    		}
    	}
    I am having 3 more methods which updates the same dataset based on the client action, basically button clicks.

    I am doing the following tasks at one of the button click event :

    Code:
    private void btnRightShift_Click(object sender, System.EventArgs e)
    {
          DataSet dSet;
          DataTable dTable;
          dSet= (DataSet)Session["SortTable"];
          dTable= dSet.Tables["Source"];
    
         //Do all the dataset operations
    
        //Again Save the dataset to the Sessions for the other methods to use  :( 
    
         Session["SortTable"]=dSet;
        //Do other stuffs
    }
    Same for all the 3 click events. ( I have 3 buttons, except the "Finish" )

    I need to do this as these button clicks are making the page to be posted back and the DataSet object is created again, and all the updates by the user are lost.

    Basically, I dont need the form to be posted for those 3 button clicks, I do have a "Finish" button to finally post the form.

    Can I prevent that to happen ?
    Everything I code, is a piece for Museum.

  9. #9
    New Member
    Join Date
    Jul 2009
    Posts
    1

    Smile Re: How to prevent postback on button click.

    <asp:ImageButton ID="btnImage" runat="server" OnClientClick="return false;"/></a>

    This will disable postback event.

    Sejal

  10. #10
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: How to prevent postback on button click.

    Hey,

    Can I ask why you have posted on a thread that is almost 4 years old?!?!?

    It is unlikely that this thread is still outstanding!!

    Gary

  11. #11
    ASP.NET Moderator mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174

    Re: How to prevent postback on button click.

    A first post that shall live forever in infamy

  12. #12
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    Re: How to prevent postback on button click.

    I've been racking my brain for 4 years, working diligently on trying to find a solution to this problem. Thank you. Now I can rest easy.

  13. #13
    New Member
    Join Date
    Jun 2011
    Posts
    1

    Re: How to prevent postback on button click.

    Quote Originally Posted by gep13 View Post
    Hey,

    Can I ask why you have posted on a thread that is almost 4 years old?!?!?

    It is unlikely that this thread is still outstanding!!

    Gary
    It always amazes me when I see posts like this... Whenever you respond to a question, you are not only answering it for the original poster, but for every person that enters the question into a search engine and ends up on this thread thereafter.

    Thanks for answering this question. It was very helpful to me, and i'm sure will be very helpful to many others in the future.

  14. #14
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: How to prevent postback on button click.

    Quote Originally Posted by mjs1234 View Post
    It always amazes me when I see posts like this... Whenever you respond to a question, you are not only answering it for the original poster, but for every person that enters the question into a search engine and ends up on this thread thereafter.

    Thanks for answering this question. It was very helpful to me, and i'm sure will be very helpful to many others in the future.
    Hello,

    That is actually a very good point!

    Gary

  15. #15
    New Member
    Join Date
    Oct 2011
    Posts
    4

    Re: How to prevent postback on button click.

    I did the recommended addition of OnClientClick="return false;"

    I ran the program and didn't get the postback. BUT my button events didn't occur, either. Then it occurred to me: putting in the recommended code basically turned off the button events.

    The whole point of this particular problem, was that button clicks cause a page reload (called a postback). that's no good at all. I happen to have state variables in the form of Booleans and other populated variablesthat can't be reset just because a button is clicked.

    So, I am repeating the original problem (and I don't care if it is 4 years old like someone said!) the problem is current! When I click a button, I don't want the page to reload. I don't want all the Public and Private variables to get reset. I just want the button event to fire.

    how do you do that? And while I am at it, why is the default behaviour of a button click to do a postback anyway?

    Thanks in advance,
    paul...

  16. #16
    New Member
    Join Date
    Oct 2011
    Posts
    1

    Re: How to prevent postback on button click.

    wow, 6 years and still active...

    Paul have you found a solution to your problem? I have the same question...

    I have a button whose code behind event I want fired when it's clicked on, but I don't want it to cause a postback... sounds strange (and someone illogical too, I appreciate that!), and there are possibly workarounds as well, but this is an interesting problem and I want to see if it can be resolved as is. anyone any ideas?

    Paul, in case nobody's got a bright idea, you may want to take a look at:
    http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx

    it explains how to determine what control caused the postback and then get creative from there. But I still badly want this question answered!

    thanks for yout time.

  17. #17
    New Member
    Join Date
    Oct 2011
    Posts
    4

    Re: How to prevent postback on button click.

    The workaround I did, which is not a very efficient one, is based on the fact that postback situations do restore form controls to their pre-postback condition, so I created a bunch of hidden text boxes that contain all of my important variable values. It works, but isn't a very good way to do things.

    There has to be a way around this, otherwise, no user button operations would be allowed as it would cause postbacks and everything gets reset!!

    Something is missing in 1) how all of us are presenting the problem or 2) developers have set some properties that result in NOT getting the problem to begin with and they don't realize it so they can't help us.

  18. #18
    New Member
    Join Date
    Oct 2011
    Posts
    4

    Re: How to prevent postback on button click.

    I've just discovered something that is relevant to this entire problem. I'm surprised that the experienced .net programmers didn't know this. Us newbies wouldn't have known about it, that's for sure.

    Let's say you have 2 private variables in your class. On the page_load routine, you need to check for the postback condition and do the following:

    Private SortField As String = ""
    Private PageIndex As Integer = 0

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Page.IsPostBack = False Then
    ViewState("SortField") = SortField
    ViewState("PageIndex") = PageIndex
    Else
    SortField = ViewState ( "SortField" )
    PageIndex = ViewState ( "PageIndex" )
    End If
    end sub

  19. #19
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,009

    Re: How to prevent postback on button click.

    You can use an updatepanel.
    Putting every variable on the viewstate is not always the right way.You should limit the amount of data you put on the viewstate.
    Slow as hell.

  20. #20
    New Member
    Join Date
    Oct 2011
    Posts
    4

    Re: How to prevent postback on button click.

    What is an updatepanel? And how would you code the placement and retrieval of the variables on a postback?

    Why can't viewstate handle all of the data that is needed because of the postback? The real solution to this is to not do a postback every time a button is clicked. I have another page that I will be supporting that has a menu-driven multiview control. Every time a menu is selected, there is a postback, when all that is really necessary is to select a corresponding tab on the multiview.

  21. #21
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,009

    Re: How to prevent postback on button click.

    Then use a jquery menu.
    About the updatepanel http://msdn.microsoft.com/en-us/library/bb399001.aspx .
    Viewstate is adequate for a small amount of data, if you decide to put, let's say, a dataset data to viewstate you obviously trying to ruin your project.
    Slow as hell.

  22. #22
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: How to prevent postback on button click.

    I have to agree with sap here. Adding lots of data into ViewState is not a good idea. At the end of the day, this will make the page slow to load, and also difficult to maintain.

    Gary

  23. #23
    New Member
    Join Date
    Sep 2012
    Posts
    2

    Re: How to prevent postback on button click.

    This does the trick:

    OnClientClick="return confirm('Are you sure?');"

    sorry I'm late!

  24. #24
    New Member
    Join Date
    Sep 2012
    Posts
    2

    Re: How to prevent postback on button click.

    This does the trick:

    OnClientClick="return confirm('Are you sure?');"

    sorry I'm late!

  25. #25
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,009

    Re: How to prevent postback on button click.

    No..
    Slow as hell.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.