views:

855

answers:

3

I have a Radgrid with 2 Textboxes and 2 DatePickers. The idea is that I have a grid with a Property name, value, valid from and until. I'm filling the first Textbox myself, the user has to fill in the value, from and until. Filling in the propertynames: (In the pageload)

foreach (String s in testProperties) {
    DataRow dr = dt.NewRow();
    dr[0] = s;
    dr[1] = "";
    dr[2] = "";
    dr[3] = "";
    dt.Rows.Add(dr);
}

When the user hit "Save" I have to read out all the data he filled in. (In the btnSave click)

foreach (GridDataItem dataItem in RadGrid1.Items) {
   String[] str = new String[3];
   str[0] = ((TextBox)dataItem["col2"].FindControl("TextBox2")).Text;
   str[1] = ((RadDatePicker)dataItem["col3"].FindControl("RadDatePicker1")).SelectedDate.ToString();
   str[2] = ((RadDatePicker)dataItem["col4"].FindControl("RadDatePicker2")).SelectedDate.ToString();
   properties.Add(((TextBox)dataItem["col1"].FindControl("TextBox1")).Text, str);
}

Now this is where I have the problem. When i read out the data all my 'str' have the value "" instead the data that the user fills in.

Question is, how comes my values in the texboxes remain ""? Or is their a better way to read out the data?

A: 

I would refactor the code as follows and then run in debug mode - when doing so, step through and make sure that the TextBoxes are really being "found" correctly (ie: they should not be null)

foreach (GridDataItem dataItem in RadGrid1.Items) {
   String[] str = new String[3];
   TextBox textBox1 = (TextBox)(dataItem["col1"].FindControl("TextBox1"));
   TextBox textBox2 = (TextBox)(dataItem["col2"].FindControl("TextBox2"));
   RadDatePicker datePicker1 = (RadDatePicker)(dataItem["col2"].FindControl("RadDatePicker1"));
   RadDatePicker datePicker2 = (RadDatePicker)(dataItem["col4"].FindControl("RadDatePicker2"));

   // stop here and check the controls from the previous 4 lines 
   // to make sure they are not null

   str[0] = textBox2.Text;
   str[1] = datePicker1.SelectedDate.ToString();
   str[2] = datePicker2.SelectedDate.ToString();
   properties.Add(textBox1.Text, str);

}

Yaakov Ellis
When i run in debug mode with your solution I find my controls not being null. The problem is that the Text of Textbox2 remains "". Yet the Text of Textbox1 (the one i fill in myself with code, see above) has his value (for example "m²").It is probably a silly mistake somewhere but i just can't see it.
Christophe
A: 

Are you using Bind() or Eval() in GridTemplateColumns?

See: GridEditableItem UpdateValues doesn't work with GridTemplateColumn?

If GridTemplateColumns are not necessary, I would suggest using GridBoundColumns and RadGrid's ItemCommand or InsertCommand/UpdateCommand events.

You can read out the data from GridDataItem or GridEditableItem using e.Item.OwnerTableView, for example:

RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
{
GridEditableItem editedItem = (GridEditableItem)e.Item;
Hashtable values = new Hashtable();
e.Item.OwnerTableView.ExtractValuesFromItem(values, editedItem);
}
mika
A: 

This error usually occurs if you are re-binding the data to the radgrid during postback and before the update event. Avoid rebinding the radgrid before any event triggering.

Raja