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);
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0