lost data in webusercontrol asp.net after postback - asp.net

in asp.net
i create one web user control and create it online(dynamic load)
when i use it static,micro soft asp.net support it and hold all data after post back ,hold all data
but when i create by code and set id
lost data after any post back, reload data and set grids and drop down list is needed.
drop down list lost data and selected index.
how i can use asp.net dynamic web user control as static mode
i need hold all data
i use update panel and create my controls into a panel that placed in update panel
thanks

Related

(ASP Web Forms) Dynamically add controls while keeping info after postback?

Let's say I have a bunch of controls that I want to "repeat" on the page. The user can click "Add" to add another set of controls.
I still want the input to be kept (that the user has put in) after the postback that will be triggered when the user clicks "Add". 
What is the best way to do this? I would like to use a repeater but then you have to (as far as I know) store the information in ViewState and then re-populate all the controls with the old data.
I am using ASP Web Forms.
Se picture for example: https://i.stack.imgur.com/rpmnG.png

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.

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.

How to create and edit gridview at runtime along with adding rows?

I have a aspx page consisting of a calendar button and a textbox.When we click calendar button then a calendar appears and the date which i select on calendar appears in textbox. Now what i want is to load the data from database according to the date selected and allow the user to edit it.And also i want to enable user to add rows at runtime. Do i need to create grid view at runtime?? Can somebody point me in a direction??
You don't need to create the gridview at runtime. You can define the gridview in the markup without having any data initially. You can set the calendar to autopostback and on the server side, capture the new date selected, grab the data based on that date and bind it to your gridview.
As far as editing the data, Gridviews already provide support for this and the amount of code that you need to write largely depends on how the datasource for the gridview is set up. If you use a SQLdatasource with the appropriate settings you practically don't have to write a single line of code besides the markup (Google: Gridview SQLdatasurce editing ASP.net).

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

Resources