ASP GridView控件得到按一下按钮行值 [英] ASP GridView get row values on button click

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

问题描述

我在做什么 - 在ImageButton的点击重置用户密码

完成迄今为止 - 添加GridViewCommandEventHandler - 它能否正常触发。使用code从<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventhandler.aspx\"相对=nofollow> MSDN 。我得到一个空字符串()为我e.CommandArgument,并运行(无法解析时,它抛出一个错误,为int)。

我可以在调试器中看到有一个rowIndex位置属性存储(正确为我的点击)其他在电子,我可以访问此?我想MSDN的code将工作 - 有没有别的东西,我已经做了,使这个错误发生,或者另一种方式来解决它?谢谢你。

 无效resetpassword(对象发件人,GridViewCommandEventArgs E)
{
    //如果使用多个ButtonField字段柱,使用
    // CommandName属性,以确定哪个按钮被点击。
    如果(e.CommandName ==resetpass)
    {
        //将存储在CommandArgument行索引
        //属性为整数。
        INT指数= Convert.ToInt32(e.CommandArgument);        //检索包含点击按钮的行
        //从行集合的用户。使用
        // CommandSource属性来访问GridView控件。
        GridView控件GridView1 =(GridView控件)e.CommandSource;
        GridViewRow行= GridView1.Rows [指数]        。字符串usrname = row.FindControl(用户名)的ToString();

aspx页面code:

 &LT; ASP:的TemplateField的HeaderText =重设密码&GT;
                &LT;&ItemTemplate中GT;
                    &LT; ASP:ImageButton的ID =ibtnReset=服务器的CausesValidation =假
                        的CommandName =resetpass的ImageUrl =〜/图片/ glyphicons_044_keys.png文本=按钮/&GT;
                &LT; / ItemTemplate中&GT;
                &LT; HeaderStyle宽度=70像素/&GT;
                &LT; ItemStyle Horizo​​ntalAlign =中心/&GT;
            &LT; / ASP:的TemplateField&GT;

事件增加code:

 保护无效GridView1_RowCreated(对象发件人,GridViewRowEventArgs E)
    {
        GridView1.RowCommand + =新GridViewCommandEventHandler(this.resetpassword);
    }


解决方案

无论是通过 CommandArgument (假设你想通过所谓的主键字段 PK

 &LT; ASP:的TemplateField&GT;
    &LT;&ItemTemplate中GT;
      &LT; ASP:ImageButton的=服务器ID =ibtnReset
        文本=重设密码
        的CommandName =resetpass
        CommandArgument ='&LT;%#的eval(PK)%GT;'
    &LT; / ItemTemplate中&GT;
  &LT; / ASP:的TemplateField&GT;

或获得的 GridViewRow 通过 NamingContainer 你的的ImageButton

  WebControl的WC = e.CommandSource为WebControl的;
GridViewRow行= wc.NamingContainer为GridViewRow;
字符串usrname =((文本框)row.FindControl(用户名))文本。

您也可以通过为rowIndex位置CommandArgument:

  CommandArgument ='&LT;%#Container.DataItemIndex%GT;'

ButtonField字段类自动填充相应的索引值 CommandArgument 属性。对于其他命令按钮,您必须手动设置命令按钮的 CommandArgument 属性。

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

解决方案

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.

这篇关于ASP GridView控件得到按一下按钮行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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