Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check if a dynamically created div already exists

Posted on 2013-06-17
4
Medium Priority
?
542 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

596 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