Wednesday, March 21, 2012

Question about using AJAX to load user controls

So I'm making a pretty simple form here, and I need some help. I'll make the content of the problem abstract and just focus on what I'm trying to understand about the language.

I have two different user controls, one is form A and the other is form B. Based on a button that the user clicks (button A or button B), I want to load form A or form B. Only one form can be visible at a time.

I've created the user controls in two separate .ascx files. On the main page, I have an update panel where I placed user control A and user control B.

Currently, the page load handler in the main page sets visibility as form A visibility = false and form B visibility = true. The click handler for button A and button B will set the appropriate visibility options so that you will see form A if you click button A, and form B if you click button B. This seems to be an amateur solution to what I'm trying to do.

Here's my question that I think will lead to me a better solution:

What if I had a much larger collection of user controls? It would become unwieldly to place them all in the updatepanel, so I assume there is a way to not load them all at once. Is there way to place a reference to a 'collection' of user controls in the update panel, and then load them individually based on a user input (such as a drop down list)? Let's say there are hundreds of user controls, and I want to load only a certain one based on user input. What is the best way to do this?

Let me know if I need to clarify my question, and thank you for any answers or explanations!

You can add a PlaceHolder control inside the UpdatePanel. Then you can load the user control using Page.LoadControl method passng the appropriate path for the user control's file. Finally add the loaded user control to the PlaceHolder as a child.


Thanks for the reply! I need some clarification though.

How do I assign a place holder control inside the update panel?

I believe I understand the use of the Page.LoadControl method. Depending on the form they select, you would pass the path of that control as a string to the method.

My last question is how do I add a loaded user control to the place holder as a child?

I'm sorry if these are basic questions, but I'm new to AJAX and web programming in general, so I'm figuring this out as I go.

Thanks again for any responses.


After reading more about what I'm trying to do, I think I know how to do all but the setting of the control as a child within the update panel. Could you elaborate on that? I can't find the correct information via search engine.

I did some more research, and I figured out everything I needed to know. Thank you for pointing me in the right direction! Here is a snippet that captures what I was trying to do.

' Declare a variable named controlToLoad of type Control.
Dim ucPlaceHolderAs Control
' Call the page's LoadControl method passing it the virtual path to
' an ASP.NET UserControl (.ascx file), assigning
' the resulting control object to the ucPlaceHolder variable.
ucPlaceHolder =Me.LoadControl(sControlPath)
' Add the control object to the update panel control container
Me.UpdatePanel.ContentTemplateContainer.Controls.Add(ucPlaceHolder)


I have been tried using

upMainContent.ContentTemplateContainer.Controls.Add(Page.LoadControl(ContentControlPath));

to dynamically load a control into an update panel however, If I do anything in that control which requires a postback, the control just disappears.

Is anyone else experiencing this problem or know how to fix it?


hello.

see if this helps:

http://msmvps.com/blogs/luisabreu/archive/2007/02/15/adding-controls-to-an-updatepanel-through-code.aspx

That C# is a little confusing for me. Could someone give a VB version?

I'm actually having the same problem as one of the above posters. I can't get viewstate to persist across postbacks.

Basically, I have 3 controls. Two of the controls are composits of drop down lists, text fields, and attach, submit, and cancel buttons.

The third control is a menu that will dynamically create one of the two forms based on a button click on the menu.

I register the menu on a aspx page, and then can bring up either form based on a button click in the browser.

I need a way to load the menu again with all of the same field data entered after the user hits the attach button. I would like to keep all the code that dynamically creates forms in the menu control if possible.

No comments:

Post a Comment