创建同一类型的另一个控件,已经宣布的一个新的控制 [英] Creating a New control of the same type as another control that is already declared

查看:113
本文介绍了创建同一类型的另一个控件,已经宣布的一个新的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是自定义模板类来生成与我的中继器控制线,我希望能够在这个中继动态地从我后面的aspx页面的code指定的控件。
在code后面我添加控件像这样的列表:

I'm using a custom template class to generate lines with my repeater control, i'd like to be able to specify the controls in this repeater dynamically from the code behind my aspx page. In the code behind I've added controls to a list like this:

Dim lstControls As New List(Of Control)
lstControls.Add(New TextBox)
lstControls.Add(New Label)
lstControls.Add(New CheckBox)
lstControls.Add(New DropDownList)
lstControls.Add(New CheckBox)

然后使用该行的控件添加到我的模板

Then i use this line to add the controls to my template

rptrSummary.ItemTemplate = New myTemplate(ListItemType.Item, , lstControls)

从instantiateIn子我在做这样的事情:

From the instantiateIn sub i'm doing something like this:

Dim ph As New PlaceHolder

For i = 0 To lstControls.Count - 1
    ph.Controls.Add(lstControls(i))
Next

这不正常,并以下.databind(我的中继器)控制我指定只出现在最后一行的控制工作。我想这是因为我只声明的控件新的一次,所以我只有值一列。

This doesn't work properly and following .databind() of my repeater control the controls i specify only appear on the final row. I think this is because i've only declared the controls as NEW once, so i only have one rows worth.

tldr /结论:

tldr?/ conclusion:

我怎么能产生相同类型的从我的列表控件的新控件?是这样的:

How can i generate new controls of the same type as controls from my list? Something like:

Dim newControl as new Control = type(lstControl(0))

(这显然是行不通的)

(this obviously doesn't work)

推荐答案

我已经找到了答案,这里有一些例子,如果任何人被卡住(我也可以改变标题所以它更接近可能的搜索条件)

I've found the answer, here are some examples in case anyone else is stuck (i may also change the title so it's more similar to likely search criteria):

dim egTextbox as new textbox
dim egLabel as new label

dim newObject1 as Object = Activator.CreateInstance(egTextbox.GetType)
dim newObject2 as Object = Activator.CreateInstance(egLabel.GetType)

newObject1现在是一个文本框
newObject2现在是一个标签

newObject1 is now a textbox newObject2 is now a label

这篇关于创建同一类型的另一个控件,已经宣布的一个新的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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