I've a page that has a series of web controls. One of these is a textbox with AutoPostBack turned off.
The way the page operates is that a button is clicked to save the form data. This button sits outside of the updatepanel.
So when I hit the save button the partial postback happens for the dropdownlist and after this postback has completed a full postback fires for the save button. However when the full postback fires the form data is returned to the state before the save button was clicked - i.e. my changes are removed.
I believe this could be to do with the viewstate being returned from the partial update and that viewstate not updating in the page before the full postback fires - or it getting corrupted.
Does anyone have any ideas?
Thanks.
Don't mean to sound negative but these scenarios are what made me give up "by the book" ASP.net AJAX. Learning jQuery /w simplistic ASP.net forms /w NO postbacks has lead me to build more useful and cooler UI experiences than what I had to battle to get working with update panels etc.
If you set UpdateMode="Conditional" and ChildrenAsTriggers="true" on your UpdatePanel, that will ensure the partial postback only executes when the DropDownList's postback event fires, not when the Button is clicked.
Thanks for the quick response! However I need the save buttons click event to fire too. The order in which the events fire is perfect:
dropdownlist changed event (partial)
save button click event (full)
The problem is the loss of form data after the partial postback.
Many thanks.
I've already posted a solution to this on another post. This simple code will ensure that your viewstate works with both the postback and partial postback.
Ideas for how to deal with viewstate when using ASP.NET AJAX and update panels
Related
I am trying to take advantage of the page lifecycle in Asp.NET so that I don't bind to my datasources more than I actually need to.
This has led to the following problem. I have an ObjectDataSource, GridView, and a button on my page. The button adds a record to the database, which should be picked up by the data source and presented on the Grid. The problem is that the item is not showing up on the gridview until I refresh the page.
I can solve the problem by calling GridView.DataBind() in my button's event handler, but this goes against what I understand about the .NET Page Lifecycle.
Based on this article, the lifecycle should be as follows:
In addition the article states that the Databinding event is "Raised after the control's PreRender event, which occurs after the page's PreRender event."
So, my button click event should fire first, during the Event Handling phase. It should add the record the database.
Next PreRender should be called on the controls.
Finally DataBind should be called on the controls, and the Grid should update to capture the new record.
Yet this doesn't seem to happen. What am I not understanding?
I think the issue is that your viewstate is not enabled on your GridView. This is what I experienced, but then I also had to call DataBind() on the GridView from the page PreRender event if the request was a postback to get the data updated in the GridView on postback.
Edit:
It would help to understand the issue and context better if you could post the source code of your page (aspx + codebehind). How and where do you connect your GridView to your datasource? Statically in markup or dynamically? Do you make any calls to page.DataBind()? ... These things may influence the behaviour of the GridView.
I have a ASP.Net(C#) page. When a user selects a value in dropdownlist, some new controls will be visible to him.
I am able to that now, but the page has to reload every time and state has to be maintained between postbacks.
Is there any way to do the same without reloading of the page in a easiest way possible without using ajax control toolkit?
Additional info: the data for the dropdownlist is coming from the database, and some places it has to be all server side so i can't use javascript .
You can try this idea: http://msdn.microsoft.com/en-us/library/ms178208(v=vs.100).aspx. I have not try it before but it seems logical.
you should go through updatepanel. Put all the controls inside a updatepanel which you want to show on selectedindexchanged event of the dropdownlist and write code behind on selectedindexchanged event of the dropdownlist accordingly. Hope this will help you...
I have a control which has an ImageButton which is tied to an OnClick event... Upon clicking this control, a postback is performed and the event handler is not called. AutoEventWireup is set to true, and I've double checked spelling etc.... We haven't touched this control in over a year and it has been working fine until a couple of weeks ago.
We have made changes to controls which load this control... so I'm wondering, what kind of changes could we have made to stop this event handler from being called? There is quite a bit of Javascript going on, so this could be the culprit too...
Edit: Some clarification... we are dynamically loading the parent control of the ImageButton in the OnLoad event of the page... if that makes sense.
AutoEventWireup is irrelevant. Is your ImageButton loaded dynamically, i.e. not written out in mark up? If it is loaded onto the page late in the Page lifecycle e.g. in PreRender then the event will not fire.
If there is a JavaScript issue your page will not even PostBack. Is that happening?
Did you give the ImageButton an ID?
I def agree with what BritishDeveloper said. I had a similar problem where I was dynamically loading controls, but I couldn't get a reference to the control using Page.FindControl("controlName") Someone pointed out that I needed to keep the page lifecycle in mind. I found out I needed to make sure to load the control in the PageInit because after doing an async postback the control was still there, but not loaded in the postback so there was no way to find it. This was all in csharp codebehind and ajax though, but I'm guessing the control isn't getting reloaded.
So, as it turns out we set the PostbackUrl property on one of our buttons in control A... this caused the event handlers for control B not to fire when a button in control B was pressed.
If you create a control dynamically. Any time you fire a postback using the new created control, you need to recreated it. Just think that your application are running at a server. How can the server hold information on controls created dynamically? Don't use Page.IsPostBack to create postback. PostbackUrl is bad solution. Workarround will be need.
I can go into a little more detail ... I just lost several hours fixing my own issue similar to the issue described here. In the course of creating some search controls, I added a pair of ImageButtons and set the PostbackUrl properties on them. A few days later while testing new code, I noticed that none of my other buttons on the webform were posting back properly. They would fire a postback, but the code-behind was behaving as if every postback was an initial page request and none of the event handlers were firing.
After several hours of troubleshooting, I came across this post. When I went back and removed the PostbackUrl from those two ImageButtons, everything went back to normal. I don't know why it was causing this issue, but the fix mentioned here worked for me. I want to add that my ImageButtons were not dynamically added ... they were in the markup and this issue still cropped up. Search your markup for any controls with PostbackUrl set ... remove that (program around it if needed) ... see if your event handlers will fire properly.
I am trying to get a crosspage postback to work in asp.net 2.0 the issue I seem to be having is the button that I press is meant to use it's on click event to store some session variables based on the values of other controls. This button has crosspage postback property to the relevant page.
The on click event seems to not run at all, it just seems to perform the postback to the other page straight away.
Is this standard behavior? Any work around?
I think I have found my answer
http://community.sgdotnet.org/blogs/chuawenching/archive/2007/03/08/ASP.NET-2.0-DataGrid_2F00_GridView-CrossPage-PostBack-.aspx
Seems like the transfer will be instant, no onClick event will run. The alternative is to use Server.Transfer in the onClick event.
Any other comments appreciated.
My understanding of the order of page events is this:
Page : Load
Control : DataBind (for a GridView or whatever)
Control : Load
Control : Clicked (for a Button)
Page: PreRender
Control : PreRender
(There are lots of others - but these are the ones I'm interested in)
The important thing to notice here is that the button's click event comes after the gridview's bind event. If the button causes a change to the data, the GridView displays the old data. I could rebind the control in the PreRender event, but that seems totally ugly.
This must be a very common pattern (a button that updates data). How do I put this together so that the GridView binds to the data after the Button click changes it?
The answer was in the Button Click event, after the data has been changed, call DataBind() on the page to cause the GridView (and anything else that needs it) to rebind. I didn't realise you could do that.
Thank you Ocdecio & Mufasa - I would mark your answers as helpful, but I got no rep yet.
ASP.NET does, by default, lots of binding and rebinding. Rebinding after a click event is normal.
The only reason the button click event comes after GridView bind is because you programmed your page to do that . I don't see any problem in binding the control in the PreRender event, in fact that is the only way to take action after a control event (such as Button onclick).