Want to implement "Web UserControl" dynamically in my page

Posted by Chikul under ASP.NET on 12/15/2009 | Views : 1814 | Status : [Member] | Replies : 5
I created one user control page "WebUserControl.ascx". which is having one button & one label.
i want to implement it dynamically in my page.
Following are the coding written on page load.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim UCDynamic As Control
UCDynamic = LoadControl("WebUserControl.ascx")
Page.Controls.Add(UCDynamic)
End Sub


But its throwing error as
"Control 'ctl02_Enter' of type 'Button' must be placed inside a form tag with runat=server".
how to do this..??

Thanks in advance...

Regards,
Chikul




Responses

Posted by: Vuyiswamb on: 12/15/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
in your markup(html) you must add your user control inside the form tags. post your html code and we will show you how you must do it.

Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Chikul, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chikul on: 12/15/2009 [Member] Bronze

Up
0
Down
I'm using the following codes in markup(html).

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="MainPage.aspx.vb" Inherits="_Default" %>

<%@ Register TagPrefix="UC" TagName="UserControl" Src="~/WebUserControl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<UC:UserControl id="UC1" runat="server">
</UC:UserControl>
<div>

</div>
</form>
</body>
</html>


The above code its working fine.

But my question is "At the runtime (Page load ) how to add "Web User control " in form .

Thank u.


Chikul, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 12/15/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
This is how its done

http://www.codeproject.com/KB/aspnet/LoadingUSerControl.aspx


Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Chikul, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chikul on: 12/15/2009 [Member] Bronze

Up
0
Down
Thank you Vuyiswamb .
We need to add the control in place holder. Now its working fine.

Chikul, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 12/15/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Thank you for posting at Dotnetfunda


Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Chikul, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response