一、控件生命周期:
简单概述:
简单实例:
OnInit(EventArgs e)
{
OutPut("1.
OnInit");
base.OnInit(e);
this.Page.RegisterRequiresPostBack
}
LoadViewState(object savedState)
{
OutPut("2.
LoadViewState");
base.LoadViewState(savedState);
}
LoadPostData(string postDataKey, NameValueCollection
postCollection)
{
OutPut("3.
LoadPostData");
return true;
OnLoad(EventArgs e)
{
OutPut("4.
OnLoad");
base.OnLoad(e);
RaisePostDataChangedEven
{
OutPut("5.
RaisePostDataChangedEven
}
RaisePostBackEvent(string
eventArgument)
{
OutPut("6. RaisePostBackEvent");
}
OnPreRender(EventArgs e)
{
OutPut("7.
OnPreRender");
base.OnPreRender(e);
}
SaveViewState()
{
OutPut("8.
SaveViewState");
base.SaveViewState();
return new Pair();
Render(HtmlTextWriter writer)
{
writer.Write("<INPUT type=button name="{0}" value="Click Me!"
style='position:absolute; left: 20; top: 280px' onclick="{1}">",
this.UniqueID, Page.ClientScript.GetPostBackEventReferenc
""));
OutPut("9.
Render");
base.Render(writer);
OnUnload(EventArgs e)
{
OutPut("10.OnUnload");
base.OnUnload(e);
}
override void Dispose()
{
OutPut("11.Dispose");
base.Dispose();
}
strText)
{
(this.DesignMode ==
false)
{
HttpContext.Current.Response.Write(strText +
"<br>");
}
二、控件呈现顺序(以Render开头方法)
三、Render呈现控件的几种方式
概述:
实例:
RenderContents(HtmlTextWriter
output)
{
output.AddAttribute(HtmlTextWriterAttribute.Href, "http://www.cnblogs.com/");
output.AddAttribute(HtmlTextWriterAttribute.Target,
"blank");
output.AddStyleAttribute(HtmlTextWriterStyle.Color,
"Blue");
output.AddStyleAttribute(HtmlTextWriterStyle.Cursor,
"Hand");
output.RenderBeginTag(HtmlTextWriterTag.A);
output.Write(this.Text1);
output.RenderEndTag();
output.WriteBreak();
//方式2
output.Write("<a href='http://www.csdn.net' target='blank'
style='color:Blue;cursor:Hand;'>");
output.Write(this.Text2);
output.Write("</a>");
output.Write("<br>");
//方式3
HtmlGenericControl A = new
HtmlGenericControl("A");
A.Attributes.Add("href", "http://blog.csdn.net/ChengKing");
A.Attributes.Add("target",
"blank");
A.Style.Add(HtmlTextWriterStyle.Color,
"Blue");
A.Style.Add(HtmlTextWriterStyle.Cursor,
"Hand");
A.InnerText =
this.Text3;
A.RenderControl(output);
四、
1、AddAttributesToRender方法(为当前自定义控件容器标记增加属性,即对控件呈现HTML标记的最外层一级增加属性)
[ToolboxData("<{0}:AddAttributesToRenderCon
runat=server></{0}:AddAttributesToRenderCon
public class AddAttributesToRenderCon
WebControl
/// <summary>
重载TagKey属性,
使用<Table>替换掉默认的<Span>
/// </summary>
override HtmlTextWriterTag TagKey
{
get
{
return
HtmlTextWriterTag.Table;
}
AddAttributesToRender(HtmlTextWriter
writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Border,
"0px");
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,
"0px");
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing,
"0px");
base.AddAttributesToRender(writer);
}
protected override void RenderContents(HtmlTextWriter
output)
{
//输出三行内容
output.RenderBeginTag(HtmlTextWriterTag.Tr);
output.RenderBeginTag(HtmlTextWriterTag.Td);
output.Write("<a href='http://www.cnblogs.com' target='blank'
style='color:Blue;cursor:Hand;'>");
output.Write("【博客园】【CNBlog】");
output.Write("</a>");
output.RenderEndTag();
output.RenderEndTag();
output.RenderBeginTag(HtmlTextWriterTag.Tr);
output.RenderBeginTag(HtmlTextWriterTag.Td);
output.Write("<a href='http://www.csdn.net' target='blank'
style='color:Blue;cursor:Hand;'>");
output.Write("【中国软件开发网】【CSDN】");
output.Write("</a>");
output.RenderEndTag();
output.RenderEndTag();
output.RenderBeginTag(HtmlTextWriterTag.Tr);
output.RenderBeginTag(HtmlTextWriterTag.Td);
output.Write("<a href='http://blog.csdn.net/ChengKing' target='blank'
style='color:Blue;cursor:Hand;'>");
output.Write("【金鹰的博客】【ChengKing(ZhengJian)】");
output.Write("</a>");
output.RenderEndTag();
output.RenderEndTag();
}
2、CreateChildcontrols方法(一般用于创建组合控件)
3、EnsureChildControls方法(确定服务控件是否包含子控件,首先调用ChildControlsCreated检查是否包含,如果不包含则调用CreateChildControls方法)
4、RecreateChildControls方法(间接调用CreateChildControls方法。使其在设计模式下,可创建子控件。默认在设计模式下CreateChildControls方法是不执行的)
== false)
base.RecreateChildControls();
5、FindControl、FindControlExtend方法(在复合容器中查找子控件方法)
base.FindControl(id);
this.Page.FindControl(id);
this.FindControlExtend(id,this.Controls);
this.FindControlExtend(id,this.Page.Controls);
controls)
if(controls[i].ID == id)
Found = countrols[i];
break;
if(controls[i].Controls.Count > 0)
Found = FindControlExtend(id,controls[i].Controls);
if(Found !=null)break;
五、开发控件常用属性及接口
属性(数据类型bool,用于获取一个值,指示是否已创建服务器控件的子控件,功能为了避免CreateChildControls方法重复创建控件)
base.ChildControlsCreated == false)
ControlCollection,用于获取ControlCollection对象,表示指定服务器控件子控件集合.)
六、常用开发技巧
注:Bindable(
文件中不会智能提示此属性.
其中ID表示我们给他命令的ID;Unique表示服务端ID,ClientID表示客户端ID;
七、复合控件实例(由一个简单的文本框与正则表达式验证控件组成)
using System;
using System.Collections.Generic;
using
System.ComponentModel;
using System.Text;
using System.Web;
using
System.Web.UI;
using System.Web.UI.WebControls;
namespace KingControls
{
[DefaultProperty("TextBoxValue")]
[ToolboxData("<{0}:Field
runat=server></{0}:Field>")]
CompositeControl
{
lb;
tb;
RegularExpressionValidat
rev;
[Category("LabelTextBox")]
[Description("标签显示信息")]
string LabelTitle
{
get
{
this.EnsureChildControls();
return
this.lb.Text;
}
set
{
this.EnsureChildControls();
this.lb.Text =
value;
}
}
[Category("LabelTextBox")]
[Description("文本框显示文本")]
string TextBoxValue
{