0

.NET v3.5

I have what seems to be some simple code that I've gotten to work before:

TableCell cell = new TableCell();
CheckBox c = new CheckBox();
c.AutoPostBack = true;
c.ID = string.Format( "cb_{0}", row.RowIndex ); // "row" is a GridViewRow
cell.Controls.Add( c );
row.Cells.AddAt( 0, cell );

The problem is the checkboxes aren't persisted between postbacks. But if I simply call the method on every postback, it prepends a new blank column to the front of the grid every time which pushes the data in the rows to the right for every postback. It's weird; each row will have the same number of cells and not change. It's just that the data gets "shifted" to the right. So data that was in cell[1] will appear in cell[2] after a postback. Then in cell[3] after the next postback. Etc.

What's up with this? Please remember this data is unbounded. The grid itself is not templatized but is being created on the fly from code. I simply need a way to check rows selected by the user.

IAmAN00B
  • 1,913
  • 6
  • 27
  • 38
  • why not just add an `` in the markup? – Graham Clark Aug 03 '11 at 15:36
  • 99% of the time, it is not worth the headache of adding controls dynamically. Add some markup and toggle the `Visible` property. Or if you really want to take over responsibility for managing what controls get put on the page, read this: http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx – Graham Clark Aug 03 '11 at 15:42
  • I'm creating the grid's columns on the fly and binding the data in code. It effectively ignores any markup after that. So doesn't work. The entire grid is being put together dynamically in code with applicable data. So the headache is there regardless I'm afraid. The thing is I'm rebuilding it every time. So why would the data be shifting to the right??? I'm calling the above snip on RowDataBound. Again, this has worked in the past. I don't know why the Controls collections always come back as 0 for that first cell. – IAmAN00B Aug 03 '11 at 15:54
  • have you perhaps got similar code in your page init/load and also in an event handler? – Graham Clark Aug 03 '11 at 15:59
  • I've fiddled with it quite a bit. The main issue is the checkboxes are not saying there on the postback. I don't have to rebuild the grid on postback if I don't want unless the data has changed. But as soon as it posts back and re-renders the checkboxes are all gone. SO I've messed with placing the checkboxes in both the RowDataBound and Page_Init. But no mutually exclusive combination seems to be working. – IAmAN00B Aug 03 '11 at 18:04

2 Answers2

0

Are you posting back when the user checks the box? Are you creating this grid everytime you write the page? Why not check first if the column in position 0 is already of a checkbox type?

Dean
  • 36
  • 3
  • I've done that and it's not. That's the thing; it's coming back with the cell added that I already added the last time, but there's no checkbox. The Controls collection is empty on the cell. – IAmAN00B Aug 03 '11 at 15:37
0

I think you need to put the code that add a new column to the grid on the page init.

Samir Adel
  • 2,691
  • 2
  • 15
  • 16
  • That code snip above is actually in a method being called from Page_Init as well as RowDataBound of the GridView. – IAmAN00B Aug 03 '11 at 15:55