ASP GridView 在单击按钮时获取行值 [英] ASP GridView get row values on button click

查看:27
本文介绍了ASP GridView 在单击按钮时获取行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么 - 在图像按钮点击时重置用户密码.

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

到目前为止完成 - 添加了 GridViewCommandEventHandler - 它正确触发.使用来自 MSDN 的代码.我的 e.CommandArgument 得到一个空字符串 (""),它在运行时抛出错误(无法将 "" 解析为 int).

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

我可以在调试器中看到在 e 的其他地方存储了一个rowIndex"属性(正确用于我的点击),我可以访问它吗?我认为 MSDN 的代码会起作用-我是否还做了其他事情来使此错误发生或以其他方式修复它?谢谢.

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 页面代码:

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

推荐答案

要么传递CommandArgument(假设你想传递名为PK的主键字段):

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>

或者通过ImageButtonNamingContainer获取GridViewRow的引用:

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;

您还可以将 RowIndex 作为 CommandArgument 传递:

You can also pass the RowIndex as CommandArgument:

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

ButtonField 类自动使用适当的索引值填充 CommandArgument 属性.对于其他命令按钮,您必须手动设置命令按钮的 CommandArgument 属性.

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.

这篇关于ASP GridView 在单击按钮时获取行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆