ViewState and client-side changes - c#

I noticed that some controls (e.g. TextBox) keep their changes between postbacks even if the change is made by a client-side script while some others don't (e.g. ListBox). Can anyone explain me why? Is there any way to extend the first behavior to other controls?
Thank you!

It depends on when the ListBox is being data-bound or ListItem are being populated.
Generally, such case would happen when the ListBox is created inside another parent control such as a Repeater, and the Repeater is data-bound at the Page_Load event. Which mean the ListBox actually does not exist until the Page_Load event is over.
ViewState is restored to the ListBox somewhere in between the Page_Init and Page_Load event of the Page Control. If the contents of the ListBox are created during Load event that means the ViewState of the ListBox is not able to restore the contents after PostBack and unable to keep track of and automatically select the new SelectedValue from the PostBack.
If the Repeater in this case is data-bound at the Page_Init event, the ListBox's contents would be ready after the Page_Init event and ViewState is able to restore correctly and automatically select the SelectedValue.
I usually data-bind everything at the Page_Init event to make sure controls are able to work with ViewState correctly.
Have a look at the page life cycle of ASP.NET web form for more details.
http://msdn.microsoft.com/en-us/library/ms178472.aspx

All controls keeps their changes between postbacks - Except if you create them again programmatically.
Probably your ListBox lose the changes because you populate it on every PostBack.
Try to do
if(!IsPostBack)
{
PopulateMyListBox()
}

Related

Save some values before button PostBack

Well I've got some textboxes dinamically created in a .cs file and I want to save/store their values when I click on Update button in a detailsview's edit mode, so I've got seted button's onclick event to a function to do this.
The problem is that the postback event occurs before the "OnClick" event, and then the textboxes' values are lost.
How can I save/store their values first?.
Please, help me with this.
Thanks in advance!
You can detect the page's first loading or post back(OnClick, SelectedItemIndexChanged like..) with the Page.IsPostBack property.
You should check this property value in the Page_Laoad event handler.
It helps to understand the ASP.NET page life cycle.(See here for more info.)
Basic events are as follows:
PreInit
Init
PreLoad
Load (controls are filled with postback values here)
Control events (OnClick happens here)
PreRender
Render
Unload
Usually during the "Init" event you want to create all of the controls, including any dynamic controls for the user.
During the Load event asp.net copies all of the post date back from the client to the controls in the page. But in order for this to work on the postback the same controls that were created on the initial render must exist in the same order with the same IDs.
Then in PreRender you can change the page state including adding and removing controls for the next post.
So to clarify here's how I would imagine your page flow:
OnInit - Construct your controls and populate with the initial values.
OnLoad - During postback your control will be populated with the users updated values.
OnClick - Use the event of the user's button click to then save the updated values.

Get all items from Repeater's datasource when using pagination

I've got a Repeater control, bound to a PagedDataSource, which datasource is a list of custom controls I've made. These custom controls contains a couple of text boxes.
I have a save button, and when it is clicked I want to save the data in all the custom controls to a database, no matter which page they are on - but currently I only got access to the custom controls displayed on the current page.
What I've tried to do is to, in the btnSave_Click event, create a new temporary datasource equal to the current one, except its not a PagedDataSource. That way my repeater contains all custom controls - BUT - the changes made in the textbox fields are no longer available. I then tried to add JavaScript onchange events on the textboxes in the custom control, so that a postback would be fired whenever text was changed, and the property in the user control codebehind would be updated. This didnt work either.
Any ideas?
save the changed values on each page index changing event (or prev /next buttons) into your persistance object (List)
http://www.dotnetfunda.com/articles/show/1611/how-to-select-multiple-records-from-multiple-pages-of-the-gridview-and
The reason your non-PagedDataSource is empty is because the changes in your text box exist in the client and not on the server - you'll need to synchronise the values from your controls with the empty slots in your repeater.
The Repeater does not have built-in Pagination (like the GridView or other complex controls) so it does not offer events such as the PageIndexChanging event. I assume therefore, that you have your own Page navigation implementation. You should therefore call the function you have presented within that implemented function.
Try Using a generic List and Skip and Take methods of that

ASP.NET form with repeaters that dynamically generate input controls

I have a repeater control that pulls back some data from a database and creates a radiobuttonlist for values related to each row. I don't know how many rows will be coming back, so I don't know how many radiobuttonlists there will be. This repeater is inside of a form, and once I click "Submit" on the form I need to get the values from the radiobuttons generated by the repeater.
Any thoughts on how to do this?
There are a few options.
One is to use a nested Repeater to display the RadioButtons.
The other is to create the controls during the original repeaters. Repeater.ItemDataBound event.
In either case, you need to be very aware of the Page Lifecycle.
You'll want to avoid databinding in the Page_Load event, unless the databinding is being done with an if(!Page.IsPostback) block. If you fail to check for postback, you'll run into issues where the Page_Load calls a DataBind() call that then wipes the values of the repeater before any event handlers can use it.
Alternatively, you can do your binding in Page_Init, which occurs BEFORE the viewstate can be applied, so you won't lose your values.
You can also use USer Controls for the RadioButtons. There's a previous post that covers how to use nexsted user controls within a Repeater here.

Dynamics controls lost on postback

This old chestnut again.
My page is constructed as follows; I have a dropdownlist which is databound on first load. When the user selects a value from this, a postback is performed which then databinds a repeater control.
The ItemTemplate of this repeater control contains a placeholder control. In code behind in the ItemDataBound event of the repeater, I am adding two controls dynamically to this placeholder, a hiddenfield and a checkbox.
When the user clicks the save button, I then want to iterate over all those dynamically created hiddenfields and checkboxes and determine their values. However when the user clicks the save button, those controls no longer exist as shown in the page trace.
I know this is a lifecycle issue and the articles I've seen on this suggest using Init methods to dynamically create your controls but I can't because of the way my page works, e.g. the repeater control only appears and binds after a value is chosen from the dropdownlist.
What do I need to do to maintain the dynamic controls through the postback caused by clicking on the save button?
The problem is when you hit the save button probabily you dont re-bind the repeater and the controls you have added at run time withint the ItemDataBound event are not longer available(because they don't exist anymore)
Why don't you add those control at design time using the Eval function the set up the value of the hidden field?
You just don't create them dynamically just on the on selection change of the drop-down set visibility true or false for the repeater that will solve your problem.on post back you have to again create those control as they are Dynamically created.

creating dynamic controls using ajax on asp.net

I have button Add and Remove on my page.
Add button adds one checkbox, two textboxes and one dropdownlist to a new line on my page.
Remove button removes them.
I have this running nicely by following Joe Stagner's example.
Problem:
The controls that are created dynamically all need to fire the same event when checked (for checkboxes), also for selected index changes (for dropdownlists).
I have tried to add event handler when I create an object but it doesn't seem to fire?
you need to persist the dynamically created controls in some way [session, viewstate, etc.] for each page load. Recreate the dynamic controls and re-bind the events using delegates on each page load in preInit function.
I think you're probably running into the fact that your page, upon each page post, is being completely recreated - essentially the page has to duplicate what controls were on your page before it can attempt to feed postback (and events) to them. I think what you probably need to do is add code to your page_load which will re-create the dynamically created controls, with the same ids as they had, and register the event handler.
Sounds like you have a page life cycle issue.
For a dynamically created controls to fire events you should create them in the PreInit event of the page.
Here's a link to a cheat sheet for Asp.net page life cycle.
yeah,
like what all said, it is Life cycle issue.
when you load user controls dynamically you should always do the following.
Assign a unique ID for each User Control.
Reload the user controls on Page_Load or Page_Init Events.
and to make it all easier i suggest to abstract the loading to a function that you will call from Page_Load and Page_Init as mentioned before, this function will check if hte target user control was loaded and will load it again for you, to do that, you store the loaded user controls IDs in Session or viewstate.
hope this helps.
If you want to do it without auto post back you can remove the auto post back and throw and ASP button on there. Any runat server should fire off your dynamic event handlers.

Categories