2012-05-04 30 views
4

私がやっていること - 画像ボタンクリックでユーザーパスワードをリセットします。ASP GridViewボタンのクリックで行の値を取得

これまでに完了したGridViewCommandEventHandlerが正常終了しました。 MSDNのコードを使用しています。私はe.CommandArgumentの空の文字列( "")を取得していますが、実行中にエラーをスローしています( ""をintに解析できません)。

私はデバッガに 'rowIndex'プロパティが格納されていることがわかります。私はMSDNのコードが動作すると思います - 私はこのエラーが発生するか、それを修正する別の方法を行うために何か他にありますか?ありがとう。

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ページコード:

<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> 

イベントのコードを追加します。

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     GridView1.RowCommand += new GridViewCommandEventHandler(this.resetpassword); 
    } 

答えて

4

のどちらか(プライマリキーフィールドを通過したいと仮定がPKと呼ばれる)CommandArgumentを渡す:

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

たりImageButtonNamingContainer経由GridViewRowの参照を取得します:

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

またCommandArgumentとしてrowIndexプロパティを渡すことができます。

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

ButtonFieldクラスが自動的に適切なインデックス値でCommandArgumentプロパティに移入されます。他のコマンドボタンについては、コマンドボタンのCommandArgumentプロパティを手動で設定する必要があります。

+0

ButtonField/ImageButtonのノートに感謝します。なぜ、その部分がうまくいかないのか分かりました。最初のオプションを使い、それは完全に動作しています。 – Volvox

4

私はあなたのコードのためのCommandArgument='<%# Container.DataItemIndex %>'

が欠けていると思います。

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

ここには、さらに読むためにSO ASP.NET GridView RowIndex As CommandArgumentに関する質問があります。

ButtonFieldクラスは、CommandArgument プロパティに適切なインデックス値を自動的に設定します。

Here is the MSDN source

+0

@Volvox: 'ButtonField'をさらに読むために、私は自分のアップデートでMSDNのリンクを提供しました。 – jams

+0

ご協力ありがとうございます!確かめます。 – Volvox

関連する問題