-
Jan 11th, 2005, 11:17 AM
#1
Thread Starter
Addicted Member
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 12:16 PM.
-
Jan 11th, 2005, 12:32 PM
#2
Frenzied Member
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 ?
-
Jan 11th, 2005, 12:41 PM
#3
Thread Starter
Addicted Member
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.
-
Jan 11th, 2005, 02:05 PM
#4
Frenzied Member
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.
-
Jan 11th, 2005, 02:28 PM
#5
Thread Starter
Addicted Member
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.
-
Jan 13th, 2005, 07:26 AM
#6
Lively Member
Re: How to prevent postback on button click.
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.
-
Jan 13th, 2005, 07:50 AM
#7
Frenzied Member
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
-
Jan 13th, 2005, 08:13 AM
#8
Lively Member
Re: How to prevent postback on button click.
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.
-
Jul 28th, 2009, 03:55 AM
#9
New Member
Re: How to prevent postback on button click.
<asp:ImageButton ID="btnImage" runat="server" OnClientClick="return false;"/></a>
This will disable postback event.
Sejal
-
Jul 28th, 2009, 03:59 AM
#10
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
-
Jul 28th, 2009, 09:08 AM
#11
Re: How to prevent postback on button click.
A first post that shall live forever in infamy
-
Jul 28th, 2009, 01:37 PM
#12
Frenzied Member
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.
-
Jun 7th, 2011, 08:47 AM
#13
New Member
Re: How to prevent postback on button click.
Originally Posted by gep13
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.
-
Jun 7th, 2011, 11:41 AM
#14
Re: How to prevent postback on button click.
Originally Posted by mjs1234
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
-
Oct 14th, 2011, 01:35 PM
#15
New Member
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...
-
Oct 20th, 2011, 02:04 AM
#16
New Member
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.
-
Oct 20th, 2011, 08:11 AM
#17
New Member
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.
-
Oct 20th, 2011, 12:55 PM
#18
New Member
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
-
Oct 20th, 2011, 05:46 PM
#19
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Oct 21st, 2011, 07:35 AM
#20
New Member
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.
-
Oct 21st, 2011, 05:19 PM
#21
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Oct 26th, 2011, 02:19 PM
#22
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
-
Sep 13th, 2012, 08:28 AM
#23
New Member
Re: How to prevent postback on button click.
This does the trick:
OnClientClick="return confirm('Are you sure?');"
sorry I'm late!
-
Sep 13th, 2012, 08:29 AM
#24
New Member
Re: How to prevent postback on button click.
This does the trick:
OnClientClick="return confirm('Are you sure?');"
sorry I'm late!
-
Sep 13th, 2012, 01:11 PM
#25
Re: How to prevent postback on button click.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|