当前位置:编程学习 > C#/ASP.NET >>

关于gridView 在线=====================

我在gridView中做了一个模版页 里面加了个Button 我想点击这个Button能选中这行的ID
高手说下详细做法,谢谢 --------------------编程问答-------------------- id是什么?字段?行号? --------------------编程问答-------------------- 字段 --------------------编程问答-------------------- 你把这个字段绑定到GridView的DataKeysName上
button的CommandName 改为"自己定义"
在GridView的RowCommand事件里写
if(e.CommandName == "button的CommandName")
{
//点button要的功能代码
} --------------------编程问答-------------------- 想到2种方法可以取得ID的值,假如ID在第8列
1. 3楼的方法:把模板列中的Button的CommandName设为btn
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btn")
        {
            GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).Parent).Parent;
            Response.Write(gvr.Cells[7].Text);    
        }
    }
2. 和上面类似,只是给模板列中的Button绑定了Click事件,没有设置CommandName
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridViewRow gvr = (GridViewRow)(((Button)sender).Parent).Parent;
        Response.Write(gvr.Cells[7].Text);
    }

   如果模板列中只有一个Button而没有其它控件的话,建议使用GridView自身的ButtonField,只需设定CommandName
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btn")
        {
            //e.CommandArgument为当前行号-1
            Response.Write(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[7].Text);            
        }
    }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,