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

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

一 、打印一个文本(hello,world)

控件:

using  System;
using  System.Web.UI;

namespace  TestCustomControl
{
    
public class First:Control
    
{
        
protected override void Render(HtmlTextWriter writer)
        
{
            writer.Write(
"Hello ,world");
        }

    }

}

要点:

1。类要继承Control。

2。类要重写Render方法

3。HtmlTextWriter用于输出html代码

用法:

<% @ Page Language="C#" AutoEventWireup="true" CodeFile="First.aspx.cs" Inherits="TestCustomControl_First_First"  %>
<% @ Register TagPrefix="Surance" Assembly="First" 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:First  ID ="F1"  runat ="server"   />
    
</ div >
    
</ form >
</ body >
</ html >

你可能感兴趣的:(自定义控件学习笔记(一))