Search code examples
c#asp.netgridviewrowcommand

GridView cell not registering any controls on RowCommand event


First off, I realize most of this can also be done using ItemTemplates. If what I'm trying to do simply isn't possible, I will consider using them instead.

Here are the basics of my dilemma:

I have a GridView in the ASPX page that is loaded in the CodeBehind. Each row contains a couple of buttons that trigger the OnRowCommand event. When someone clicks the "Edit" button, I create a TextBox object and add it to the Controls collection of a particular cell.

This works fine.

The problem is, when the person clicks the "Save" button, OnRowCommand is triggered again but the cell is registering 0 items in the Controls collection. I'm pretty sure this is happening before a PostBack so I'm not sure why I can't access the TextBox control.

I checked after initially adding the TextBox and it shows 1 Control in the cell. Somewhere between loading the page with the textboxes and clicking the button, these controls have gone missing. Google wasn't much help. Any ideas?


Solution

  • You are dynamically creating textboxes so you have to re-bind your grid on each post back, give your textboxes and id (always the same) and re-attach any event handlers.