asp.net textbox text not updating

S

sekisho

I'm dynamically adding a column of labels and a column of text boxes to
a panel on a webform, based on data returned from an SQL query, which
the user builds by selecting options from a few dropdowns and hitting
the 'Search' button.

The first column returned is a user name and is added to the panel as a
label, the second is a number and is added to the label as the text
property of a textbox, so the user can update it.

When the webform is first called and 'Search' is clicked it works fine,
but when the dropdown options are changed and 'Search' clicked again,
the labels change to the correct values but the textboxes retain their
previous values. What's even stranger is that I explored the problem by
creating labels instead of textboxes for the second column, and they
updated correctly, as did using buttons, so it seems to be an issue
only with textboxes not updating.

The code I'm using is pretty basic:

For i = 0 to dt.Rows.Count() - 1

Dim newLabel As Label = New Label
newLabel.Text = dt.Rows(i)(1)
newLabel.ID = "Label" & i
Panel1.Controls.Add(newLabel)

Dim newTextBox As TextBox = New TextBox
newTextBox.Text = dt.Rows(i)(2)
newTextBox.ID = "Textbox" & i
Panel1.Controls.Add(newTextbox)

Panel1.Controls.Add(New LiteralControl("<BR>"))

Next

Where dt is the datatable holding the results of my query, there are
three columns, an index value, a person's name and a number.

Does anyone have any idea what is going on?
 
D

David Lloyd

The following information is from the MSDN documentation and explains the
issue you are seeing. It relates to the page ViewState. Please see the
MSDN documentation on ViewState for more information.

From MSDN documentation:

If you reinsert controls with each round trip, each generation of
dynamically created controls will pick up property values from the view
state of the preceding set of controls. In many cases, you can avoid this
problem by setting the EnableViewState property of the container control to
false. In that case, no information about the dynamic controls is saved, and
there is no conflict with successive versions of the controls.


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top