GridView的RowCommand事件中取得行索引

ASP.NET GridView行拖拽
本文介绍如何在ASP.NET中使用GridView控件实现行的上移和下移功能。通过在GridView的RowDataBound事件中处理ImageButton的可见性和CommandArgument属性,确保用户仅能在适当的时候触发行的移动操作。
     protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
    
... {
        
if (e.Row.RowType == DataControlRowType.DataRow)//如果是为数据行
        ...{
            ImageButton imgbtnup 
= (ImageButton)e.Row.Cells[1].FindControl("btnMoveUp");//找控件
            imgbtnup.CommandArgument = e.Row.RowIndex.ToString();//设置与此BUTTON关联的命令参数
            imgbtnup.Visible = e.Row.RowIndex != 0
            ImageButton imgbtndown 
= (ImageButton)e.Row.Cells[2].FindControl("btnMoveDown");
            imgbtndown.CommandArgument 
= e.Row.RowIndex.ToString();
            imgbtndown.Visible 
= e.Row.RowIndex != ((DataSet)((GridView)sender).DataSource).Tables[0].Rows.Count - 1;
        }

    }


    
protected   void  GridView1_RowCommand( object  sender, GridViewCommandEventArgs e)
    
... {
        
if (e.CommandName == "MoveUp")
        
...{
            
int index = Convert.ToInt32(e.CommandArgument);//取的行索引
            DataKey key = this.GridView1.DataKeys[index];
            
string keyval = key.Value;//取得主键
        }

        
else if (e.CommandName == "MoveDown")
        
...{
            
int index = Convert.ToInt32(e.CommandArgument);
            DataKey key 
= this.GridView1.DataKeys[index];
            
string keyval = key.Value; 
        }

    }




文章出处:http://blog.youkuaiyun.com/sonce8/archive/2007/09/09/1777777.aspx

转载于:https://www.cnblogs.com/pyt5208/archive/2007/12/01/979442.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值