开发者

Dynamically adding a custom user control to a page

开发者 https://www.devze.com 2023-01-23 13:20 出处:网络
I have a usercontrol (ascx) that I want to dynamically add to a page. Neither the control nor the page has a namespace (thanks crappy vendor).

I have a usercontrol (ascx) that I want to dynamically add to a page.

Neither the control nor the page has a namespace (thanks crappy vendor).

When I use the following, it tells me it cant find the "type or namespace"

StayTunedControl = (UserControler_StayTuned)LoadControl("~/UserControler/StayTuned.ascx");           
Page.Controls.Add(StayTunedControl);
StayTunedControl.StayTunedID = Convert.ToInt32(IncludesStayTunedMeta.Value);

After开发者_如何学Python some tweaking to the namespaces, etc, I am now at a point where the 3rd line above generates the following error:

'System.Web.UI.UserControl' does not contain a definition for 'StayTunedID'

I was hoping that casting StayTunedControl as type (UserControler_StayTuned) would fix this.

HELP?


Essentially, had problems with namespaces and remnants being left in the inherits attribute of the control - which was causing the compiler failed with error code 1. message.

Referred to the following for resolution -- matching up with some of the above comments:

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


Before accessing any property of the user control you need to create an instance of the user control when loading it to the page.

Dim tc As New Page
Dim c As Control = tc .LoadControl("~/usercontrols/tasks.ascx")
Dim uc As usercontrols_tasks = DirectCast(c, usercontrols_tasks)

I first loaded my user control as a generic control. Then created a new instance of the user control and casted the generic loaded control to my uc instance.

After creating an instance of your user control its properties will be available.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号