您的位置:首页 > 编程语言 > ASP

在 Visual Studio2008 中使用自定义 ASP.NET 服务器控件

2009-07-17 10:54 831 查看
由于涉及到将GridView进行改造,当所有的数据删除时,GridView的表结构还要显示。完成之后,想把改造后的GridView生成自定义的服务器控件。

1、改造GridView的类TGridView.cs

using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using System;
using System.Collections;
using System.Data.SqlClient;
using System.Drawing;

namespace GridViewControl
{
public class TGridView : System.Web.UI.WebControls.GridView
{
private Boolean _enableEmptyContentRender = true;
private String _EmptyDataCellCssClass;
/// <summary>
/// 数据源为空时是否显示列头
/// </summary>
public Boolean EnableEmptyContentRender
{
get
{
return _enableEmptyContentRender;
}
set
{
_enableEmptyContentRender = value;
}
}

/// <summary>
/// 数据为空时信息单元格样式类
/// </summary>
public String EmptyDataCellCssClass
{
get
{
return _EmptyDataCellCssClass;
}
set
{
_EmptyDataCellCssClass = value;
}
}

/// <summary>
/// 为空时输出内容
/// </summary>
/// <param name="writer"></param>
protected virtual void RenderEmptyContent(System.Web.UI.HtmlTextWriter writer)
{
System.Web.UI.WebControls.Table table = new System.Web.UI.WebControls.Table();
table.CssClass = this._EmptyDataCellCssClass;
table.GridLines = this.GridLines;
table.CellPadding = this.CellPadding;
table.CellSpacing = this.CellSpacing;
table.BorderStyle = this.BorderStyle;
table.BorderWidth = this.BorderWidth;
table.HorizontalAlign = this.HorizontalAlign;
table.Width = this.Width;
table.CopyBaseAttributes(this);
System.Web.UI.WebControls.TableRow tr = new System.Web.UI.WebControls.TableRow();
table.Rows.Add(tr);
foreach (System.Web.UI.WebControls.DataControlField field in this.Columns)
{
System.Web.UI.WebControls.TableCell cell = new System.Web.UI.WebControls.TableCell();
cell.Text = field.HeaderText;
cell.CssClass = this.CssClass;
tr.Cells.Add(cell);
}
System.Web.UI.WebControls.TableRow row = new System.Web.UI.WebControls.TableRow();
table.Rows.Add(row);
System.Web.UI.WebControls.TableCell msgCell = new System.Web.UI.WebControls.TableCell();
msgCell.CssClass = this._EmptyDataCellCssClass;
if (this.EmptyDataTemplate != null)
{
this.EmptyDataTemplate.InstantiateIn(msgCell);
}
else
{
msgCell.Text = this.EmptyDataText;
}
msgCell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
msgCell.ColumnSpan = this.Columns.Count;
row.Cells.Add(msgCell);
table.RenderControl(writer);
}

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
if (this._enableEmptyContentRender == true && (this.Rows.Count == 0 || this.Rows[0].RowType == System.Web.UI.WebControls.DataControlRowType.EmptyDataRow))
{
RenderEmptyContent(writer);
}
else
{
base.Render(writer);
}
}
}
}


2、将 TGridView.cs 编译生成 TGridView.dll
手动把asp.net的类生成dll文件的方法
开始-》程序-》Microsoft Visual Studio 2005-》Visual Studio Tools-》Visual Studio 2005 命令提示
然后进入你的类目录,比如你的类文件是在E:/test/App_Code下面,
输入如下命令:
Csc /r:system.dll /t:library /out:TGridView.dll TGridView.cs

3、GridView生成自定义的服务器控件。

在“工具箱”中右击并选择“选择项”。

在“选择工具箱项”对话框中选择“.NET Framework 组件”选项卡,然后单击“浏览”按钮以找到刚生成的 TGridView.dll。

选中 TGridView 控件的复选框并单击“确定”。

自定义 TGridView 控件将出现在工具箱中。

--------------------------------------------------------------------------------------------------------------------

不将GridView生成自定义的服务器控件的使用方法,手动

(1)、将**.aspx

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

改为

<TGridView:TGridView ID="GridView1" runat="server">
</TGridView:TGridView>

(2)、在**.aspx开头加入

<%@ Register TagPrefix="TGridView" Namespace="GridViewControl"%>

(3)、在web.config<page><controls></controls></page>中加入<add></add>

<add tagPrefix="TGridView" namespace="GridViewControl"></add>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: