namespace Haoze.Web.UI.Controls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
public class MyControl : WebControl
{
[TypeConverter(typeof(MyTypeConverter))]
public MyType Type
{
get;
set;
} protected override void RenderContents(HtmlTextWriter output)
{
output.RenderBeginTag(HtmlTextWriterTag.Span);
output.Write("Type is:");
if (Type != null && Type.Type != null)
{
output.Write(Type.Type.AssemblyQualifiedName);
}
output.RenderEndTag();
}
}
}[DefaultProperty("Text")]
[ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
是定义的什么,有谁知道这方面的资料
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
public class MyControl : WebControl
{
[TypeConverter(typeof(MyTypeConverter))]
public MyType Type
{
get;
set;
} protected override void RenderContents(HtmlTextWriter output)
{
output.RenderBeginTag(HtmlTextWriterTag.Span);
output.Write("Type is:");
if (Type != null && Type.Type != null)
{
output.Write(Type.Type.AssemblyQualifiedName);
}
output.RenderEndTag();
}
}
}[DefaultProperty("Text")]
[ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
是定义的什么,有谁知道这方面的资料
http://msdn.microsoft.com/zh-cn/library/system.attribute.aspx
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
//获取类上的属性类
foreach (object obj in typeof(TestClass).GetCustomAttributes(false))
{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取字段属性类
foreach (object obj in typeof(TestClass).GetField("_aa").GetCustomAttributes(typeof(Test1), true))
{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取属性属性类
foreach (object obj in typeof(TestClass).GetProperty("aa").GetCustomAttributes(typeof(Test1), true))
{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取方法属性类
foreach (object obj in typeof(TestClass).GetMethod("test").GetCustomAttributes(typeof(Test1), true))
{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取字事件性类
foreach (object obj in typeof(TestClass).GetEvent("onTest").GetCustomAttributes(typeof(Test1), true))
{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
Console.Read();
}
} //测试属性类,传入string
public class Test1 : System.Attribute
{
string strName = "";
public Test1() { }
public Test1(string str)
{
strName = str;
}
public override string ToString()
{
return strName;
}
}
//测试属性类,传入int
public class Test2 : Attribute
{
int _f;
public Test2(int f) { _f = f; }
public override string ToString()
{
return _f.ToString();
}
} //给class测试
[Test1("jinjazz for TestClass")]
[Test2(12356)]
public class TestClass
{
//给字段和属性测试
[Test1("jinjazz for _aa字段 ")]
public string _aa = "";
[Test1("jinjazz for aa 属性")]
public string aa
{
get { return _aa; }
} //给方法和事件测试
[Test1("jinjazz for test 方法")]
public void test() { }
[Test1("jinjazz for onTest 事件")]
public event System.EventHandler onTest;
}
}
定义一个自定义控件,属于服务器端控件
类似的DefaultEvent可以表示一个控件默认事件:
如你双击form他会默认到onLoad事件中,双击button,他会默认到onclick事件中。一个简单的测试,你添加一个windows窗体1叫Form1,然后增加如下代码 [DefaultEvent("FormClosed")]
public partial class Form1 : Form然后新建一个窗体2,继承自窗体1public partial class Form2 : Form1然后把Form2切换到设计器的模式,然后双击Form2的工作区域,会直接定位到代码的private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{ }而不是先前的
private void Form2_Load(object sender, EventArgs e)
{
}
[ToolboxData(" <{0}:MyControl runat=server> </{0}:MyControl>")]
是定义的什么,有谁知道这方面的资料 定义控件属性,[DefaultProperty("Text")]:指定控件的默认属性,就是当你选中控件时再点属性面板,光标为蓝色的属性
这里设置text为默认属性[ToolboxData(" <{0}:MyControl runat=server> </{0}:MyControl>")]:表示当你将此控件注册到工具箱里
就象系统自带的控件一样可以直接拖放时,系统自动生成的标记名称,就象你拖一个label到页面,它自动生成
<asp:Label ID="Label1"></asp:Label>一样。
这对这两条解释,想了解更多Attributes请参看msdn