本节讨论如何从现有的控件,进而扩展成强大的,更定制的GridView控件
1.扩展BoundField
默认的BoundField不能显示多文本,文字一多,就会扩大整个Table的Height值,解决这个问题的方法可以通过TemplateField加入Div控件来解决,但是,也可以从BoundField类上进行扩展,加入一点特有的功能,让他能够显示多文本 例1: 创建长文本字段 ===App_code\myControls.cs===
Code namespace 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)==0){ 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字段
Code public 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[0]; button.Attributes["onclick"] = String.Format("return confirm('{0}');", _confirmMessage); } }}
3.待续…