title: Datagrid ItemCommand event not firing
body: <p>I am still a novice with asp.net, so I'm sure there are better ways to do some of this...</p>
  
  <p>I want to allow a user to enter a date, and then execute an SQL query against a database to return all records matching that date. I want to display those records, and allow the user to select one for additional processing. It seemed like the DataGrid with a column of Pushbuttons was a good choice. In fact, I've done this before, but it those cases there was no user interaction involved. The page just ran a fixed SQL query. With what I'm doing now, the data is displayed as I want, but the Pushbuttons aren't working...the ItemCommand event doesn't fire.</p>
  
  <p>I've read a lot of threads about the ItemCommand event not firing, but I still can't get this to work. My understanding is that I need to bind the DataGrid while not in a Postback, and I think my code does that. When I debug the Page_Load event, I can see that the code inside <strong>if (!IsPostBack){}</strong> is running, and the session variables have the expected data.</p>
  
  <p>On the page that hosts the DataGrid, I have a 'Find' button that executes a SQL query against a database. The query uses a date entered into the textbox that is on that page. In the 'Find' button click event, I store the results of the query (a DataTable) in a session variable, then do a Redirect to re-load the page. </p>
  
  <p>Once the page reloads, the session variables contain my expected data and the data is displayed in the DataGrid, along with the Pushbuttons. When I click any of the buttons in the DataGrid, the contents of the DataGrid disappear and the ItemCommand event does not fire.</p>
  
  <p>Here's the definition of the DataGrid (updated to include the button):</p>
  
  <pre><code>&lt;asp:Panel runat="server" ID="pnlSelect" HorizontalAlign="Left"  Visible="true" style="margin-top:10px" &gt;
     Please select a participant.
     &lt;asp:DataGrid runat="server" ID="grdItems" AutoGenerateColumns="true" BorderColor="black" BorderWidth="1" CellPadding="3" style="margin-left:2px; margin-top:6px" OnItemCommand="grdSelect_ItemCommand" &gt;
        &lt;Columns&gt;
           &lt;asp:ButtonColumn HeaderStyle-HorizontalAlign="Center" ButtonType="PushButton" Text="Select" HeaderText="Select" CommandName="Select" &gt;
              &lt;ItemStyle Width="60px" HorizontalAlign="Center"/&gt;
           &lt;/asp:ButtonColumn&gt;
        &lt;/Columns&gt;
     &lt;/asp:DataGrid&gt;
  &lt;/asp:Panel&gt;
  </code></pre>
  
  <p>Here's the Page Load code (unneeded code-behind stuff is commented out):</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
                  if (!IsPostBack)
          {
              if (Session["y"] != null)
              {
                  txtFind.Text = Session["x"].ToString();
                  DataTable table = Session["y"] as DataTable;
                  Session.Remove("x");
                  Session.Remove("y");
  
                  if (table != null)
                  {
                      /*
                      ButtonColumn buttonColumn = new ButtonColumn
                      {
                          HeaderText = "Select",
                          Text = "Select",
                          CommandName = "Select",
                          ButtonType = ButtonColumnType.PushButton,
                      };
  
                      grdItems.Columns.Add(buttonColumn);
  
                      foreach (DataColumn c in table.Columns)
                      {
                          BoundColumn column = new BoundColumn
                          {
                              HeaderText = c.Caption,
                              DataField = c.Caption,
                              ReadOnly = true
                          };
  
                          grdItems.Columns.Add(column);
                      }
                      */
  
                      grdItems.DataSource = table;
                      grdItems.DataBind();
                  }
              }
          }
  
  }
  </code></pre>
  
  <p>Here's the relevant 'Find' button event code. I can't post the details of the sql query text, but that part does work and the DataTable does contain the expected data:</p>
  
  <pre><code>DataTable table = DatabaseHelper.FindBySQL(sqlText);
  
  if (table != null &amp;&amp; table.Rows.Count &gt; 0)
  {
      Session["x"] = searchtext;
      Session["y"] = table;
      Response.Redirect(Request.RawUrl, true);
  }
  </code></pre>
  
  <p>And this the ItemCommand event. I place a breakpoint on the first line, and the debugger never hits it.</p>
  
  <pre><code>protected void grdItems_ItemCommand(object source, DataGridCommandEventArgs e)
  {
      string item = "";
  
      switch (e.CommandName.ToLower())
      {
         case "select":
            item = e.Item.Cells[1].Text.Trim();
            //todo: handle the selected data
            break;
      }
  }
  </code></pre>
  
  <p>Thanks for any thoughts.</p>
  
  <p>Don</p>