4

What I'm doing - reset user password on imagebutton click.

Done so far - added GridViewCommandEventHandler - it's firing correctly. Using code from MSDN. I'm getting an empty string ("") for my e.CommandArgument, and it's throwing an error when running (can't parse "" to int).

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.

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();

aspx page code:

<asp:TemplateField HeaderText="Reset Password">
                <ItemTemplate>
                    <asp:ImageButton ID="ibtnReset" runat="server" CausesValidation="false" 
                        CommandName="resetpass" ImageUrl="~/Images/glyphicons_044_keys.png" Text="Button" />
                </ItemTemplate>
                <HeaderStyle Width="70px" />
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>

Event add code:

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        GridView1.RowCommand += new GridViewCommandEventHandler(this.resetpassword);
    }
Volvox
  • 611
  • 2
  • 7
  • 19

2 Answers2

4

I think you are missing CommandArgument='<%# Container.DataItemIndex %>'

for your code.

<asp:ImageButton ID="ibtnReset" runat="server" CausesValidation="false" 
        CommandArgument='<%# Container.DataItemIndex %>'
        CommandName="resetpass" ImageUrl="~/Images/glyphicons_044_keys.png" 
Text="Button" />

Here is a question on SO ASP.NET GridView RowIndex As CommandArgument for further reading.

The ButtonField class automatically populates the CommandArgument property with the appropriate index value.

Here is the MSDN source

Community
  • 1
  • 1
Amit
  • 21,570
  • 27
  • 74
  • 94
  • @Volvox: For further reading on `ButtonField` I have provided MSDN link in my update. – Amit May 04 '12 at 18:42
4

Either pass the CommandArgument(assuming you want to pass the primary key field called PK):

 <asp:TemplateField>
    <ItemTemplate>                
      <asp:ImageButton runat="server" ID="ibtnReset"
        Text="reset password"
        CommandName="resetpass"
        CommandArgument='<%# Eval("Pk") %>'
    </ItemTemplate>
  </asp:TemplateField>

or get the reference of the GridViewRow via the NamingContainer of your ImageButton:

WebControl wc = e.CommandSource as WebControl;
GridViewRow row = wc.NamingContainer as GridViewRow;
String usrname = ((TextBox)row.FindControl("username")).Text;

You can also pass the RowIndex as CommandArgument:

CommandArgument='<%# Container.DataItemIndex %>'

The ButtonField class automatically populates the CommandArgument property with the appropriate index value. For other command buttons, you must manually set the CommandArgument property of the command button.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks for the note on the ButtonField/ImageButton - now I know why that part wasn't working! Went with the first option, and it's working perfectly. – Volvox May 04 '12 at 18:29