0

I have a form that has an array of dynamically created labels of varying size based on a search from a database. The problem I'm having is that when the user searches for a different term, it looks like some of the labels don't get new values. Here's my code for adding the labels:

If rdr.HasRows Then
        ReDim Preserve entities(cnt)
        While rdr.Read()
            entities(cnt) = New Label()
            If getNodeType(txtSearch.Text) = "command" Then
                entities(cnt).Text = rdr("name").ToString
            Else
                entities(cnt).Text = rdr("command").ToString
            End If
            entities(cnt).ID = "entity" & cnt
            Panel1.Controls.Add(entities(cnt))
            place_label(entities(cnt), cnt)
            cnt += 1
            ReDim Preserve entities(cnt)
        End While
    End If

I've tried for loop over the controls in panel1 to dispose of any still on there in both the page_load and page_init subs, but neither had an effect. I don't know if it might have something to do with controls having the same IDs after the postback.

Any help would be greatly appreciated.

spuppett
  • 547
  • 10
  • 26
  • Since I don't have enough points to answer yet, here's my solution: when I created the dynamic labels, I needed to disable the viewState. – spuppett Jul 06 '11 at 18:48

2 Answers2

0

You'll need to do something like this:

Me.Controls.Remove(controlName)
  • where would I do this and is that different then what I tried in the page_load / page_init? – spuppett Jul 06 '11 at 16:22
  • you could probably loop through all controls on your page, in say page_load, and remove all controls that are labels. –  Jul 06 '11 at 16:24
  • Just tried it int the page_load and no change. For i = 0 To Panel1.Controls.Count - 1 Me.Controls.Remove(Panel1.Controls(i)) Next – spuppett Jul 06 '11 at 16:25
  • when doing For i = 0 To Panel1.Controls.Count - 1 check if Panel1.Controls(i) is a label. If so, see if you can say set the background color on it or something to see if once the page has fully rendered if it updated it. If so, then, I'd say you got a valid reference. Then, remove adding that background color, if label, and call Me.Controls.Remove(ctrlName). Without more code, its difficult to know what other things could affect your dynamically generated labels. But in general, when you call Panel1.Controls.Add, calling the Remove function should remove it. –  Jul 06 '11 at 16:41
  • Looks like the controls aren't in the Panel1.Controls collection on page_load. Setting a break point in the if statement never broke. As for other code, that's it as far as placing them. The only other thing I have in that sub is db connection/reader stuff. The place_label() sub just sets up some css styles for displaying them. – spuppett Jul 06 '11 at 17:35
0

Got it. When I created the dynamic labels, I needed to disable the viewState for the labels.

locLabel.EnableViewState = False
spuppett
  • 547
  • 10
  • 26