?
Solved

Check if a dynamically created div already exists

Posted on 2013-06-17
4
Medium Priority
?
507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 501 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 501 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 498 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
The viewer will learn how to count occurrences of each item in an array.
Simple Linear Regression
Suggested Courses

762 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