4

私がやっていること - イメージボタンのクリックでユーザーパスワードをリセットします。

これまでに完了 - GridViewCommandEventHandler を追加 - 正しく起動しています。MSDNのコードを使用します。e.CommandArgument の空の文字列 ("") を取得しています。実行時にエラーがスローされます ("" を int に解析できません)。

デバッガーで、「rowIndex」プロパティが e の別の場所に保存されていることを確認できます (私のクリックに対して正しく)。これにアクセスできますか? 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

2 に答える 2

4

次のいずれかを渡しCommandArgumentます (主キー フィールド を渡したい場合PK):

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

またはあなたののGridViewRow経由での参照を取得しNamingContainerますImageButton:

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

RowIndex を CommandArgument として渡すこともできます。

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

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

于 2012-05-04T17:53:11.930 に答える
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 プロパティに適切なインデックス値を自動的に設定します。

MSDNソースはこちら

于 2012-05-04T17:50:00.537 に答える