自定义控件学习笔记(二)--增加属性
1。要点
1)使用访问器,和写类是一样的
2。控件代码
using
System;
using System.Web.UI;
namespace TestCustomControl
... {
public class AddAttribute:Control
...{
private string text;
public string Text
...{
get...{return text;}
set...{text=value;}
}
protected override void Render(HtmlTextWriter writer)
...{
writer.WriteFullBeginTag("h1");
writer.Write(Text);
writer.WriteEndTag("h1");
}
}
}
using System.Web.UI;
namespace TestCustomControl
... {
public class AddAttribute:Control
...{
private string text;
public string Text
...{
get...{return text;}
set...{text=value;}
}
protected override void Render(HtmlTextWriter writer)
...{
writer.WriteFullBeginTag("h1");
writer.Write(Text);
writer.WriteEndTag("h1");
}
}
}
3。引用代码
<%
...
@ Page Language="C#" AutoEventWireup="true" CodeFile="AddAttribute.aspx.cs" Inherits="TestCustomControl_First_AddAttribute"
%>
<% ... @ Register TagPrefix="Surance" Assembly="AddAtrribute" Namespace="TestCustomControl" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//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:AddAttribute ID ="A1" runat ="server" Text ="Test,Surance" />
</ div >
</ form >
</ body >
</ html >
<% ... @ Register TagPrefix="Surance" Assembly="AddAtrribute" Namespace="TestCustomControl" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//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:AddAttribute ID ="A1" runat ="server" Text ="Test,Surance" />
</ div >
</ form >
</ body >
</ html >