I am trying to add user controls once the page is loaded.In the page depending on the selection (dropdown or checkbox) the respective user control will be added.I know that if we need to add user controls I need to add them in the Page_InIt method.but I will know which user control to add only when the page is completely loaded and the user has made a selection.
The page has Update panel so there will be many Async postback's so I also need to look at how to persist the user-control once it is added to the page.
Any help will be grateful.
You can get user selection using the
Request.Form["YourControlID"].
Check this link to know about getting postback data from forms
Read Post Data submitted to ASP.Net Form
Also read this post about using dynamic controls in ASP.NET. It very helpfull.
TRULY UNDERSTANDING DYNAMIC CONTROLS
Related
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
I have an ASP.NET web form on which I'm displaying a list of database items via user controls, generating the user controls dynamically - working fine.
Also on the page, I have an asp:dropdownlist filled with items that can be added to this database list. Along with this dropdown I have a button 'ADD'. My intent is that the user chooses and item, clicks add, and then the list of user controls on the form will include this new item.
I have all this working.
My issue is that the user control has a button 'DELETE', which removes the selected item from the list. That works, EXCEPT when I add a new item. Because my 'add' button event is always fired after Page_Load, even if I regenerate the list of user controls, the internal user control click events won't fire because the controls weren't created as part of Page_Load.
I need to know what I'm doing wrong, or best practices here, any advice. I tried to be precise in the description of the problem, but if I've not succeeded, let me know and I can add more details.
SIMPLE RESTATE: I need to know how to add a dynamically created user control to a page via a page button click event, and still have the user control internal click(etc) events firing.
Thanks for any help.
EDIT: Based on the feedback from the gentlemen here, and doing some further research related to their suggestions, I ended up implementing a solution based on what's presented on this page:
http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx
Here's a snippet showing how I dealt with this. This snippet resides in my PreInit event handler. Not exactly an elegant weapon for a civilized age, but sometimes a blaster is all you've got to use.
'Look to see if button for adding a new client number has been
'clicked. If so, call the sub to add the item NOW, so that it
'is able to have it's internal click events fire.
For Each control_string As String In Request.Form
Dim ctl As Control = Page.FindControl(control_string)
If (ctl IsNot Nothing) AndAlso (ctl.ID = "cmdAddClientNumber") Then
Me.AddClientNumberToList()
Exit For
End If
Next
On the button handler, you initially add the UserControl to the Page. OnPreInit (which will next be fired when the user clicks Delete on the UserControl), you should re-add the UserControl - so that it exists, and can handle the Delete button event.
You will need to devise your own state tracking mechanism to determine that you need to add the UserControl during PreInit. I generally use ViewState, as evidenced by this seemingly similar post.
Similar question:
How to create and use custom control with UpdatePanel added to the page programmatically
Dynamic control should be re-added to the control tree OnPreInit, see documentation:
PreInit - Create or re-create dynamic controls.
ASP.NET Page Life Cycle Overview
I understand the page lifecycle and how I need to add the dynamic controls on page_init if I want to take advantage of viewstate. And I already have read a lot of topics regarding the issue but it was not very useful.
I have to work with the solution where I have a set of user controls, that inherit one interface and then I add/delete them dynamically to a literal control. I can't load them at once in a page_init because later I have to delete/add them based on user actions. These controls contains a button, and its post back event handler is fired only when I generate this user control at page_init or page_load methods.
So I have to somehow reinitialise/restart lifecycle of the page after needed processing in some Button_Click, (where I update the controls list) events handlers. That is not possible, as I have found. Redirecting to the same page is not a solution, because in the case I will lost my whole viewstate.
So I am stuck, could you please help me with the solution?
It sounds like you could benefit from creating a CompositeControl.
I recently answered a similar question based on dynamically creating textboxes in which I provided a fairly detailed example.
See: Dynamically add a new text box on button click
Hope this helps.
If I'm understanding this correctly, you need to instantiate the dynamic controls to retrieve their view state, but you're relying on the view state to determine how to instantiate the dynamic controls.
You could utilize the ASP.NET Session State? Use the Session State to store the information from which you generate the dynamic controls.
When you handle the button click event, destroy and recreate all of the controls, and then store all the information you need to recreate them again in the Session State.
The next time the page instantiates, the page_init function can check if there's any pertinent info in the Session State, and use this to recreate the controls.
I have a custom server control (composite control having dynamically created dropdown boxes and textboxes). I have enabled AJAX in order to avoid page reload.The server control is used inside the ASP.NET webcontrol having few buttons which controls the visibility of the server control. Now I enter values in the dropdown box and texboxes and click on any other button. After this postback the last entered values are gone! The control is not remembering the values. Can anyone help me? How can I retain the values after post back?
Ensure that the controls are given the exact same ID after each postback. Also you might want to try initialising your dynamic controls (DropDownList and TextBox) during Page_Init if possible.
If your custom server control is create dynamically all the controls, then you need to recreate them on every post, and you need to create them before the Page_Load(), or else the asp.net cant know them to filled with the post data.
To solve this, if you can not create them before the Page_Load(), then you can fill them by your self with the posted data by using the Request.Form[YourCustomControl.UniqueID] for all controls.
You need to re-create on each postback, see This article
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