定製.NET GridView的長文本顯示表格

來源:互聯網
上載者:User

這裡是一個LongTextField繼承於BoundField用來顯示長文本(比如說備忘資訊之類的),該控制項根據當前的顯示模式是編輯模式還是顯示模式來分別顯示一個Div控制項和TextBox控制項.

我的表達能力不好,還是直接上代碼吧.

首先定製控制項如下

Code
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);
        }
    }
}

然後在頁面中使用的代碼如下

Code
<%@ 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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.