首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>NET专区>ASP.NET>实例讲解为GridView/DataGrid整行添加服务器事件
实例讲解为GridView/DataGrid整行添加服务器事件
来源: 发布时间:2008-08-28 发布人: 浏览: 人次   字体: [ ]  

在asp.net开发中,GridView/DataGrid 本身均支持行选择事件(通过设置Button/LinkButton.CommandName="Selected",并在 SelectedIndexChanged 事件中处理)。
然而,有时候我们希望用户点击 GridView/DataGrid 一行中任意位置都可以实现触发一个事件,并在服务端对此行进行相应处理,现在我们就实现此功能。

实现方式如下:

这里我们采取的方法有点 "hack" :
通过客户端 javascript 引发行中隐藏的按钮(Button/LinkButton 均可以)的click 事件。

主要代码如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
            <Columns>                             
                <asp:TemplateField HeaderText="ProductName" >
                    <ItemTemplate>
                        <%# Eval("ProductName") %>
                        <asp:Button ID="btnHiddenPostButton" CommandName="HiddenPostButtonCommand" runat="server" Text="HiddenPostButton" style="display:none" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
            </Columns>
        </asp:GridView>
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button;
        if (btnHiddenPostButton != null) {
            e.Row.Attributes["onclick"] = String.Format("javascript:document.getElementById('{0}').click()", btnHiddenPostButton.ClientID);
            // 额外样式定义
            e.Row.Attributes["onmouseover"] = "javascript:this.style.background='red'";
            e.Row.Attributes["onmouseout"] = "javascript:this.style.background=''";
            e.Row.Attributes["style"] = "cursor:pointer";
            e.Row.Attributes["title"] = "单击选择当前行";
        }
        // 若希望将隐藏按钮单独放于一列,则设置此列隐藏,占位符 <cellIndex> 表示此列索引
        //e.Row.Cells[<cellIndex>].Attributes["style"] = "display:none";
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int rowIndex = -1;
        GridViewRow row = null;
        switch (e.CommandName) {           
            case "HiddenPostButtonCommand": // 模板列               
                Control cmdControl = e.CommandSource as Control; // 表示触发事件的 IButtonControl,保持统一性并便于后续操作,我们这里直接转化为控件基类 Control
                row = cmdControl.NamingContainer as GridViewRow; // 当前行
                // 如何访问单元格值
                // string txt = row.Cells[0].Text;
                // 如何获取模板列中的 Label
                // string lbl = row.FindControl("MyLabelID") as Label;
                // 执行更多的自定义操作
                //
                //
                Response.Write(String.Format("GridView Version 当前第 {0} 行:", row.RowIndex + 1));
                break;
            // case "Command2":
            // more cases
            //                
        }
    }
测试效果


相 关 文 章   发布商链接
·实例讲解GridView/DataGri同时支持行...
·Asp.net实现加密和解密的运算代码实...
·Asp.net+AJAX实现的省市县无刷新级联...
·实例讲解asp.net一次预览并上传多张...
·asp.net中页面Div层内部的控件在垂直...
·解决B/S系统开发中Activex控件需要激...
·asp.net如何自定义配置文件的探讨
·如何在asp.net刷新页面时不提示 "重...
·如何在asp.net中使用javascript获取s...
·asp.net动态生成txt文件并提供用户下...
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·如何实现在DataList控件中的D...
·实例讲解asp.net生成高质量缩
·asp.net适用于IE或FireFox的...
·举例详解C#代码与javaScript...
·asp.net无刷新上传文件
·在Ie中保存图片时出现"800700...
·asp.net文件上传大小限制的控
·asp.net动态生成txt文本文件...
·asp.net实现将Excel文件导入...
·实现多列,带图片的DropDownLi...
·javascript实现TreeView 控件
·ASP.NET配置Word的操作权限
·让FileUpload控件在IE和FireF...
·ASP.NET实现Office文档的分类
·asp.net文件下载显示进度条的
热 门 文 章
·asp.net(C#)上传下载及文件管...
·图解asp.net如何用excel做报...
·asp.net无刷新上传文件
·Asp.Net防止刷新重复提交数据...
·asp.net实现将Excel文件导入...
·asp.net中Web.Config配置文件...
·ASP.NET取得物理路径和虚拟路...
·asp.net(c#)生成验证码代码,...
·asp.net页面回传与js调用服务...
·asp.net(C#版)实现登录验证码...
·DataGrid中DropDownList触发S...
·ASP.net 实现批量数据更新或...
·asp.net中DataBinder.Eval的...
·asp.net可输入的下拉框复合控...
·asp.net中常见的几种日历控件...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .