且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态将usercontrol添加到内容页面并访问属性

更新时间:2023-09-20 13:10:46

假设
Public Class dashboard_Employer
   Inherits System.Web.UI.UserControl
...


然后


then

Dim _usercontrol As dashboard_Employer = LoadControl("~\Usercontrols\dashboard_Employer.ascx")
_usercontrol.EMPID = [whatever you want to put here]
EmpDashboard.Controls.Add(_usercontrol)




or

Dim _usercontrol As UserControl = LoadControl("~\Usercontrols\dashboard_Employer.ascx")
((dashboard_Employer)_usercontrol).EMPID = [whatever you want to put here]
EmpDashboard.Controls.Add(_usercontrol)



参见 [ ^ ]文章以寻求帮助.

原因是您要声明将_usercontrol变量声明为其基本类型.为了访问dashboard_Employer属性,您必须将其声明为该类型,或者将其强制转换为该类型.



See this[^] article for some help.

The reason is that you are declaring declaring the _usercontrol variable as its base type. In order to access the dashboard_Employer properties you either have to declare it as that type, or cast it as that type.