Solved

Check if a dynamically created div already exists

Posted on 2013-06-17
4
452 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 28
asp.net web application 3 31
VB.NET String Settings and Temp Folder Question 3 55
Finding Events logs for IIS website that restarts 2 14
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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…

830 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