博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
gridview 选中行
阅读量:4665 次
发布时间:2019-06-09

本文共 1813 字,大约阅读时间需要 6 分钟。

1、gridview中用的是按扭控件imagebutton,

  

    .aspx

 

     <asp:GridView ID="GridView_user" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView_user_RowDataBound"

                        Width="99%" OnRowCommand="GridView_user_RowCommand">
                        <Columns>
                            <asp:TemplateField HeaderText="选择">
                                <ItemTemplate>
                                    <asp:ImageButton ID="ImageButton_select" runat="server" ImageUrl="~/images/Select.gif" CommandName ="ib_sel" CommandArgument ='<%# Eval("user_sn")%>' />
                                </ItemTemplate>
                                <ItemStyle Width="30px" />
                            </asp:TemplateField>
                            <asp:BoundField DataField="user_sn" HeaderText="用户名称" />
                            <asp:BoundField DataField="user_Givename" HeaderText="工号" />
                                                   </Columns>
                    </asp:GridView>

 

 

2、后台写了一个数据源的绑定,完成的操作是当选中某行时,获得应行上的“用户名称”和“工号”的两个字段的值,大家已经看到,在.aspx页上已经在ImageButton 按扭上绑定了“用户名称”的值,即CommandArgument ='<%# Eval("user_sn")%>'  ;那么我们只要再获得“工号”的值就可以了,

接下来,我们来看一下在后台.aspx.cs文件中如何写:

 

.aspx.cs

 

/*初始化gridview绑定*/

 

  void DBind(DataTable dt)

    {
        string[] str = new string[] { "user_Givename" };        

        this.GridView_user.DataKeyNames = str;                //这里是设置主键

        GridView_user.DataSource = dt;             //  注:dt 这里是一个已经事先写好的datatable列表的数据源,这里你可以换成你自己要用的数据源的绑定。

        GridView_user.DataBind();
     }

 

 

/*功能:当选中某行时,获得应行上的“用户名称”和“工号”的两个字段的值*/

/*关键就是这里的实现喽*/

 

    protected void GridView_user_RowCommand(object sender, GridViewCommandEventArgs e)

    {
        if (e.CommandName == "ib_sel")
        {
            string name = e.CommandArgument.ToString();

            Control cmdSource = e.CommandSource as Control;

            GridView gridv = sender as GridView;
            GridViewRow row = cmdSource.NamingContainer as GridViewRow;
            int rowIndex = row.RowIndex;
            string workno = gridv.DataKeys[rowIndex].Value.ToString();          //获得选中行的“工号”字段的值
           

            string strword = name+ "(" + workno + ")";   

            

           ClientScript.RegisterStartupScript(Page.GetType(), "script", "alert('"+strword.Trim()+"')", true);        

        }
    }

 

3、现在就完成了,运行一下看看效果吧,仅供参考,如果有更好的方法,欢迎大家跟贴,在此向大家学习了。^0^

转载于:https://www.cnblogs.com/aminta/articles/1273146.html

你可能感兴趣的文章
制作SM2证书
查看>>
C++ set简介及简单应用
查看>>
【C#】 Socket通讯客户端程序
查看>>
第四章作业4
查看>>
MySQL-如何删除hash表分区
查看>>
eclipses新建maven项目xml第一行报错 什么没有,又下载不了
查看>>
Ibatis中返回一对多结果集的解决方法
查看>>
“匈牙利标记法”相关知识集结
查看>>
TCP: time wait bucket table overflow问题解决
查看>>
Ubuntu Phone开箱上手
查看>>
分布式系统常用思想和技术
查看>>
Tomcat7调试运行环境搭建与源代码分析入门
查看>>
C语言简单实现下载
查看>>
递归读取文件夹下的文件
查看>>
CodeForces Round 200 Div2
查看>>
HDU-2032
查看>>
总结day6 ---- set集合,基本类型的相互转化,编码,数据类型总结,循环时候不要动列表或者字典,深浅copy...
查看>>
C++的Enum hack(转)
查看>>
【方案】去哪儿网徐磊:如何利用开源技术构建日处理130亿+的实时日志平台?...
查看>>
工作中遇到的人和事
查看>>