自定义控件学习笔记(三)--如何获取客户提交数据
1。要点
1)继承接口IPostBackDataHandler
2)重写LoadPostData
3)在LoadPostData中,通过postCollection[postDataKey]获取客户的提交
2。控件
usingSystem;
usingSystem.Web.UI;
usingSystem.Collections.Specialized;

namespaceTestCustomControl
...{
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">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<Surance:DealPostBackDataID="D1"runat="server"Text="TypeSomething"/>
<asp:ButtonID="B1"Text="Click"runat="server"OnClick="B1_Click"/>
</div>
</form>
</body>
</html>
后台:
protectedvoidB1_Click(objectsender,EventArgse)
...{
Response.Write(this.D1.Text);
}
3100

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



