ASP GridView uzyskać wartości wiersza na kliknięcie przycisku
Co robię - zresetować hasło użytkownika na kliknięcie przycisku Imagebutton.
Zrobione do tej pory - dodał GridViewCommandEventHandler - uruchamia się poprawnie. Używanie kodu zMSDN. Dostaję pusty ciąg ("") dla mojego e.CommandArgument, a to powoduje błąd podczas uruchamiania (nie można przeanalizować "" do int).
Widzę w debuggerze, że właściwość 'rowIndex' jest przechowywana (poprawnie dla mojego kliknięcia) gdzie indziej w e, czy mogę uzyskać do niej dostęp? Myślę, że kod MSDN zadziała - czy jest coś innego, co zrobiłem, aby ten błąd wystąpił lub inny sposób, aby to naprawić? Dzięki.
<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>
kod strony aspx:
<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> </code>
Kod dodawania wydarzenia:
<code> protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { GridView1.RowCommand += new GridViewCommandEventHandler(this.resetpassword); } </code>