动态将usercontrol添加到内容页面并访问属性 [英] Add the usercontrol to content page dynamically and access property

查看:80
本文介绍了动态将usercontrol添加到内容页面并访问属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过这个问题,但没有成功.

我有一个内容页面(default.aspx),我有一个用户控件(dashboard.ascx). Dashboard.ascx具有一个名为(EMPID)的公共属性.

我想做什么...

1)在我的default.aspx页面上,我有一个网格视图(其中包含链接按钮列(EmployeeID)

2)当我点击链接按钮时,点击事件后面的代码唤醒我正在工作.

3)在单击事件中,一个值是从链接按钮传递的(该值是EmployeeID,我想传递给我的用户控件属性(EMPID)

4)然后我要在我的DIV(EMP_div)中显示用户控件

到目前为止,我的代码:

加载我的用户控件

I have asked this question before but i didn''t have any success .

I have a content page(default.aspx) and i have a User control(dashboard.ascx). Dashboard.ascx have a public property called (EMPID).

What i want to do ...

1) on my default.aspx page i have a grid view ( which contain link button column (EmployeeID)

2) When i click on link Button on code behind click event awake which i working .

3) On click event a value is pass from link button (the value is EmployeeID which I want to pass to my user Control property (EMPID)

4) Then i want User Control display in my DIV (EMP_div)

So far my code :

TO LOAD MY USER CONTROL

Dim _usercontrol As UserControl = LoadControl("~\Usercontrols\dashboard_Employer.ascx")

EmpDashboard.Controls.Add(_usercontrol)




问题:::

如何访问USER CONTROL的属性. (EMPID)




Problem :::

How to access property of USER CONTROL . (EMPID)

推荐答案

假设
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.


这篇关于动态将usercontrol添加到内容页面并访问属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆