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!

Struggling to locate programatically added control

Status
Not open for further replies.

danabnormal

Technical User
May 10, 2008
4
GB
Think I'm about to put a fist thru my screen... Save my laptop!

I think this is stupidly simple.

I have a form where the user selects a list of reports that they want to show. When the form is submitted, I am counting the number of selected reports in order to then generate a table that contains the correct number of cells and then puts that table into a placeholder..

Function drawTable(ByVal numCharts)

Dim tbl2 As New Table
tbl2.ID = "DataTableInnit"
Dim tr1 As New TableRow
Dim tc1 As New TableCell
Dim myPlc As New PlaceHolder
Dim thead As TableHeaderRow = New TableHeaderRow
Dim thc1 As TableHeaderCell = New TableHeaderCell
thc1.Text = "here"
thc1.ID = "thc1"
thead.ID = "thead1"
tr1.ID = "tr1"
thead.Controls.Add(thc1)
tbl2.Controls.Add(thead)
'Dim myTable, myRows
'myTable = "<table width=""100%"" cellpadding=""10"" id=""tblReports"" runat=server>"

Select Case numCharts
Case 1
'myRows = "<tr id = ""row1""><td width=""100%""><asp:placeHolder runat=server ID=""dashPlace1"" /></td></tr>"
myPlc.ID = "dashPlace1"
tc1.Controls.Add(myPlc)
tr1.Controls.Add(tc1)
tbl2.Controls.Add(tr1)
Case Else
For a = 1 To numCharts
'myRows = "<tr><td width=""50%""><asp:placeHolder runat=server ID=""dashPlace""" & a & """ /></td><td width=""50%""><asp:placeHolder runat=server ID=""dashPlace""" & a + 1 & """ /></td></tr>"
'a = a + 1
Next
End Select
'myTable = myTable & myRows & "</table>"
drawTable = tbl2
'drawTable = myTable & "TABLEISHERE"
End Function

If I run through the process on the site and check the resulting source, I see dashPlace1 in the source so I know that the table is generated.

Now.

For the life of me I cannot locate the dashPlace1 control in order to place an object within it. I can locate the table, and within that the row, but I cannot locate the dashPlace1 PlaceHolder object within it.

'Dim myCon As New Control
'Dim myctl As New Control
'For Each myCon In tbl.Controls
' MsgBox("tbl:" & myCon.ID)
' Next
'myctl = tbl.FindControl("thead1")
'MsgBox(myctl.HasControls)
'For Each myCon In myctl.Controls
' MsgBox(myCon.ID)
'Next
'Next

Any tips??
(Oh, and apologies if the above code is messymessymessy...)
 
I seem to have this problem a lot with asp.net as well. In fact I am dealing with one right now that I can't seem to solve, and I like you, want to put my fist through the screen. What is aggravating, is the fact that this so often comes up, yet there does not to be one consistent way to solve these controls that have "gone missing" as it were. Have you tried the "parent" property? Perhaps something like this would help you troubeshoot - (Did not test):


Code:
'ok, so we know that it finds "thead1", right?  so then:
Dim myControl1 As Control = FindControl("thead1")
If (Not myControl1 Is Nothing)
   ' Get control's parent.
   Dim myMissingPlaceholder As Control = myControl1.Parent
   Response.Write("Parent of thead1 which theoretically should be the placeholder is : " & myMissingPlaceholder.ID)
Else
   Response.Write("Control not found.....")
End If
End Sub
 
You need to look in the ROWS of the table for your Placeholder control
 
tperri,

I tried that. no joy. it's like the third level isn't even there at all. Any other suggestions? Like most of my issues, this must be something simple and obvious that my lack of experience with .NET components makes me blind to.
 
WHOOPS! my last comment was supposed to go to this thread:
thread855-1472432

SORRY!
 
Cheers for your responses guys, managed to fix it with the code below...

Dim tbl As Table = drawTable(2)
dashTable.Controls.Add(tbl)
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
ctl.Controls.Add(myPart)
End If
End If
Next
Next

Seems very clunky though, does this look right or is there a more shorthand way of doing it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top