Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Odd behaviour when dynamically placing controls

Status
Not open for further replies.

danabnormal

Technical User
May 10, 2008
4
GB
So, thanks to your help I managed to find my dynamically created controls for placing even more gumpf inside them.

My app presents various options within a listbox, and the user chooses which options they would like to view. The idea is then that a suitable table is generated, and these options are then placed into each cell for viewing. So....

Code:
Function drawTable(ByVal numCharts)
        Dim tbl2 As New Table
        tbl2.ID = "PortletDataTable"
        Select Case numCharts
            Case 1
                Dim tr2 As New TableRow
                Dim tc2 As New TableCell
                Dim myPlc As New PlaceHolder
                Dim myDiv As New HtmlGenericControl("div")
                myDiv.ID = "dashPlace1"
                tc2.Controls.Add(myDiv)
                tr2.Cells.Add(tc2)
                tbl2.Rows.Add(tr2)
            Case Else
                For a = 1 To numCharts
                    Dim tr2 As New TableRow
                    Dim tc3 As New TableCell
                    Dim tc2 As New TableCell
                    Dim myPlc As New PlaceHolder
                    Dim myDiv As New HtmlGenericControl("div")
                    Dim myDiv2 As New HtmlGenericControl("div")
                    tc2.Width = Unit.Percentage(33)
                    tc3.Width = Unit.Percentage(33)
                    myDiv.ID = "dashPlace" & a
                    myDiv2.ID = "dashPlace" & a + 1
                    tc2.Controls.Add(myDiv)
                    tc3.Controls.Add(myDiv2)
                    tr2.Cells.Add(tc2)
                    tr2.Cells.Add(tc3)
                    tbl2.Rows.Add(tr2)
                    a = a + 1
                Next
        End Select
        drawTable = tbl2
    End Function

This is called by my form handler and is passed the number of reports that have been requested. This works cool, and generates the following HTML...

Code:
     <table id="PortletDataTable" border="0">
	<tr>
		<td style="width:33%;"><div id="dashPlace1"></div></td><td style="width:33%;"><div id="dashPlace2"></div></td>
	</tr><tr>
		<td style="width:33%;"><div id="dashPlace3"></div></td><td style="width:33%;"><div id="dashPlace4"></div></td>
	</tr>
     </table>

So far so groovy.

The form handler then goes through to locate each dashPlace control to place the data inside it. The following code locates them and is msgboxing the control ID name as proof that it finds the controls....

Code:
                Dim myCtl As Control
                Dim tblRow As TableRow
                Dim tblCell As TableCell
                For Each tblRow In tbl.Rows
                    Dim ctl As Control
                    For Each tblCell In tblRow.Cells
                        For Each ctl In tblCell.Controls
                            If ctl.ID <> "" Then
                                If Left(ctl.ID, Len(ctl.ID) - 1) = "dashPlace" Then
                                    MsgBox(ctl.ID)
                                    'ctl.Controls.Add(myPart)
                                End If
                            End If
                        Next
                    Next
                Next

I then get a series of MsgBoxes listing each name. Excellent! However, if I uncomment the ctl.Controls.Add(myPart) line, the HTML resulting HTML is...

Code:
          <table id="PortletDataTable" border="0">
	<tr>
		<td style="width:33%;"><div id="dashPlace1"></div></td><td style="width:33%;"><div id="dashPlace2"></div></td>
	</tr><tr>
		<td style="width:33%;"><div id="dashPlace3"></div></td><td style="width:33%;"><div id="dashPlace4">
MyControl1Source
MyControl2Source
MyControl3Source
MyControl4Source
</div></td>
	</tr>
</table>

....which just confuses me. The idea is that the dashPlace controls are used as a placemark for each control, yet it is only chucking them in the last DIV. I have also otried PlaceMark controls etc and get the same thing so its obviously something fundamental I'm getting wrong. What confuses me is that I would assume if the preceeding MsgBox command is returning the ID "dashPlace1", I would assume that I am working with a different control!

Any help, as always, most appreciated chaps!
 
And there I was thinking I covered every option. DOH!

myPart is simply a user control that is instantiated earlier on using other options provided by that user.

Essentially a new instance is created, a configuration is fed to it depending on the user selections, and the resulting control is then placed within the DIV.

I am happy by its configuration, and do not think its functionality has much of a bearing on this issue, but if it would help I can post some example code when next at that machine.
 
Is there anyway you could create the controls while you are making the table? that way you could avoid using the place holders and just put the controls in to start with: no searching for the proper place.

If the code gets too cluttered you could create a function that determines the proper type of control to put in and call it while you're making the cells. Something like:

Code:
Sub TableControl(<whatever information is needed>) AS Control

<logic goes here>

End Sub

and then when you're making the table instead of

Code:
Dim myDiv As New HtmlGenericControl("div")
myDiv.ID = "dashPlace1"
tc2.Controls.Add(myDiv)

you could use

Code:
tc2.Controls.Add(TableControl(<paramaters>))

If that's not possible, for example if the table is generated and THEN the user is prompted to choose the type of control... well my idea won't work very well.

PS: Please excuse my VB syntax, I'm a little rusty after writing exclusively in C# for the past few months.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top