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

自定义控件学习笔记(三)--如何设置ID和Name

1。要点

1)注意ID的设置,为了允许客户自己设置,应写(If(ID!=null))。。。
2)一般情况下,ID为ClientID
3)一般情况下,name为UniqueID

2。控件

using System;
using System.Web.UI;

namespace TestCustomControl
... {
publicclassIDAndName:Control
...{
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");


}

}

}

3。用法

<% ... @PageLanguage="C#"AutoEventWireup="true"CodeFile="IDAndName.aspx.cs"Inherits="TestCustomControl_First_IDAndName" %>
<% ... @RegisterTagPrefix="Surance"Assembly="IDAndName"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:IDAndName ID ="I1" runat ="server" Text ="Test,Surance" />
</ div >
</ form >
</ body >
</ html >

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