asp.net学习之扩展GridView
本节讨论如何从现有的控件,进而扩展成强大的,更定制的GridView控件
1.扩展BoundField
默认的BoundField不能显示多文本,文字一多,就会扩大整个Table的Height值,解决这个问题的方法可以通过TemplateField加入Div控件来解决,但是,也可以从BoundField类上进行扩展,加入一点特有的功能,让他能够显示多文本
例1: 创建长文本字段
===App_code\myControls.cs===
Codenamespace myControls{ // 自定义GridView的Field字段,该字段能够在显示模式下 // 显示多行文本,在编辑模式下显示多行输入框 public class LongTextField: BoundField // 继承BoundField { private Unit _width = new Unit("250px"); private Unit _height = new Unit("60px"); // LongTextField有两个属性,分别是Widht和Height. public Unit Width { get { return _width; } set { _width = value; } } public Unit Height { get { return _height; } set { _height = value; } } // InitializeDataCell 方法是一种帮助器方法,用于初始化 BoundField 对象中的单元格 // 扩展 BoundField 类时,可以重写该方法,以执行自定义初始化例程。 protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState) { // 不处于编辑模式下 if((rowState&DataControlRowState.Edit)==){ HtmlGenericControl div = new HtmlGenericControl("div"); //创建一个Html中的div控件 div.Attributes["class"] = "longTextField"; //通过HtmlTextWriterStyle设置div控件的样式 div.Style[HtmlTextWriterStyle.Width] = _width.ToString(); div.Style[HtmlTextWriterStyle.Height] = _height.ToString(); div.Style[HtmlTextWriterStyle.Overflow] = "auto"; // div控件的DataBinding事件发生时,调用div_DataBinding函数 div.DataBinding += new EventHandler(div_DataBinding); cell.Controls.Add(div); } else { TextBox txtEdit = new TextBox(); txtEdit.TextMode = TextBoxMode.MultiLine; txtEdit.Width = _width; txtEdit.Height = _height; // txtEdit的DataBinding事件发生时,调用txtEdit_DataBinding函数 txtEdit.DataBinding += new EventHandler(txtEdit_DataBinding); cell.Controls.Add(txtEdit); } } void div_DataBinding(object sender,EventArgs e) { HtmlGenericControl div = (HtmlGenericControl)sender; // 取得控件 object value = this.GetValue(div.NamingContainer); // Get the field value; div.InnerText = this.FormatDataValue(value, this.HtmlEncode); // Assign the formatted value } void txtEdit_DataBinding(object sender,EventArgs e) { TextBox txtEdit = (TextBox)sender; Object value = this.GetValue(txtEdit.NamingContainer); // Get the field value txtEdit.Text = this.FormatDataValue(value, this.HtmlEncode); } }}
===custer_list.aspx===
Code<%@ Register TagPrefix="custom" Namespace="myControls" %> <!-- 引入LongTextField类型 --><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> <asp:BoundField DataField="Director" HeaderText="Director" SortExpression="Director" /> <custom:LongTextField DataField="Description" Width="300px" height="60px" HeaderText="Movie Description" /> </Columns></asp:GridView>
2.扩展ButtonField
扩充的ButtonField能够具有警告作用,即在点击时能够弹出确认消息。
例2:扩展ButtonField字段
Codepublic class DeleteButtonField : ButtonField{ private string _confirmMessage = "确认要删除吗?"; public string ConfirmMessage { get { return _confirmMessage; } set { _confirmMessage = value; } } // 默认情况下,作为删除按钮,按钮上显示删除字样 public DeleteButtonField() { this.CommandName = "Delete"; this.Text = "删除"; } public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex) { base.InitializeCell(cell, cellType, rowState, rowIndex); if(cellType==DataControlCellType.DataCell) //如果是数据Cell { WebControl button = (WebControl)cell.Controls[]; button.Attributes["onclick"] = String.Format("return confirm('{0}');", _confirmMessage); } }}
3.待续…
asp.net学习之扩展GridView的更多相关文章
- asp.net学习之GridView事件、GridViewRow对象
原文:asp.net学习之GridView事件.GridViewRow对象 1. GridView控件的事件 GridView有很多事件,事件可以定制控件的外观或者行为.事件分为三类 1.1 ...
- asp.net学习之GridView七种字段
原文:asp.net学习之GridView七种字段 asp.net中GridView绑定到数据源时,可以自动显示数据源的各个字段.只要设定其AutoGenerateColumns为TRUE即可.但这, ...
- asp.net学习之数据绑定控件、数据源控件概述
原文:asp.net学习之数据绑定控件.数据源控件概述 1.asp.net数据绑定控件分为三大类,每个类分别进行详细: ● 列表式数据绑定控件: 列表式数据绑定控件常用来在一个表格内的一个字 ...
- asp.net学习之DataList控件
asp.net学习之DataList控件 DataList控件与Repeater控件一样由模板驱动,与Repeater控件不同的是: DataList控件默认输出是一个HTML表格.DataLis ...
- asp.net学习之 数据绑定控件--表格绑定控件
原文:asp.net学习之 数据绑定控件--表格绑定控件 数据绑定 Web 服务器控件是指可绑定到数据源控件,以实现在 Web 应用程序中轻松显示和修改数据的控件.数据绑定 Web 服务器控件 ...
- asp.net学习之ado.net(连接模式访问)
原文:asp.net学习之ado.net(连接模式访问) ado.net框架支持两种模式的数据访问: 连接模式(Connected)和非连接模式(disconnected).这一节介绍如何使用连 ...
- asp.net学习之ado.net(无连接模式中的DataAdapter)
原文:asp.net学习之ado.net(无连接模式中的DataAdapter) 在非连接模式下,主要讨论以下对象:DataAdapter. DataAdpater的作用是在物理存储模式的数据 ...
- asp.net学习之SqlDataSource
原文:asp.net学习之SqlDataSource 通过 SqlDataSource 控件,可以使用 Web 服务器控件访问位于关系数据库中的数据.其中可以包括 Microsoft SQL Serv ...
- ASP.NETCore学习记录(一)
ASP.NETCore学习记录(一) asp.net core介绍 Startup.cs ConfigureServices Configure 0. ASP.NETCore 介绍 ASP.N ...
随机推荐
- Hadoop学习笔记Hadoop伪分布式环境建设
建立一个伪分布式Hadoop周围环境 1.主办(Windows)顾客(安装在虚拟机Linux)网络连接. a) Host-only 主机和独立客户端联网: 好处:网络隔离: 坏处:虚拟机和其他serv ...
- [LeetCode129]Sum Root to Leaf Numbers
题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...
- struts 1.x 原理
Struts 当我接触到这个框架的时候.我就在想为什么是struts,而不是什么CraigFramework.结构.支撑,这样来理解也不难怪了. 为什么须要struts? 在struts in act ...
- Ant—Ant标签解释
采用ant命令必须写ant命令脚本,脚本由非常多Ant标签组成.现在总结一下我也遇到过Ant标签: 版权声明:本文博主原创文章,博客,未经同意不得转载.
- Nio学习4——EchoServer在IO,NIO,NIO.2中的实现
堵塞IO实现: public class PlainEchoServer { public void serve(int port) throws IOException { final Server ...
- 8、Cocos2dx 3.0三,找一个小游戏开发3.0存储器管理的版本号
重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27693365 复杂的内存管理 移动设备上的硬件资源十 ...
- Cocos2dx-3.1.1 冒险01----> 文件夹结构、新项目project创建并执行
windows开发环境:window7.vs2012.python2.7.6 Cocos2d-x 3.1.1的完整文件夹例如以下:比起曾经的2.x的版本号来说分类更规范了 watermark/2/te ...
- SQLServer数据类型优先级对性能的影响
原文:SQLServer数据类型优先级对性能的影响 译自: http://www.mssqltips.com/sqlservertip/2749/sql-server-data-type-preced ...
- debian软件安装基础(同tomcat案件)
基本介绍 笔者是一个Linux盲.一旦只在虚拟机上载通过Ubantu-图形版本,我看着接口.打了几场比赛卸载的光盘上. 往下看,在过去的几天.试想想,在Linux关于建设nexus(mavenPW)玩 ...
- Bag标签成一条线的代码来实现中国字
说明: <Bag id=书包名 act=2words[name=key] [gap=字符] [quotes=引號]>中英文混合内容</Bag> 例0: 默认分词(无gap和qu ...