ASP.NET Nested User Control

Hi Experts,

I have one page and two user controls. User Control B is a nested control within Control A and Control A is sitting at Page.

I am adding one instance of Control B to Parent control A it displays fine but then I have a command button on Control A (parent control) and every time command button is clicked one instance of Control B is added to a container in Control A. But problem is only very first added instane of Control B is being displayed/rendered. Nothing else is showing. how can I achieve this.

I am trying to achieve an effect like some email web interfaces allow additions of as many "TO" addresses as possible and new rows keep appearing. Let  say that row was a user control and I want to appear one new when "Add" is clicked.

Please help i have spent two days but cudnt make it work !!!
hussainAbidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BondoCommented:
here is an example of how I've dynamically loaded multiple versions of he same control on another control.  My case it was a questionnaire system.
For Each oUQ As UserQuestion In UserQuestions
                        If IsNothing(oUQ.Response) = False Then
                            If Left(oUQ.QuestionSeq, Len(Criteria)) = Criteria Then

                                Dim oLoad As QuestionControl = DirectCast(LoadControl("ERGOQuestion.ascx"), QuestionControl)

                                oLoad.UserQuestion = oUQ
                                oLoad.BindData()
                                pnlQuestions.Controls.Add(oLoad)
                            End If
                        End If
                    Next oUQ

Open in new window

Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
(1) But problem is only  very first added instane of Control B is being displayed/rendered.  Nothing else is showing.

Because whenever the page loads it removes each and every instance of dynamic controls added to it. So you would need to keep track of those controls somehow.

(2)
How to solve mystery?

I am assuming that you have added your user controls to page as:

.aspx page --> uc1--->uc2

And you are having a panel in uc1 with a single button. uc2 is having a textbox simply. Now you are trying to add uc2 instance to panel in uc1 each time you click button in uc1. So for this follow steps:

(A) Add reference to the uc2 in uc1 page as:

<%@ Reference Control="~/UC2.ascx" %>

(B) Create an array (of UserControl type) or arraylist to track dynamically added controls. Also declare static counter to track number of controls added to array & also for referencing later.

(C) Write code for button click:

    C.1. Add uc2 to panel in uc1
    C.2. Add same instance to above created array.
   
(D) Write a function which can add our earlier created controls from array to panel again (on page load) as:

       Page_Load (Code snippet for explanation only)
       if (arrUC[0] is UserControl)
            {
                //for each usercontrol saved in our array, recreate it
                foreach (UserControl uc in arrUC)
                {
                    if (uc != null)
                        addUC(uc); // Passing uc to Function for readding controls again
                }
            }

(3) What is happening behind the scene?

We are adding controls to panel by clicking on button. Next time as page reloads on clicking button we are getting controls from array and adding them again to our page by calling function for readding controls. (Simply).

Regards,
VSS


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hussainAbidAuthor Commented:
Thanks very much for answer but here are two issues:
1) even though i have marked the control code behind a SERianlizable it still gives error that useControl is not marked as serializable.
2) controls added dynamically does not appear (only very first added one does) and seems like it is because creating a control instance at a command button level is far too late for the page life cycle to render it along. Any suggessions please?

Looking forward to hear from fellas out there :)
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Hello hussainAbid,

Did you tried suggestion as I asked you?

Regards,
VSS
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
hussainAbidAuthor Commented:
Many thanks and sorry for late response i was put on another rather urgent task :). Happy programming!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.