自定义控件学习笔记(四)

自定义控件学习笔记(三)--如何获取客户提交数据

1。要点

1)继承接口IPostBackDataHandler
2)重写LoadPostData
3)在LoadPostData中,通过postCollection[postDataKey]获取客户的提交

2。控件

using System;
using System.Web.UI;
using System.Collections.Specialized;


namespace TestCustomControl
... {
publicclassDealPostBackData:Control,IPostBackDataHandler
...{
privatestringtext="";

publicstringText
...{
get...{returntext;}
set...{text=value;}
}


protectedoverridevoidRender(HtmlTextWriterwriter)
...{
writer.WriteBeginTag(
"input");
writer.WriteAttribute(
"name",UniqueID);

if(ID!=null)
...{
writer.WriteAttribute(
"id",ClientID);


}


if(text.Length>0)
...{
writer.WriteAttribute(
"value",text);


}


writer.Write(HtmlTextWriter.TagRightChar);

writer.WriteEndTag(
"input");


}


publicboolLoadPostData(stringpostDataKey,NameValueCollectionpostCollection)
...{
text
=postCollection[postDataKey];
returnfalse;
}


publicvoidRaisePostDataChangedEvent()
...{
}

}

}

3。用法

前台
 
 
<% ... @PageLanguage="C#"AutoEventWireup="true"CodeFile="DealPostBackData.aspx.cs"Inherits="TestCustomControl_First_DealPostBackData" %>
<% ... @RegisterAssembly="DealPostBackData"TagPrefix="Surance"Namespace="TestCustomControl" %>
<! DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//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 >
< Surance:DealPostBackData ID ="D1" runat ="server" Text ="TypeSomething" />
< asp:Button ID ="B1" Text ="Click" runat ="server" OnClick ="B1_Click" />
</ div >
</ form >
</ body >
</ html >

后台:

protected void B1_Click( object sender,EventArgse)
... {
Response.Write(
this.D1.Text);
}

你可能感兴趣的:(html,Web,UI,XHTML,asp)