Best Way to Postback Dynamic Table - asp.net

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.

Related

Create Dynamic Controls using retrieved data [Asp - Vb .Net]

I'm trying to create multiple controls by using retrieved data from query, but preventing them from dissapearing on postback, allowing me to get and mantain their values, the problem I have is that I cannot create them on Init because the number of controls, their IDs, and other properties are only known after user selects an item on menu.
Page loads a Menu with all its items and values (Data dependent), plus, a button is loaded too
User clicks a item on menu.
Selected value is used to perform a query by using a Dataset (This happens inside a function which is called from Menu_ItemClick event).
Retrieved data from query is used to determine how many controls must be created (2, 4, 6, etc.). Each control has its unique ID, which is given according to data.
Controls are created and placed into a Panel (named p).
Now controls are visible and available for editing (RadioButtons, TextAreas)
User clicks on button to save information from dynamic controls into a Database
Problems I'm facing
Controls dissapear on postback when clicking button, because they weren't created on Init.
Placing button on UpdatePanel to prevent whole page postback, makes dynamic controls not accesible when trying this:
For Each c In p.Controls
...
Next
The only control it gets is a single Literal control (Controls count is 1), as if other controls didn't exist.
Thank you in advance.
When you wrote "Controls dissapear on postback when clicking button, because they weren't created on Init", did you mean to say that "Controls dissapear on postback when clicking button, because they weren't re-created on Init"? If not, then that is likely a root cause of your problem - dynamically-created controls must always be recreated in response to a PostBack (cf. ASP.NET dynamically created controls and Postback). There may be other issues as well, as dynamic controls in Web Forms can provide a lot of challenges as your scenario gets more involved - here's one article that lays out many of them under various scenarios http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx (e.g., if the user can re-select from the DropDownList to generate a different set of dynamic controls). The canonical reference on all of this is http://msdn.microsoft.com/en-us/library/ms178472.aspx.
Now, on PostBack you'll need some way to ascertain which controls were dynamically created so they can be dynamically re-created. As such, you'll need to store somewhere whatever information allowed you to dynamically create the controls. Since ViewState isn't available in Page_Init and there can be other issues introduced when using sessions, my suggestion is to simply declare a HiddenField that contains that state information. In Page_Init, you'll then need to get the HiddenField's value from Request.Form (since the value of your HiddenField won't be loaded until after Page_Init from ViewState) and go from there to re-create your controls.
My final suggestion: try getting everything working with a regular Panel first and then try and introduce the UpdatePanel - no need to over-complicate the problem at first.

Accessing GridView's ViewState

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.

Dynamic Controls and Postback

I have a Panel. Now in that panel, i'm adding controls. It is getting added and displayed.The problem is when the page is posted back.I know the controls have to be binded again in the panel. But lets say if the user has entered some value in the dynamic created text box. Its is getting lost..
I have done this before by storing the data from the controls in session.
Every time you dynamically add a control, store the current data entered into the controls in session or viewstate for example, and then rebind on postback. Not the most elegant solution but it worked. I take it this is a webforms question?
Add dynamic control click
Save current form data
On page_load load the data from viewstate into the controls
Make sure that the controls get the same ID each time (for ex. by specifying an ID explicitly)
and you should add the dynamic controls on Page.Init so they can participate in the page's life-cycle.
There's an article on 4guysfromrolla.com, Dynamic Web Controls, Postbacks, and View State

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.

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