1. make sure your 服务器控件 has a namespace
2. compile the 服务器控件 cs file into a dll and copy it to the bin directory of your virtual directory
3. try something like the following in your aspx page:<%@ Register TagPrefix="my" Namespace="YourControlNamespace" Assembly="YourControlDLLNameWithoutTheExtension" %><my:YourControlClassName id="whatever" runat="server" />

解决方案 »

  1.   

    我都按上面做了,但还是有错,请上面的高手再指教:WebCustomControl1.cs文件:using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    namespace wdfx_test
    {
    public class WebCustomControl1 : WebControl
    {
    private string text;
    public string Text 
    {
    get
    {
    return text;
    } set
    {
    text = value;
    }
    }
    protected override void Render(HtmlTextWriter output)
    {
    output.Write("这是第一个自一义控件");
    }
    }
    }
    引用控件的文件:CustomControl.aspx为:<%@ Register TagPrefix="CC1" Namespace="wdfx_test" Assembly="customcontrols"%>
    <%@ Page language="c#" Codebehind="CustomControl.aspx.cs" AutoEventWireup="false" Inherits="wdfx_test.CustomControl" %>
    <HTML>
    <HEAD>
    <title>CustomControl</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="CustomControl" method="post" runat="server">
    <CC1:WebCustomControl1 runat="server" id="wc1" />
    </form>
    </body>
    </HTML>
    而且在BIN下也生成了customcontrols.dll
    但运行时出现以下错误:
     基类包括字段“wc1”,但其类型 (wdfx_test.WebCustomControl1) 与控件 (wdfx_test.WebCustomControl1) 的类型不兼容。源错误: 
    行 12:  <body MS_POSITIONING="GridLayout">
    行 13:  <form id="CustomControl" method="post" runat="server">
    行 14:  <CC1:WebCustomControl1 runat="server" id="wc1" />
    行 15:  </form>
    行 16:  </body>
     
      

  2.   

    are you defining it in multiple places? the following aspx works for me<%@ Register TagPrefix="CC1" Namespace="wdfx_test" Assembly="customcontrols"%>
    <HTML>
    <body MS_POSITIONING="GridLayout">
    <form id="CustomControl" method="post" runat="server">
    <CC1:WebCustomControl1 runat="server" id="wc1" />
    </form>
    </body>
    </HTML>