在 Visual Studio2008 中使用自定义 ASP.NET 服务器控件
2009-07-17 10:54
831 查看
由于涉及到将GridView进行改造,当所有的数据删除时,GridView的表结构还要显示。完成之后,想把改造后的GridView生成自定义的服务器控件。
1、改造GridView的类TGridView.cs
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>
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>
相关文章推荐
- 如何:使用自定义函数对 ASP.NET 服务器控件进行验证
- 如何:使用自定义函数对 ASP.NET 服务器控件进行验证
- ASP.NET - 如何:使用自定义函数对 ASP.NET 服务器控件进行验证
- ASP.NET服务器验证控件和JS自定义验证同时使用(小技巧)
- 【ASP.Net】使用自定义服务器控件
- ASP.NET 部署和使用自定义 Web 服务器控件
- ASP.NET 自定义服务器控件的使用
- Asp.net控件使用(1)-自定义SitemapProvider(转帖)
- ASP.NET中保护自定义的服务器控件
- Asp.net自定义服务器控件开发小技巧: 如何正确获得回传数据
- ASP.NET - 使用 ASP.NET Web 服务器控件(八)
- ASP.NET自定义Web服务器控件-TextBox文本框控件
- Visual studio.net 2003中使用自定义(用户)控件(服务器控件)的方法(原创)
- Asp.net自定义服务器控件开发小技巧: 如何正确获得回传数据
- ASP.NET MVC 扩展服务器控件使用方式
- asp.net不使用服务器控件上传文件时的无意问题
- asp.net web页面自定义分页控件使用详解
- ASP.NET 2.0自定义服务器控件---事件冒泡
- asp.net 自定义服务器控件属性
- 在 ASP.NET Web 部件应用程序中使用服务器控件