自定义控件学习笔记(八)--第一阶段学习成果总结
1。要点
1)制作了控件 AutoCounter,这个控件中间是一个文本框,左右各有2个按钮,来实现加减中间文本框数字的作用
2)用到了以前7节的全部知识
2。控件
usingSystem;
usingSystem.Web.UI;
usingSystem.Collections.Specialized;

namespaceTestCustomControl
...{
publicclassAutoCounter:Control,IPostBackDataHandler,IPostBackEventHandler
...{
publiceventEventHandlerDecrement;
publiceventEventHandlerIncreMent;
publiceventEventHandlerCountChanged;
publicintCount
...{
get
...{
intcount=0;
if(ViewState["mycount"]!=null)
count=(int)ViewState["mycount"];
returncount;
}
set
...{
ViewState["mycount"]=value;
}
}
publicboolLoadPostData(stringpostDataKey,NameValueCollectionpostCollection)
...{
inttemp=Count;
try
...{
Count=Convert.ToInt32(postCollection[postDataKey]);
}
catch(FormatException)
...{
Count=0;
}
return(temp!=Count);
}
publicvoidRaisePostDataChangedEvent()
...{
if(CountChanged!=null)
CountChanged(this,newEventArgs());
}
publicvoidRaisePostBackEvent(stringeventArgment)
...{
if(eventArgment=="dec")
...{
Count--;

if(Decrement!=null)
Decrement(this,newEventArgs());
}
elseif(eventArgment=="inc")
...{
Count++;
if(IncreMent!=null)
...{
IncreMent(this,newEventArgs());
}
}
}
protectedoverridevoidRender(HtmlTextWriterwriter)
...{
writer.WriteBeginTag("a");
PostBackOptionsopt=newPostBackOptions(this);
opt.Argument="dec";
writer.WriteAttribute("href","javascript:"+Page.ClientScript.GetPostBackEventReference(opt));
writer.Write(HtmlTextWriter.TagRightChar);
writer.Write("dec");
writer.WriteEndTag("a");
writer.WriteBeginTag("input");
writer.WriteAttribute("type","text");
writer.WriteAttribute("name",UniqueID);
if(ID!=null)
writer.WriteAttribute("id",ClientID);
writer.WriteAttribute("value",Count.ToString());
writer.WriteAttribute("size","3");
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEndTag("input");

writer.WriteBeginTag("a");
opt=newPostBackOptions(this);
opt.Argument="inc";
writer.WriteAttribute("href","javascript:"+Page.ClientScript.GetPostBackEventReference(opt));
writer.Write(HtmlTextWriter.TagRightChar);
writer.Write("Inc");
writer.WriteEndTag("a");

}
}
}
3。用法
前台

<%...@PageLanguage="C#"AutoEventWireup="true"CodeFile="AutoCounter.aspx.cs"Inherits="TestCustomControl_First_AutoCounter"%>
<%...@RegisterAssembly="AutoCounter"TagPrefix="Surance"Namespace="TestCustomControl"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<Surance:AutoCounterID="A1"Count="3"OnDecrement="A1_OnDecrement"OnIncreMent="A1_OnIncrement"OnCountChanged="A1_OnCountChanged"runat="server"/>
</div>
</form>
</body>
</html>
后台
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
publicpartialclassTestCustomControl_First_AutoCounter:System.Web.UI.Page
...{
protectedvoidPage_Load(objectsender,EventArgse)
...{
}
protectedvoidA1_OnDecrement(objectsender,EventArgse)
...{
Response.Write("Counteris"+this.A1.Count.ToString());
}
protectedvoidA1_OnIncrement(objectsender,EventArgse)
...{
Response.Write("Counteris"+this.A1.Count.ToString());
}
protectedvoidA1_OnCountChanged(objectsender,EventArgse)
...{
Response.Write("Counteris"+this.A1.Count.ToString());
}
}
3100

被折叠的 条评论
为什么被折叠?



