Looking to delete dynamiclly created labels on postback in vb asp.net
I have a form that has an array of dynamically created labels of varying size based on a 开发者_如何学Pythonsearch 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.
You'll need to do something like this:
Me.Controls.Remove(controlName)
Got it. When I created the dynamic labels, I needed to disable the viewState for the labels.
locLabel.EnableViewState = False
精彩评论