asp.net 自定义控件(2种方法)
概念
1. 构建自定义控件
目的:创建类似MS自带的控件,具有属性、方法、事件等特性,利于重用。技术:HtmlTextWriter类---用于生成Html。
该类所支持的方法:
AddAttribute()---给调用RenderBeginTag()生成的标签添加一个Html属性.
AddStyleAttribute()---给调用RenderBeginTag()生成的标签添加一个CSS属性.
RenderBeginTag()---生成开始Html标签.
WriteBreak()—生成<br />标签
方法所用枚举:
HtmlTextWriterTag---Html标签列表
HtmlTextWriterAttribute—Html属性列表
HtmlTextWriterStyle---CSS属性列表.
重写RenderContents(HtmlTextWriter writer)用于呈现页面.
组合控件包含子控件,在属性内要强制调用EnsureChildControls(),重写CreateChildControls()方法
指定包围WebControl标签,重写HtmlTextWriter TagKey属性
在混合控件中,两种方法都要重写.
实现方式:在组件中编写代码.调用命名空间.
System.Web.UI.Control 所有控件的基类
System.Web.UI.WebControls.WebControl 含有开始结束标签
System.Web.UI.WebControls.CompositeControl 组合控件
继承它们,然后在其中编写各种所需的属性\方法\事件.其布局方法用编码代替了直观的方法.
2. 视图状态和控件状态
视图状态概念:利用一个_VIEWSTATE隐藏表单域在页面回传中保存控件属性的状态.应用于简单的值.
目的:重复回传也能显示新文本
实现方式:添加ViewStateText属性
技术:ViewState[“Message”]=value
控件状态概念:添加在控件状态中的值保存在_VIEWSTATE中,而它不能被禁用.
目的:用于在页面回传中存贮最重要的信息
技术:重写OnInit(EventArgs e)
SaveControlState() 保存
LoadControlState(object saveState) 加载
3. 处理回传数据
目的:用于状态表单域数据传给控件.相当于事件的实现.
技术:实现IPostBackDataHandler接口
所包含的方法:LoadPostData() 搜索从浏览器传回的表单域.
RaisePostDataChangedEvent() 引发表单域的值发生改变的事件
实现方式:从WebControl,IpostBackDataHandler继承
分别定义事件\视图\属性\表现\接口的方法.
处理回传数据,例如:button的事件就是用来处理回传事件的
技术:实现IpostBackEventHandler接口
包括:RaisePostBackEvent()方法,用于服务器端调用
Page.ClientScript.GetPostBackClientHyperlink(this,String.Empty)返回在浏览器引起表单回传的JavaScript.
传递回传时间参数
可为GetPostBackClientHyperlink()提供可选参数,其值可传到服务器端的RaisePostBackEvent()中.
使用回传选项
PostBackOptions类指定回传属性,例如ActionUrl跨页提交,验证,焦点等.
Page.ClientScript.GetPostBackEventReference(options);实例化后传递给方法.
实现方式:
4. 使用控件属性集合
目的:创建表示对象集合的控件,如DropDownList
技术:ParseChildren attribute 如何分析控件包含内容
[ParseChildren(true)]所包含内容作属性分析
[ParseChildren[(false)]独立分析
ControlBuilder 自定义此类去修改控件解析内容
包含方法:AllowWhiteSpaceLiterals() 删除空间内容所有空白
AppendLiteralString()删除空间内容中中所有空白
GetChildControlType() 指定某种标签解析成哪种控件
AddParsedSubObject() 指定把哪些解析过的控件添加到Controls集合.
实现方式:判断内容解析成控件还是属性 ParseChildren
构造要被解析的控件 Class
是控件解析成何种控件 继承ControlBuilder GetChildControlType()
把解析过的控件加进Controls集合,剔除其他的. AddParsedSubObject()
再加入这种控件的其他功能 视图,回传
构造控件的实现方式 RenderContents(------)
5. 构建更好的设计器体验
设计时属性(densign-time attribute),用于给属性关联某个编辑器
如:[Category(“Movie”)]
[Description(“Movie Title”)]
创建ControlDesigner改变自定义控件在设计视图中的外观
创建ContainerControlDesigner
目的:在设计时,添加子控件到其中.
技术:继承ContainerControlDesigner
[Designer(typeof(GradientPanelDesigner))]关联attribute
创建DesignerActionList
目的:设计时添加智能标签功能
一、在类中写自定义控件
自定义DropDownList:
1,首先创建一个cs类文件Controls(采用工厂模式)
using System.Collections.Generic;
using System.Text;
using System;
using System.IO;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Configuration;
using Model;
using BLL;
namespace Controls
{
public class useDepDropDownList : DropDownList
{
//不带Session的自定义控件
public useDepDropDownList()
{
this.Items.Add(new ListItem("全部部门", " "));
foreach (DepInfo cat in Dep.GetDeps())
{
this.Items.Add(new ListItem(cat.Name, cat.Name));
}
}
}
public class CompanyDepDropDownList : DropDownList
{
//带Session的自定义控件
public CompanyDepDropDownList()
{
if (Context.Session["username"] != null)
{
this.Items.Add(new ListItem("全部", "全部"));
foreach (GWInfo cat in GW.GetDepByloginid(Context.Session["username"].ToString()))
{
this.Items.Add(new ListItem(cat.dep.ToString(), cat.dep.ToString()));
}
}
}
}
}
2,创建一个页面aspx页面使用自定义的控件来绑定下拉框的数据
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Defaulthc.aspx.cs" Inherits="Import_Defaulthc" %>
<%@ Register Assembly="Controls" Namespace="Controls" TagPrefix="Hc" %>
<!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>
部门<Hc:useDepDropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
Width="120px">
</Hc:useDepDropDownList>
</div>
</form>
</body>
</html>
这样就不用在UI页面的aspx.cs里写任何代码,而且自定义控件可重复使用省去很多重复代码
二、还有一种方法vs里提供了自定义Web用户控件(.ascx)
同样在你的自定义控件.ascx.cs内写入方法,和上述相同
public class useDepDropDownList : DropDownList
{
//不带Session的自定义控件
public useDepDropDownList()
{
this.Items.Add(new ListItem("全部部门", " "));
foreach (DepInfo cat in Dep.GetDeps())
{
this.Items.Add(new ListItem(cat.Name, cat.Name));
}
}
}
在调用它的 .aspx文件中同样需要在最上边加入自定义命名:
首先要注册自定义控件,注册的语句要写到页面的上方(属于页面指令的位置。 )
<%@ Register TagPrefix="可自定义的控件前缀" TagName="自定义的控件标签名" Src="自定义控件的位置"%>
<%@ Register src="MyDDL.ascx" TagName="useDepDropDownList" TagPrefix="Hc" %>
然后在aspx文件中使用:
<自定义的控件前缀:自定义的控件标签名 ID="yourControl" runat="server"></前缀:控件标签名>
<Hc:useDepDropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
Width="120px">
</Hc:useDepDropDownList>
使用自定义控件的属性在 .aspx.cs文件中调用就可以了:
还可以在ascx中自己设计一个模版块,在aspx中引用,ascx.cs后台可以写任意方法
.ascx代码
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserLeftControl.ascx.cs" Inherits="Controls_UserLeftControl" %>
<div style="text-align:left;">
<div>
<h3>登录用户信息</h3>
<ul>
<li>用户名:<span class="red"></span></li>
<li>称呼:<span ></span></li>
<li>性别:<span ></span></li>
<li>注册时间:<span></span></li>
<li>[<a style="color:Red" href="~/index.aspx" >回到首页</a>]</li>
</ul>
</div>
<div class="menber_link">
<h3>管理中心</h3>
<ul>
<li><a href="#">我的资料</a></li>
<li><a href="#">我的收货地址</a></li>
<li><a href="#">修改密码</a></li>
<li><a href="#">我的订单</a></li>
<li><a href="#">我的积分</a></li>
<li><a href="#">我的收藏</a></li>
<li><a href="#">我的购物车</a></li>
<li><a href="#">投诉/建议/咨询</a></li>
<li><asp:LinkButton ID="linkbin" CausesValidation="false" runat="server">安全退出</asp:LinkButton></li>
</ul>
</div>
</div>
aspx中引用自定义快
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="text1.aspx.cs" Inherits="text1" %>
<%@ Register src="~/Controls/UserLeftControl.ascx" TagName="UserLeftControl" TagPrefix="Hc" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
<link href="~/css/UserLeft.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="from1" runat="server">
<div runat="server">
<Hc:UserLeftControl runat="server"></Hc:UserLeftControl>
</div>
</form>
</body>
</html>
将编译好的dll添加到工具箱内
在工具栏下面找个空白的地方点击右键-->添加选项卡-->选择项-->浏览-->找到自己编译好的dll-->确定,这时候就能在工具箱的自定义选项卡内看到自己的控件了.
http://msdn.microsoft.com/zh-cn/library/yhzc935f(VS.80).aspx
http://www.cnblogs.com/holywolf/archive/2008/12/23/1360316.html
http://blog.csdn.net/gujia200808/article/details/5403437
注册AssemblyInfo.cs
http://blog.bossma.cn/dotnet/aspnet-custom-control-develop-2/
注:欢迎喜爱编程的朋友进群交流。QQ群交流:256169347
群共享了很多pdf书籍文档