这里是一个LongTextField继承于BoundField用来显示长文本(比如说备注信息之类的),该控件根据当前的显示模式是编辑模式还是显示模式来分别显示一个Div控件和TextBox控件.
我的表达能力不好,还是直接上代码吧.
首先定制控件如下


using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
/// <summary>
///LongTextField 的摘要说明
/// </summary>
namespace Custom
{
public class LongTextField : BoundField
{
private Unit _width = new Unit("250px");
private Unit _height = new Unit("60px");
/// <summary>
/// The Width of field
/// </summary>
public Unit Width
{
get { return _width; }
set { _width = value; }
}
/// <summary>
/// The Height of field
/// </summary>
public Unit Height
{
get { return _height; }
set { _height = value; }
}
protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
{
// if not editing,show in scrolling div
if ((rowState & DataControlRowState.Edit) == 0)
{
HtmlGenericControl div = new HtmlGenericControl("div");
div.Attributes["Class"] = "longTextField";
// Set width of div
div.Style[HtmlTextWriterStyle.Width] = _width.ToString();
// Set height of div
div.Style[HtmlTextWriterStyle.Height] = _height.ToString();
//本句代码有疑惑,明天查看
div.Style[HtmlTextWriterStyle.Overflow] = "auto";
// Add eventhandler to handle 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 += new EventHandler(txtEdit_DataBinding);
cell.Controls.Add(txtEdit);
}
}
void div_DataBinding(object sender, EventArgs e)
{
HtmlGenericControl div = sender as HtmlGenericControl;
object value = this.GetValue(div.NamingContainer);
// Assign the formatted value
div.InnerText = this.FormatDataValue(value, this.HtmlEncode);
}
void txtEdit_DataBinding(object sender, EventArgs e)
{
TextBox txtBox = sender as TextBox;
object value = this.GetValue(txtBox.NamingContainer);
txtBox.Text = this.FormatDataValue(value, this.HtmlEncode);
}
}
}
然后在页面中使用的代码如下


<%@ Page Language="C#" %>
<%@ Register Namespace="Custom" TagPrefix="LLS" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>自定义数据字段</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="grd" DataSourceID="srcProducts" AutoGenerateEditButton="true"
AutoGenerateColumns="false" DataKeyNames="EmployeeID">
<Columns>
<asp:BoundField HeaderText="EmployeeID" DataField="EmployeeID" Visible="false" />
<LLS:LongTextField DataField="Notes" HeaderText="备注">
</LLS:LongTextField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="srcProducts" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT EmployeeID,LastName,FirstName,Notes FROM Employees" UpdateCommand="UPDATE Employees SET Notes = @Notes WHERE EmployeeID = @EmployeeID ">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>