定制.NET GridView的长文本显示表格

这里是一个LongTextField继承于BoundField用来显示长文本(比如说备注信息之类的),该控件根据当前的显示模式是编辑模式还是显示模式来分别显示一个Div控件和TextBox控件.

我的表达能力不好,还是直接上代码吧.

首先定制控件如下

ContractedBlock.gifExpandedBlockStart.gifCode
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);
        }
    }
}

然后在页面中使用的代码如下

ContractedBlock.gifExpandedBlockStart.gifCode
<%@ 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>


转载于:https://www.cnblogs.com/zhuisha/archive/2008/08/24/1275129.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值