I have a repeater in my page with some pre-conditions such as a checkbox list and listboxes that dictate what the datasource for the repeater brings back. A button is clicked which will databind the repeater which works fine. However if the user decides they want to add something else in or remove something they can check/uncheck some fields and then hit the button again which will rebind the repeater and change what is displayed, however the viewstate of all the current controls in the repeater will be lost.
Binding the repeater in the initialization event would not be possible because the ViewState for the checkboxlist/listbox values are not available at that point in the page lifecycle, and those values are required in order to pass as parameters into my datasource for the repeater.
What are my options for maintaining the state of my repeater controls?
repeater control binding fully recreates all of children controls inside repeater templates. Because new DataBind result may (or may not) contains extremely different data inside repeater.
What kind of controls and their state you want to maintain inside repeater? Maybe using ordinal html controls and operating with theirs through Request.Form collection would be a better way?
If you want to save data from repeater before applying new databinding, best place for making it is a Page.PreRender event. In this event all Page controls already recreated and their viewstate already restored. So you may iterate by Repeater.Items collection and save data from repeater row by row. And after saving all the data you may rebind repeater controls according current filter values from page.
Related
We have a requirement where i need to delete the view state of particular repeater from Jquery/javascript.
Example
I have two repeater repeater 1 and repeater 2
onclientclick of button i want remove all the data of the repeater 1 along with the particular repeater viewstate.
i am able remove the data of the repeater using
$(tableid).remove();
but view state of the repeater still exist. I can not disable the view state of the repeater otherwise few functionality will not work.
You can't do that.
In this case, don't use a repeater. Create your own version of the repeater using js/jquery and html markup. You can maintain the state and load data trough callbacks using ajax.
I have seen posts which recommend that I populate the list within a if(!IsPostback) block.
However, I only generate the dropdown lists in response to postbacks so I dont think that will work for me.
What I currently do is databind the dropdownlists in Page_Init. However, when I inspect the list after a new selection is made (and therefore the autopostback has been triggered) using "inspect element" I see that the first item in the list is always "selected."
Please let me know how to keep the selection selected through a postback.
If you bind the control in Page_Init, then you'll lose your selection. Don't do that.
Bind the control once, then ViewState will retain the control data. But when the selection changes, that will be sent to the server, and the SelectedIndexChanged event will fire.
More generally, any properties you set on a control before the Render phase will be saved in ViewState. On the next Post Back, the control will load the contents of ViewState, and therefore will restore itself to the state it was in before the PostBack.
The control will then take note of any POSTed values that were sent on the PostBack. This includes changes in the selection, textbox contents, etc. The control will fire the appropriate somethingChanged event to indicate that there has been a change from the prior state of ViewState.
I have a gridview with checkbox in Itemtemplate. What i need to do is check the condition:
(checkbox.checked==true)
if its true take the rows to another gridview. i binded the data of the gridview in
if(!PostBack)condition.
Everything was working fine until my employer said he doesnt wants the gridview to be bound in the if(!postback) condition but if i take that condition out checkbox.checked value is false always!
From the details of your question, I'm not sure if your boss wants the grid unbound or doesn't want the page bloated with the grid's ViewState, but in my experience, it's usually the latter. However, if your boss simply doesn't want the grid bound in the "if(!Postback)" condition and doesn't mind if it's bound or uses ViewState, then your boss doesn't understand web development using C# and server-side controls.
But... to answer your question, the reason the checkboxes are all unchecked is because you're rebinding the grid AFTER the ViewState has been applied. ViewState contains all the data needed in a postback condition to set the state of the checkboxes during postbacks and applies that data prior to the OnLoad (or Page_Load) event. By rebinding the grid, you are essentially wiping out anything that was posted back in ViewState and applied. To overcome this, turn ViewState off for the grid and apply the state of the checkboxes yourself AFTER you rebind the grid using the FORM collection.
If you want to do away with ViewState, here are some tips in this article.
http://www.codeproject.com/KB/viewstate/DataGridViewState.aspx
I have a Repeater nested inside of a GridView. On the RowDataBound event for the GridView, I set the DataSource (based on one of the row's columns) and then bind the Repeater. This works fine for the initial load, however I need to be able to add new items to the Repeater dynamically.
I append an item to the DataSource, save it to the ViewState, and where I would normally bind using a method call, I bind to the object saved to the ViewState instead. The DataSouce reflects the change, however the page does not.
What am I missing? I have the exact same setup on another page without the nesting and it works perfectly.
if (ViewState["RepeaterObj"]!=null)
{
rpt.DataSource=(IList<DataTransferObject>)ViewState["RepeaterObj"];
}
else
{
rpt.DataSource = controller.GetObj(param);
rpt.DataBind();
}
I ended up resolving the question by cutting out use of the ViewState entirely, though I thought my temporary DataSource would be lost across the postback it wasn't. I ended up going with a class-level variable which works perfectly. It seems I didn't properly understand what happens during a postback.
First of all, you shouldn't be storing a datasource in ViewState. That's pretty much the worst place you could put it.
As for your problem, I would suggest either rebinding the GridView when new items are added to the repeater, or find the repeater in the event that saves the new record and rebind it there.
I think the problem is you are not rebinding the the Repeater. You say you change how you bind to look at the ViewState object but are you actually triggering the Bind to occur? It sounds like you are not and the page is just reloading with the current data stored with the control's ViewState.
Make sure you are calling your Repeaters bind event explicitly to sure it is getting rebound.
EDIT: I suspect it might have something to do where you might need to rebind your GridView and not just the Repeater.
I am creating dynamic label and textbox based on the number of values from the database for the selected item of the dropdownlist. Then the dynamic labels will have the names and the text box with the values. To retain the values of these controls im using Page_init event. So im using cache to hold the selectteditem from the dropdownlist.
Problem: The process is going fine. But if i try to refresh the page no items are selected in the dropdown list but the cache is not getting clear so using this cache value dynamic control are creating.
Unfortunately, dynamically added controls are not retained so you have to add them on every page load. So you will have to reload from the db, or store the total number of controls and recreate using the index on page init. ViewState will be reloaded into those controls to retain the previous value.
It's just that it's unfortunate that you have to do the work, but that is the case for dynamic controls. As an alternative, you could use a repeater which would retain the list of items that are bound to it.
HTH.