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.Convertdbn ullsInt(Ob jDR("Count yID"))
Dim myDiv As HtmlGenericControl = FindControl("div" & intCountyID)
If IsNothing(myDiv) Then 'create a new div
Dim newDiv As New System.Web.UI.HtmlControls .HtmlGener icControl( "DIV")
newDiv.ID = "div" & intCountyID
'Build the HTML as a string
strDivHTML = ""
strDivHTML = "publicfunctions.Convertdb nulls(ObjD R("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("cla ss", "CountyDetail radius")
newDiv.Attributes.Add("run at", "server")
Else 'Append to the existing div
strDivHTML = myDiv.InnerHtml
strDivHTML &= "<br />Extra stuff"
myDiv.InnerHtml = strDivHTML
End If
End While
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.Convertdbn
Dim myDiv As HtmlGenericControl = FindControl("div" & intCountyID)
If IsNothing(myDiv) Then 'create a new div
Dim newDiv As New System.Web.UI.HtmlControls
newDiv.ID = "div" & intCountyID
'Build the HTML as a string
strDivHTML = ""
strDivHTML = "publicfunctions.Convertdb
strDivHTML &= "some data"
'Set the inner HTML
newDiv.InnerHtml = strDivHTML
'Add it to the page
CountyDetails.Controls.Add
'Set attributes
newDiv.Attributes.Add("cla
newDiv.Attributes.Add("run
Else 'Append to the existing div
strDivHTML = myDiv.InnerHtml
strDivHTML &= "<br />Extra stuff"
myDiv.InnerHtml = strDivHTML
End If
End While
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(
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!