开发者

How do I add an unbound checkbox column to my ASP.NET GridView?

开发者 https://www.devze.com 2023-03-25 11:20 出处:网络
.NET v3.5 I have what seems to be some simple code that I\'ve gotten to work before: TableCell cell = new TableCell();

.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 开发者_JS百科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.


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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号