Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load web user control dynamic in other web user control

Status
Not open for further replies.

IlseDC

Programmer
Mar 24, 2003
49
0
0
BE
I have made two web user controls (Control_1 and Control_2) with some labels and buttons on it.
Is it possible to load Control_2 dynamic at runtime in Control_1?
Control_1 is part of the web page default.aspx.

So in the page_load of Control_1 I thought to use following code:
Control_2 myControl = new Control_2();
myPlaceHolder.Controls.Add(myControl);


But this does not work.

Any ideas?
Thanks.
 
You'll have to load the second user control using the LoadControl method

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
I tried with the LoadControl method.
I get following error message:

Control 'Control_11__ctl0_Button1' of type 'Button' must be placed inside a form tag with runat=server.

Button1 is the control on web user control Control_2.
In the page_load van web user control Control_1 I do the load of Control_2.

Page_Load of Control_1:
Control_2 myControl_2 = (Control_2) LoadControl("Control_2.ascx");
PlaceHolder1.Controls.Add(myControl_2);

Any ideas?



 
does the second control have <form runat="server"> set to it???

Known is handfull, Unknown is worldfull
 
That would suggest to me that you haven't included a form tag on your aspx page...

Here's a really simple example of how it should be done:

Page1.aspx has the following HTML in the body tag:
Code:
		<form id="Form1" method="post" runat="server">
			<uc1:WebUserControl1 id="MyUserControl" runat="server"></uc1:WebUserControl1>
		</form>

WebUserControl1 has a placeholder in it's HTML (with no form tags or any other HTML):
Code:
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
It also has code in it's page load event to load the second control:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        PlaceHolder1.Controls.Add(Me.LoadControl("WebUserControl2.ascx"))
    End Sub
and WebUserControl simply has a button on it (again with no other HTML:
Code:
<asp:Button id="Button1" Text="Button" runat="server"></asp:Button>

Now, when you run Page1.aspx, ypu should see the button from the second user control.


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
I dragged Control_1 to my web page but is was outside the <form> </form>.
I changed the location of the user control in HTML view and now it works fine.
Thanks for your advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top