难得有一片天

success.aspx?para1="要有决心"+para2="要努力"+para3="要有机会"
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2007-03-21 16:01  seyon  阅读(784)  评论(2编辑  收藏  举报
1.自定义控件中的子控件
控件功能:创建SimpleRotator2和RotatorItem2 子控件,从SimpleRotator2中选择任意一个RotatorItem2 并显示其text值
<%@ Page Language="C#" %>
<%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="SimpleRotator2"%>

<html>
<head><title>DisplaySimpleRotator2.aspx</title></head>
<body>
//表明上包含三个子控件,实质上包含了七个子控件,另外四个是换行符,作为默认的文本控件LiteralContent
<myControls:SimpleRotator2
  
Runat="Server">//换行符一
  
<myControls:RotatorItem2
    
Text="First Item"
    Runat
="Server" />//换行符二
  
<myControls:RotatorItem2
    
Text="Second Item"
    Runat
="Server" />//换行符三
  
<myControls:RotatorItem2
    
Text="Third Item"
    Runat
="Server" />//换行符四
</myControls:SimpleRotator2>

</body>
</html>

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

namespace myControls {

    
public class SimpleRotator2 : Control {

        
public ArrayList Items = new ArrayList();

        
protected override void AddParsedSubObject( object parsedObject ) {//重写添加子控件事件
            
if ( parsedObject is RotatorItem2 )//判断控件类型,以剔除LiteralContent控件
                Items.Add( parsedObject );
        }


        
protected override void Render(HtmlTextWriter objTextWriter) {
            Random  objRandom 
= new Random();
            
int intRanIndex;
            RotatorItem2 objSelectedItem;
            
if ( Items.Count > 0 ) {
                intRanIndex 
= objRandom.Next(Items.Count);//取随机数,随机选择一个RotatorItem2 子控件
                objSelectedItem 
= (RotatorItem2)Items[intRanIndex];
                objTextWriter.Write( objSelectedItem.Text );
            }

        }

    }


    
public class RotatorItem2 : Control {
        
public string  Text;
    }


}

2.自定义控件构建器
在上面例子中每个子控件都需要有Runat="Server",这样显得很麻烦,因为子控件使用的是默认的控件构建器,所以需要这样声明,可以通过更换我们自己的构建器
<%@ Page Language="C#" %>
<%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="SimpleRotator3"%>

<html>
<head><title>DisplaySimpleRotator3.aspx</title></head>
<body>

<myControls:SimpleRotator3
  
Runat="Server">
  
<RotatorItem3 Text="First Item" />//没有了runat="server"
  
<RotatorItem3 Text="Second Item" />
  
<RotatorItem3 Text="Third Item" />
</myControls:SimpleRotator3>

</body>
</html>

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

namespace myControls {

    
// Declare Custom Control Builder
    public class SimpleRotator3ControlBuilder : ControlBuilder {
        
public override Type GetChildControlType( string TagName, IDictionary Attributes ) {//获取子控件的类型
            
if (TagName == "RotatorItem3")
                
return typeof(RotatorItem3);
   
            
return null;
        }

    }



    
 //把SimpleRotator3ControlBuilder代替原有的构建器,RotatorItem3已经有了runat="server"属性
    [ControlBuilderAttribute(typeof( SimpleRotator3ControlBuilder))]
    
public class SimpleRotator3 : Control {

        
public ArrayList Items = new ArrayList();

        
// Exclude Everything but RotatorItem controls
        protected override void AddParsedSubObject( object Obj ) {
            
if ( Obj is RotatorItem3 )
                Items.Add(Obj);
        }


        
protected override void Render( HtmlTextWriter objTextWriter ) {
            Random objRandom 
= new Random();
            
int intRanIndex;
            RotatorItem3 objSelectedItem;
 

            
if (Items.Count > 0)  {
                intRanIndex 
= objRandom.Next( Items.Count );
                objSelectedItem 
= (RotatorItem3)Items[intRanIndex];
                objTextWriter.Write( objSelectedItem.Text );
            }

        }

    }


    
// Declare Rotator Item
    public class RotatorItem3 : Control {
        
public string Text;
    }


}

以上代码均没有使用元数据,所以在项目中如果要引入到工具栏中,需要自行编译,然后引用其dll
如果在代码中使用了元数据,则将其引用到项目中时系统会根据元数据自动在工具栏创建这个控件,并从你所定义的元数据中获取其属性
如:
[DefaultPropertyAttribute("CardholderNameText")]
    [ToolboxData(
@"<{0}:CreditCardForm2 
    PaymentMethodText='信用卡类型' CreditCardNoText='信用卡卡号' 
    CardholderNameText='信用卡持有者姓名' SubmitButtonText = '提交'  
    runat='server'></{0}:CreditCardForm2>
")
    ]
为改控件添加了默认属性
[BrowsableAttribute(true)]
    [DescriptionAttribute(
"获取或设置信用卡卡号")]
    [DefaultValueAttribute(
"信用卡卡号")]
    [CategoryAttribute(
"Appearance")]
为控件属性设定属性