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 ...
随机推荐
- Easyui 异步树直接所有展开
初始化异步树直接所有展开代码: $(function(){ $('#tt').tree({ url:'<%=request.getContextPath()%>/treeInit', li ...
- opencv环境的搭建,并打开一个本地PC摄像头。
1.opencv环境结构 推荐连结 http://www.cnblogs.com/Anykong/archive/2011/04/06/Anykong_OpenCV1.html 2.以下是基本測试,和 ...
- LA3026 - Period(KMP)
For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...
- Cookie rejected: Illegal path attribute "/nexus". Path of origin: "/content/" 解
问题叙述性说明 通过运行"mvn clean deploy" 命令 将 Maven 项目公布 Nexus 当PW.举例控制台输出以下警告消息: [INFO] Downloaded: ...
- C++结构体之统计最高最低分
[Submit][Status][Web Board] Description 输入学生的姓名和成绩,统计出最高分的学生和最低分的学生. Input 输入5个学生的姓名和分数,用结构体完成 Outpu ...
- Web指纹识别目的Discuz识别+粗糙的版本演绎
这个识别程序是本学期在我的职业培训项目.它是做一类似至Zoomeye怪东西,然后使用ES集成,为了让搜索引擎寻找.因此,我们必须首先去网上识别相应的能力Web包裹,如果用户输入的关键词:Discuz ...
- Android Studio 1.0 (稳定版) 完全攻略
这篇博文中主要从以下几点进行叙述: 1.Android Studio安装与使用 2.Android Studio特性 3.Android Studio优点 Android Studio 安装与使用 A ...
- Kotlin
关于Kotlin,网上已有一些介绍的文章,包括Antonio Leiva的这组blog翻译稿.不过,我还是想跟进它们.翻译它们,以锻炼自己的英文翻译.各位高手发现问题,请及时“拍砖”. 原文题目:Ko ...
- PKU A Simple Problem with Integers (段树更新间隔总和)
意甲冠军:一个典型的段树C,Q问题,有n的数量a[i] (1~n),C, a, b,c在[a,b]加c Q a b 求[a,b]的和. #include<cstdio> #include& ...
- 我有DIY一Android遥控-所有开源
我有DIY一Android遥控-所有开源 1.试用 记得宋宝华在「设备驱动开发具体解释」提出一个这种理论「软件和硬件互相渗透对方的领地」,这次证明还是确实是这样,使用上层APP软件加上简单的更为简单的 ...