Solved

Check if a dynamically created div already exists

Posted on 2013-06-17
4
432 Views
Last Modified: 2013-06-19
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
0
Comment
Question by:Paulconsulting
4 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 167 total points
ID: 39256107
I tried a simplified version of your code (fixed numbers instead of datareader) and it works fine. You might need to do .FindControl() like this:
Dim myDiv As HtmlGenericControl = CountyDetails.FindControl("div" & intCountyID)

Open in new window

Because in your situation it only exists in there. But for me (with a very simple aspx as well) it works either way.

EDIT: if that doesn't work, only some generic advice: put a breakpoint of the FindControl and see what happens.
0
 
LVL 23

Assisted Solution

by:Roopesh Reddy
Roopesh Reddy earned 167 total points
ID: 39256847
Hi,

One more quirk, Controls created dynamically, will be lost on Postback. You may have to re create on every post back!

http://stackoverflow.com/questions/4216329/asp-net-dynamically-created-controls-and-postback

Hope it helps u...
0
 
LVL 15

Assisted Solution

by:jorge_toriz
jorge_toriz earned 166 total points
ID: 39257930
If I need to create controls at runtime, I do it in the CreateChildControls procedure.
0
 

Author Closing Comment

by:Paulconsulting
ID: 39259643
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!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now