Groups | Blog | Home
all groups > asp.net datagrid control > december 2005 >

asp.net datagrid control : Why doesn't TextBox control get updated


Tuomo
12/30/2005 3:33:40 PM
Hi!

I have a form (System.Web.UI.Page) that contains a DataGrid control and a
DataTable that contains values for the data grid and that is data bound to
the DataGrid. When I switch the grid to edit mode (e.g. if I set
DataGrid.EditItemIndex = 0) the values in the data table are displayed
correctly in TextBox controls in the DataGrid. Then, if I cause a postback
(e.g. pressing a button) and I set a new value into the data table into a
certain data field (e.g. in the Page_Load method) the TextBox contains the
old value although the data table field where the text box in data bound
contains a new value.

Why doesn't the TextBox control get updated?

I have tested that other controls such as Labels, show the current value of
the data table field but I don't understand why TextBox doesn't get a new
value.

Thank you.

BR, Tuomo

Daniel
12/30/2005 6:29:58 PM
Make sure in your page load that you are updating or re-popuating the
datagrid again.

so in your page load

if(!(Page.IsPostBack))
{
//everything in here fires only the first time your page is loaded
}
else
{
//reload your data grid here ad everytime you post back it will be
forced to update (assuming your changes
//were saved to your datasource)
}

[quoted text, click to view]

Tuomo
1/2/2006 8:57:14 AM
Thank you Daniel.

I bind the data again to the data grid after I have set a new value to the
data table field. I do it in this way:

DgrdValues.DataSource = dtTest
DgrdValues.DataBind()

I have also ItemDataBound handler for the data grid but I don't handle the
text box column in any way in the handler.

The thing that I don't understand is that all other controls such as labels
and hyperlinks show their new values but text boxes don't. Could this be a
ViewState problem or even a bug in the text box or data grid control? Or
could there be some kind of refresh function for the data grid of text box..

BR, Tuomo



[quoted text, click to view]

Tuomo
1/2/2006 1:02:56 PM
I found a little bit curious solution to the problem. I create a new text
box control in the ItemDataBound method and at the same I generate a new ID
for the control. That causes the text box updating its value.

I checked that this is a problem only when the text box is used in a data
grid. The text box updates normally its value when used without a data grid.
It seems that this could be some kind of view state problem, I think.

Do you have any ideaos or better solutions according to this issue? Could
someone give an explanation of this interesting logic? This is getting
interesting..

BR,
Tuomo

[quoted text, click to view]

AddThis Social Bookmark Button