Link to home
Start Free TrialLog in
Avatar of Paulconsulting
Paulconsulting

asked on

Check if a dynamically created div already exists

Hello experts,

I'm creating a .NET web app that dynamically creates several <div>s and sets their contents from VB code behind.

The id and the innerHTML of each <div> is assigned as .NET cycles through a SQL datareader. Sometimes the datareader will return results I would like to append to a <div> that have already been created.

The code below will create all the <div>s but doesn't append text to the existing ones. I can't figure out where I'm going wrong.

so far I have this:

        While ObjDR.Read
            intCountyID = publicfunctions.ConvertdbnullsInt(ObjDR("CountyID"))
            Dim myDiv As HtmlGenericControl = FindControl("div" & intCountyID)

            If IsNothing(myDiv) Then 'create a new div
                Dim newDiv As New System.Web.UI.HtmlControls.HtmlGenericControl("DIV")
                newDiv.ID = "div" & intCountyID
                'Build the HTML as a string
                strDivHTML = ""
                strDivHTML = "publicfunctions.Convertdbnulls(ObjDR("County")) "
                strDivHTML &= "some data"
                'Set the inner HTML
                newDiv.InnerHtml = strDivHTML
                'Add it to the page
                CountyDetails.Controls.Add(newDiv)
                'Set attributes
                newDiv.Attributes.Add("class", "CountyDetail radius")
                newDiv.Attributes.Add("runat", "server")

            Else 'Append to the existing div
                strDivHTML = myDiv.InnerHtml
                strDivHTML &= "<br />Extra stuff"
                myDiv.InnerHtml = strDivHTML
            End If
        End While
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paulconsulting
Paulconsulting

ASKER

Thanks guys!
I worked around the issue by getting my data ordered by the controls I was creating and then checking if the "id" was the same as the last run through the data reader before I closed out the <div> and added it to the page.

I'm hoping that robert_schutt's answer:
     Dim myDiv As HtmlGenericControl = CountyDetails.FindControl("div" & intCountyID)
is the best one. Although I'm not going to go back and redo it to confirm. ;)

I'll be doing another similar project soon so I hope I can use this instead of my messy (but functional) work around!

Thanks!