2012-02-27 67 views
1

我有一个用户控件,它返回一个数据表,在某些情况下需要循环显示一个数据表。动态添加多个用户控件vb.net

我可以通过在页面中放置一个固定的placeholder来动态地添加一个这样的实例。

我现在正在努力研究如何添加多个,因为我不知道可能需要多少个我不想对占位符进行硬编码。

我试过以下,但我刚开始一个实例,想必首先是由第二

覆盖我的HTML

<div id="showHere" runt="server"/> 

VB

Dim thisPh As New PlaceHolder 
thisPh.Controls.Add(showTable) 
showHere.Controls.Add(thisPh) 

Dim anotherPh As New PlaceHolder 
anotherPh .Controls.Add(showTable) 
showHere.Controls.Add(anotherPh) 

如何我可以在showHere div中添加重复表吗?

回答

1

我劝产生不同的ID对每个表。例如,

Dim i As Integer 
i = 0 
For each tbl in ShowTables 
    tbl.ID = "MyTab" + i.ToString() 
    i = i + 1 
    showHere.Controls.Add(tbl) 
    showHere.Controls.Add(New LiteralControl("<br />")) 
Next 

在另一方面,它会更有意义有一个用户/自定义控件生成HTML的一个表,然后中继器中嵌套的用户/自定义控件(或类似的控制,如ListView等)。

+0

这样做!不知道为什么...无论如何,它现在工作!谢谢 – 2012-02-27 12:26:09

1

你试图简单地说,这样的:

For each tbl in ShowTables   
    showHere.Controls.Add(tbl) 
    showHere.Controls.Add(New LiteralControl("<br />")) 
Next 
+0

这看起来可爱,但它仍然只显示一个表 – 2012-02-27 11:54:43

+0

你确定你没有设置一些CSS脚本,修复表中的位置(位置:绝对...),或DIV大小? – 2012-02-27 12:06:50

+0

绝对。在呈现的HTML中没有任何内容显示,只有一个isntance – 2012-02-27 12:12:50

0

在我自己对这个问题大惊小怪之后,我偶然发现了下面的解决方案。

On button click() 

    LocationDiv.Visible = True 
    Dim existingItems As New List(Of Object) 

    If Not Session("existingItems") Is Nothing Then 
     existingItems = CType(Session("existingItems"), List(Of Object)) 

     For Each item As Object In existingItems 
      LocationDiv.Controls.Add(item) 
     Next 
     existingItems.Clear() 
    End If 

    LocationDiv.Controls.Add(New LiteralControl("<b>" & Text & "</b>")) 

    For Each item As Object In LocationDiv.Controls 
     existingItems.Add(item) 
    Next 

    Session.Add("existingItems", existingItems)