Accessing GridView's ViewState - asp.net

What I have:
I have a GridView without paging. In the Page_Load handler of the containing Page I read user specific values from a table storage and store them in a collection so the SelectMethod of the ObjectDataSource can get them.
What I want:
When the user uploads some new data from the same page the GridView is in I want the GridView to show the new data additionally to the old one without querying the storage again.
Helpful stuff:
IsPostback within the Page_Load handler helps avoiding queries when the data is still in the ViewState. After the user clicked the upload button he can still see the old data thanks to the ViewState of the GridView I guess.
My attempt:
I tried to derive from GridView and make my own user control in order to have access to the ViewState Property that returns a StateBag but it cannot find the user data that is displayed in it. It shows only stuff like DataSourceID, _!ItemCount and so on.
My Question:
How to access ViewState from a GridView in order to retrieve data like the column values or the DataKeyName values from code behind.
Additional info:
The answer to the question does gridview save data in viewstate? states that the GridView saves its data in its ViewState.

You definitely don't want to access the GridView's view state. That's an internal implementation detail of the control and could change at any time.
All you need to do is call DataBind() on the GridView after you set the new data and the control will show the updated data.

Related

Best Way to Postback Dynamic Table

I have a server-side table control. Since I don't know how many rows and columns it will have beforehand, I build the contents of this table in my pages Form_Load event handler.
That works well. But I'm populating that same table with textboxes, and I need to be able to read those textboxes if a submit button is clicked.
As it stands, it appears the table contents are cleared upon postback.
What is the best way to dynamically create textboxes in a table, and then read the values of those textboxes when the page is posted back?
If you want to persist the values on postback you will need to recreate the table control hierarchy on the Page_Init event. This usually means that you will need to rebind your table control to a data structure coming from the database.
Once the control hierarchy is in place, ASP.NET will load any data from the ViewState. This data can be in fact data that was previously set from the server on the textboxes. This step will ensure that these values are preserved on the control.
In the next step ASP.NET will process the POST data, and here basically will update the table textboxes with the input from the client (browser). Any values set from the ViewState will (most probably) be overwritten.
All this happens before Page_Load, so at this stage you should be able to access the client textboxes values.
Check out the ASP.NET Page Life Cycle, especially this image.

Automatic data binding mysteries

I have some questions about how and when data sources are bound in the page lifecycle, and I can't seem to find the answers anywhere.
I have a gridview which is bound to a data source at design time. One of the parameters for the DS is the selected value of a dropdown list. These dropdown lists are also databound to retreive their options, and the dropdowns have their auto-postback property set true. Now some things are confusing me about this.
Most of the time changing the value of the dropdown will reload the gridview with the new parameter, and it's not necessary to manually call databind() on the gridview in order to do so. However there are situations where the gridview is not rebound, and I'm not sure what these conditions are. When will a data-source be rebound automatically upon parameter change, and when do you need to call databind manually from code?
Does calling databind from codebehind prevent the automatic databind event from firing, or will they both fire, resulting in a wasteful extra query of the data source?
If the former, is there a way to stop the extra databind from occurring without having to move everything into code behind and lose the convenience of setting up data sources for the controls in the design view?
Are the answers to these questions documented on any official sources? (MSDN, etc)
I can't be sure without looking at you code, but you probably want to read about ViewState.
Once the control has been loaded, the data stays in the viewstate. You generally want to do the DataBinding on (!IsPostBack) event so that on postbacks you do not need to rebind the data from the DB.

ASP.NET, what is a datagrid bound to after postback?

I have a gridview full of telephone numbers. To populate the gridview I bind the gridview's datasource to a List<> of telephone numbers. I do this when the page is first loaded, but not on postbacks.
I want the user to be able to delete some of the telephone numbers, and then, if they want, click a Save button, and this will update the database, otherwise their changes will be ignored. So I have a button in the grid, and an event is fired, and I can call DeleteRow(row index) and remove the row from inside this event. For some reason this doesn't work.
All the gridview examples I find on the Internet execute the delete straight away by calling an sql function, and then bind again. And some examples bind the grid every time the page ios loaded, which seems inefficient.
My questions is:
The delete button causes a postback to the server. On postback the list of telephone numbers no longer exists. And the gridview's datasource is null. The grid is no longer bound. But there must be data somewhere, because the data in the grid is still visiable. Where is this data, and can I delete a row of it, so that a row in the gridview is deleted?
The viewstate saves the contents of the datagrid, so the answer is "The Viewstate"
Understanding the viewstate is essential to understanding how ASP.NET works, so rather than posting just enough info to answer your question, I'm going to recommend you read the entire article I linked to.
You can use jquery , you need to save the Datakey value of each deleted row in a hidden field
and hide the selected row , and when user clicks save , u can delete the rows based on hidden field values # code behind.

ASP.NET GridView problem

Well i have a gridview where i have defined the columns on my own and turned autogenerating off but now i have the problem that i cant access GridView.SelectedRow.DataItem.
As it turns out to be null now, when it had a value when auto generation was turned on..
Edit:
What i need is a way to save the ID of the row while not showing the ID to the user so if there is any way to do this?
I'm guessing DataItem is only properly filled when you are using DataBinding.
Are you using DataBinding?
Ok from this url:
The GridView (and actually, all our
data controls) does not save data
items across postbacks. This reduces
ViewState (if the objects are even
serializable) and enables garbage
collection to happen and clean up your
objects. So, when you click the
button to post back, the GridView has
not called DataBind and therefore your
data item isn't there. This is what
you've discovered.
Guessing you're reading the value from a postback, might just be the problem.
Try using SelectedValue, if you've setup the (primary) key for the items.
I've always used that and it worked.
msdn about SelectedValue
You can create a new hidden template column that will have a label with the ID . and in the cs file you use .FindControl on the rows.
You also have DataKeys property on the gridview, witch I think also does what you want

Listbox values are persisting across postbacks

I am having a listbox in ASP.net. I am populating the listbox values from another listbox in a page dynamically. During postbacks the values of output listbox are not persisted.
(while going to another page and come back to this page).
Please suggest some good answer. EnableViewstate = "true" is not working.
Are you doing anything in Page_Load that should be in a
if(!IsPostBack) {}
Initialization code in load needs to only be called when the page is first loaded, not on postbacks.
If you are going to another page and then coming back to this page, I think you need to preserve the information yourself in the Session and then restore it when you come back to the page.
The viewstate is only preserved as long as your on the same page doing postbacks.
As Lou Franco wrote
if(!IsPostBack) {}
You use this on the initial pagerequest to fill in the data. if you wish to preserve the data across pages using the session to store the values is the best bet.
preferably you fill in the data in your listbox before the SaveViewState event thats in PreInit as far as I recall.
Initialize the content of your controls in your Page's Init event (Page_Init). That way any values the user supplies are not overwritten by your defaults.
EnableViewState will just repopulate the output listbox with the values that it had when the page first rendered, since they're still the ones stored in the viewstate. The browser sends only the selected value in the postback, so there's no way for the server to know what other values you added on the client.
You can work around this by adding a hidden input to the page and populating it with the dynamic values when you update the listbox. Your page can then check that value during a postback and repopulate the list properly.
Changes made to the listbox on the client side are not persisted during a postback, you need to record that information in hidden fields and then configure the control during the page_load event to make the changes stick during the rest of the postback.

Resources