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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically Assign IDs to Controls

Status
Not open for further replies.

dcusick

Technical User
Aug 9, 2000
271
US
Okay, I've searched EVERYWHERE for this, and I am still confused. Saw a few posts here about it, but again, still confused...

I am creating a dynamic HTML table, from a database table. In the database table, I have cell properties for the HTML Table (Rowspan, colspan, color, etc). I need to be able to fill in these cells with the information I need. I had this running in ASP, but .NET is quite different...

I've decided to create the table, and use placeholders in each cell. Then in the Page_Load event, populate those placeholders with the necessary info. Now, in order to keep the ID's straight, I need to dynamically assign ID's when I create the PlaceHolder. I am doing it by this..

Code:
	dim plcHold as PlaceHolder
	plcHold = new PlaceHolder()
	plcHold.ID = "MainHold" & myReader("templateSpaceVar")
	Page.controls.add(plcHold)
myReader is a datareader and has the Unique ID I am assigning to the Placeholder. Then in my code, I am trying to place a list box in each place holder, like so

Code:
	Page.findcontrol("MainHold"&dr("templateSpaceVar")).controls.add(selBox)
selBox is already defined as a DropDownList.

However, the FindControl returns Null, and doesn't find the PlaceHolders I dynamically created. What am doing wrong? Thanks in advance...
 
Hi, The problem is that you are doing page.findcontrol but the page does not exists yet. if you are trying to create a page dynamically you have to write the whole mogilla then send it to the page. I wrote a dropdown box a long time ago that addresses this.

Here is the DReader portion of the code hope it helps a bit

Code:
 SqlConnection1.Open()
                dreader = SqlCommand1.ExecuteReader(CommandBehavior.CloseConnection)
                Dim myval As String = Me.SelectedValue
                While dreader.Read
                    Dim mytext As String
                    If Me.IsAbbreviation And dreader.FieldCount > 2 Then
                        If Not dreader(2) Is DBNull.Value Then
                            mytext = dreader(2)
                        Else
                            mytext = dreader(1)
                        End If

                    Else
                        mytext = dreader(1)
                    End If

                    If Me.IsReadOnly Then
                        If Not myval Is Nothing Then
                            If dreader(0) = myval Then
                                Me.Item.Text = mytext
                            End If
                        End If


                    Else

                        myWindowRow = New TableRow
                        Dim myitemtd As New TableCell

                        myitemtd.Attributes.Add("onMouseOver", "DropDownHover(this)")

                        If Not myval Is Nothing Then
                            If dreader(0) = myval Then
                                myitemtd.Attributes.Add("class", "Clicked")
                                Me.Item.Text = mytext

                            Else
                                myitemtd.Attributes.Add("class", "Dropdown")
                            End If

                        End If


                        If Me.CausesPostBack Then

                            If (Me.Visible And Not Me.Page.IsClientScriptBlockRegistered("CESITabAutopostback")) Then
                                Me.Page.RegisterClientScriptBlock("CESITabAutopostback", "<script>function CESIdoPostBackOnTab(obj) {if(event.keyCode == 9){if (obj.value != obj.defaultValue) document.forms[0].submit();}return true;}</script>")
                            End If

                            myitemtd.Attributes.Add("onClick", "DropDownGo(this,'" & dreader(0).ToString & "','" & Me.ClientID & "');var theform=document.Form1; theform.submit();")

                        Else

                            myitemtd.Attributes.Add("onClick", "DropDownGo(this,'" & dreader(0).ToString & "','" & Me.ClientID & "')")
                        End If

                        myitemtd.Attributes.Add("title", mytext)
                        myitemtd.Attributes.Add("tag", dreader(0).ToString)
                        myitemtd.Text = mytext


                        myWindowRow.Controls.Add(myitemtd)
                        innermosttable.Rows.Add(myWindowRow)
                        _itemcount += 1

                    End If
                End While
 
dcusick,

What you're trying to do, while technically possible w/ ASP.NET, sounds very ASP'esque.

Have you looked into DataBound controls at all? Perhaps a DataGrid or a DataList would work for you better in this situation.

When you assign ID's, they very often get changed by the Framework (part of the INamingContainer interface... a necessary but sometimes annoying bit of the webforms world), so assigning id's, and then pulling those controls back out, dynamically writing out a table (much like you would have in ASP) is very code intensive and ignores much of the power of ASP.NET.

Perhaps if you drew us a picture... told us your story... maybe someone could help you build a better mousetrap.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks for the code Bassguy, I'm gonna look into that..

And Link... As for what I'm doing...

I'm creating a document-template generator for my users. Basically, I don't want to be bothered everytime one of the documents in my webapp changes. I am going to give them a bunch of outlines, and let them deal with creating the documents. For instance..

Code:
 _______________________________
|      A          |     B       |
|_________________|_____________| 
|              C                |
|_______________________________|
|   D   |      E       |   F    |
|_______|______________|________|

That would be my outline. And the users would have a drop down box in each box, with an option, to make it a free-form text, a variable from the database, a date stamp, an image, etc...

As you could see, their could be a different number of columns in each row, which I'm handling in the database.

I would want to name those drop down boxes ddA, ddB, ddC, etc... That way when I post my form, I'll know what each box should be assigned... I know this is ASP'esque, but I can't think of any other way around it. As far as I know, datagrids/lists won't work in my situation here.

I hope I'm clear in what I'm looking for. Thanks for the help.
 
Oh yeah, I forgot to mention that Each outline is different. So there are no set rows/columns for each. I need this to be completely dynamic...
 
Indeed, it does sound as if databound controls can't help you in this situation.

What if you place a breakpoint on the line that comes after:

Page.Controls.Add(plcHold)

And then add a watch:

Page.FindControl("MainHold" & myReader("templateSpaceVar"))

Can it find it immediately after you add the control? Are you still in the same class?

What else is occuring between the time you add the control, and when you are then trying to find it.

You can add a watch of Page.Controls, and literally drill down through all your controls to try and pinpoint what you're looking for. This can be a slow and painful process, but is effective.

Something else to keep in mind is that if you can narrow the scope on the object that you're calling .FindControl on, your code will run much faster. Page.FindControl can potentially be an extremely inefficient use of your server resources.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Okay, I figured out a way to get this working... I am actually just putting on PlaceHolder on my form, within <td> tags. Then in my Code-Behind, I am running through the database, getting the rows and columns, and creating the new Table on the fly. I am using Placeholder.Controls.Add(New Literal Control("<td>")) etc. to build the table. Within these, I can add dynamically created DropDownLists with my own personal Naming Conventions... Thank you everyone for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top