title: ASP GridView get row values on button click
body: <p>What I'm doing - reset user password on imagebutton click.</p>
  
  <p>Done so far - added GridViewCommandEventHandler - it's firing correctly. Using code from <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventhandler.aspx" rel="nofollow">MSDN</a>. I'm getting an empty string ("") for my e.CommandArgument, and it's throwing an error when running (can't parse "" to int).</p>
  
  <p>I can see in the debugger there is a 'rowIndex' property being stored (correctly for my click) elsewhere in e, can I access this? I would think MSDN's code would work - is there something else I've done to make this error occur or another way to fix it? Thanks.</p>
  
  <pre><code>void resetpassword(Object sender, GridViewCommandEventArgs e)
  {
      // If multiple ButtonField columns are used, use the
      // CommandName property to determine which button was clicked.
      if (e.CommandName == "resetpass")
      {
          // Convert the row index stored in the CommandArgument
          // property to an Integer.
          int index = Convert.ToInt32(e.CommandArgument);
  
          // Retrieve the row that contains the button clicked
          // by the user from the Rows collection. Use the
          // CommandSource property to access the GridView control.
          GridView GridView1 = (GridView)e.CommandSource;
          GridViewRow row = GridView1.Rows[index];
  
          String usrname = row.FindControl("username").ToString();
  </code></pre>
  
  <p>aspx page code:</p>
  
  <pre><code>&lt;asp:TemplateField HeaderText="Reset Password"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:ImageButton ID="ibtnReset" runat="server" CausesValidation="false" 
                          CommandName="resetpass" ImageUrl="~/Images/glyphicons_044_keys.png" Text="Button" /&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;HeaderStyle Width="70px" /&gt;
                  &lt;ItemStyle HorizontalAlign="Center" /&gt;
              &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>Event add code:</p>
  
  <pre><code> protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
      {
          GridView1.RowCommand += new GridViewCommandEventHandler(this.resetpassword);
      }
  </code></pre>