ASP.NET控件开发学习笔记--第二回 又见Hello World

 

第二回 又见Hello World

上次讲到有没有什么方法可以不直接编译DLL就能测试控件。ASP.NET 2.0增加了这个新功能。我们可以使用 ASP.NET 动态编译功能对页中的控件进行测试,而无需将控件编译为程序集。ASP.NET 能动态编译 ASP.NET 网站根目录(虚拟目录)下 App_Code 目录中放置的代码。这样就可以从页访问 App_Code 中源文件中的类,而无需将其手动编译为程序集。如果将控件的源文件放入了 App_Code 目录,则对控件的代码所做的更改能立即在使用这些控件的页中反映出来。

好!马上试试,为了让每个例子的代码互不干扰,这里对前面控件进行一些简单地修改。首先在虚拟目录下新建一名为“App_Code”的文件夹,在新文件夹内新建一“SecondControl2.cs”文件,输入如下代码:

2-1代码1SecondControl2.cs代码

 

using System;
using System.Web.UI;
namespace MyControl2
{
    
public class SimpleControl:Control
    {
        
protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(
"<a href='http://cgbluesky.blog.163.com' target=_blank>HelloWorld!</a>");
        }
    }
}
 

现在编写aspx文件直接访问这个未经编译的文件。回到虚拟目录(网站根目录)下,新建一名为“SecondHelloWorld2.aspx”的文件,用记事本打开,在其中输入如下代码:

2-1代码2SecondHelloWorld2.aspx代码

<%@Register TagPrefix="CG" Namespace="MyControl2"%>
<html>
    
<body>
        
<CG:SimpleControl runat="server" />
    
</body>
</html>
 

把这段代码跟“例1-3代码3”进行对比,我们发现在第一行少了Assembly属性,也就是说没有指定DLL文件。在浏览器内运行SecondHelloWorld2.aspx文件,我们发现跟前面的例子效果一样,但不再需要编译DLL,真爽啊!方便得多了。

可是方便还未达到完美的程度,您可能会想,一个网站可能会有上百个页面需要使用到一个控件,需要在每一页中都加入这么一句:

<%@Register TagPrefix="CG" Namespace="MyControl" Assembly="SecondControl"%>

虽然可以拷贝,但还是很麻烦啊!有没有什么解决方案啊?呵呵,只要配置Web.configOK了!我们利用例1-3来进行演示。在虚拟目录下新建一个“Web.config”文件,用记事本打开,并在其中输入如下代码:

2-2代码1Web.config代码

<?xml version="1.0"?>
<configuration>
  
<system.web>    
   
<pages>
     
<controls>
       
<add tagPrefix="CG" namespace="MyControl" assembly="SecondControl">
       
</add>
     
</controls>
   
</pages>
  
</system.web>
</configuration>
 

在这个xml文件中察看<add>标签的内容,可以发现和

<%@Register TagPrefix="CG" Namespace="MyControl" Assembly="SecondControl"%>

基本一样,它意味着给无需在每个页面注册就能够直接使用控件。下面来试试,打开“例1-3代码3”中的SecondHelloWorld.aspx文件,删除第一行注册控件的代码,最后代码如下:

2-2代码2SecondHelloWorld3.aspx代码

<html>
    
<body>
        
<CG:SimpleControl runat="server" />
    
</body>
</html>

 

把文件另存为“SecondHelloWorld3.aspx”,在浏览器中运行它,可以看到,结果一样。这里需要注意,如果在Web.config文件中的<add>标签内缺少assembly属性,则 ASP.NET 会推断该程序集是从 App_Code 目录中的源文件动态编译而来。关于这一点大家可以自己动手试试。

相信很多人愿意使用Visual Studio开进行ASP.NET的开发,如果能让自己制作的ASP.NET控件显示在Visual Studio工具箱那该是一件多么美好的事啊!这一点完全可以做到,现在就可以把你的控件加进Visual Studio内,我现在使用的是Visual Studio 2008,其它版本应该也没什么问题。

打开Visual Studio新建一个【ASP.NET应用程序】,找到【常规】工具箱,在空白处右击鼠标,在弹出的菜单中单击【选择项】(图2-1所示)打开【选择工具箱项】窗口(这个窗口打开时间非常漫长,要半分钟,VS2008并非传说中的那么快)。


 

在【选择工具箱项】窗口中单击【浏览】按钮,在打开文件对话框中选择前面所编译的DLL文件“SecondControl.dll”(如图2-2所示),单击【确定】按钮退出。此时可以发现【常规】工具箱中已经多出了一个SimpleControl控件。


 

哈哈,太爽了,就这么简单。拖动此控件到设计窗体中,按Ctrl+F5运行程序,可以发现运行结果和前面的例子一样。现在切换到源窗口查看源代码,我们只关注刚才添加控件的部分,如下:

......

<%@ Register assembly="SecondControl" namespace="MyControl" tagprefix="cc1" %>

......

        <cc1:SimpleControl ID="SimpleControl1" runat="server">

        </cc1:SimpleControl>

......

可以看到,VS2008自动给控件注册,并把前缀命名为默认的cc1,如果您对cc1这个名字感到很不满意,可以给它另起名字,但需要在源码中给命名空间添加一个特性(attribute)。打开“SecondControl.cs”文件,并更改代码如下:

2-3代码1SecondControl3.cs代码

using System;
using System.Web.UI;
[assembly:TagPrefix(
"MyControl""CG")]
namespace MyControl
{
    
public class SimpleControl3:Control
    {
        
protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(
"<a href='http://cgbluesky.blog.163.com' target=_blank>HelloWorld!</a>");
        }
    }
}
 

把文件另存为SecondControl3.cs,这一段代码把控件名改为SimpleControl3,并添加了一句话:

[assembly:TagPrefix("MyControl", "CG")]

其中,MyControl表示命名空间,CG表示它使用的前缀。下面对它进行编译,打开CompileSecondControl.bat文件(不要告诉我你已经把它删了,如果是这样就参照“例1-3代码2”),并修改代码如下:

2-3代码2CompileSecondControl3.bat代码

set indir=H:\ASP\SecondControl3.cs
set outdir
=H:\ASP\bin\SecondControl3.dll
csc 
/t:library /out:%outdir% %indir%
pause
 

把文件另存为CompileSecondControl3.bat,并双击编译SecondControl.cs,查看bin目录是否生成了程序集SecondControl3.dll。成功后,再次打开Visral Studio,并打开前面所建立的ASP.NET应用程序,按前面所述步骤把它添加进工具箱。然后删除前面的那个控件,把新建的控件添加进设计窗体,切换到源代码窗口,现在代码变了,我们只关注添加控件部分:

......

<%@ Register assembly="SecondControl3" namespace="MyControl" tagprefix="CG" %>

......

        <CG:SimpleControl3 ID="SimpleControl31" runat="server">

        </CG:SimpleControl3>

......

现在把它跟前面的assembly特性代码对比一下,您应该知道这句特性代码的作用是什么了。


posted @ 2008-02-22 17:05  abatei  阅读(1499)  评论(4编辑  收藏  举报