难得有一片天

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

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

Posted on 2007-03-23 12:41  seyon  阅读(1641)  评论(6编辑  收藏  举报
INamingContainer接口
INamingContainer接口是组合控件需要继承的一个接口,但它具体有什么作用呢?

INamingContainer 是一个没有方法的标记接口。当控件在实现INamingContainer 时,ASP.NET 页框架可在该控件下创建新的命名范围,因此能够确保子控件在控件的分层树中具有唯一的名称。当复合控件公开模板属性,提供数据绑定或需要传送事件到子控件时,这是非常重要的。

简单的说就是为子控件id加一个前缀,前缀名就是父控件名称;比如在formview1下的模板列中加入一个label1,那么在生成的页面中,改label做为formview1的子控件,其id被改为了formview1_label1,作用是为了保证其id的唯一性

请看实例:
有这样一个组合控件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CommandButton
{
    [DefaultProperty(
"Text")]
    [ToolboxData(
"<{0}:CommandButton runat=server></{0}:CommandButton>")]
    
public class CommandButton : Control
    
{
        
public string Username
        
{
            
get
            
{
                
this.EnsureChildControls();
                
return ((TextBox)Controls[2]).Text;
            }

            
set
            
{
                
this.EnsureChildControls();
                ((TextBox)Controls[
2]).Text = value;
            }

        }


        
public string Password
        
{
            
get
            
{
                
this.EnsureChildControls();
                
return ((TextBox)Controls[5]).Text;
            }

            
set
            
{
                
this.EnsureChildControls();
                ((TextBox)Controls[
5]).Text = value;
            }

        }


        
protected override void CreateChildControls()
        
{
            
this.Controls.Add(new LiteralControl("<div style=border:5px inset #cccccc;background-color:#eeeeee;width:50%;padding:10px>"));

            
// Add Username
            this.Controls.Add(new LiteralControl("<b>Username:</b> "));
            
this.Controls.Add(new TextBox());
            
this.Controls.Add(new LiteralControl("<p>"));

            
// Add Password
            TextBox txtPass = new TextBox();
            
this.Controls.Add(new LiteralControl("<b>Password:</b> "));
            txtPass.TextMode 
= TextBoxMode.Password;
            
this.Controls.Add(txtPass);
            
this.Controls.Add(new LiteralControl("<p>"));

            
// Add Submit Button
            Button btnButton = new Button();
            btnButton.Text 
= "Login!";
            
this.Controls.Add(btnButton);
            
this.Controls.Add(new LiteralControl("</div>"));
        }

    }

}


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Trace ="true" %>

<%@ Register Assembly="CommandButton" Namespace="CommandButton" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
        
<cc1:CommandButton ID="CommandButton1" runat="server">
        
</cc1:CommandButton>

    
</form>    
</body>
</html>


在没有继承INamingContainer接口的情况下:

继承了INamingContainer接口:
public class CommandButton : Control, INamingContainer


 
ps:一个小技巧,在页面上引用该控件时,不用对其注册,需要以下两个步骤:
1.在AssemblyInfo.cs里面
using System.Web.UI;
[assembly: TagPrefix(
"SeyonControls.CommandButton""Seyon")]//SeyonControls.CommandButton是命名空间,Seyon是控件前面的tag

2.在web.config里面添加
<pages>
      
<controls>
        
<add tagPrefix="Seyon" assembly="CommandButton" namespace="SeyonControls.CommandButton"/>
      
</controls>
    
</pages>
这样在页面中就可以直接使用,不用每次都注册
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Trace ="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>

    
<form id="form1" runat="server">
        
<Seyon:CommandButton ID="CommandButton1" runat="server">
        
</Seyon:CommandButton>
        
    
</form>    
</body>
</html>