木子屋 Dnawo's BLOG

cs页中引用的命名空间无法在aspx页中使用的一点解释

👤 dnawo 📅 2008-09-18 👁 5781 👍 0 💬 0 🔄 本站原创
在我新建的类库中有个MzwuCom.Common命名空间,其下有个Input类,代码为:

using System;
using System.Collections.Generic;
using System.Text;

namespace MzwuCom.Common
{
    public class Input
    {
        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="strTemp"></param>
        /// <param name="length"></param>
        public static string CutStr(string strTemp,int length)
        {
            if (length > strTemp.Length)
                return strTemp.Substring(0);
            else
                return strTemp.Substring(0, length);
        }
    }
}

然后我在站点中添加了对这个类库项目的引用,并在Default.aspx.cs中使用Input.CutStr:

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
[color=Brown]using MzwuCom.Common;[/color]

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write([color=Brown]Input.CutStr("abcdef",2)[/color]);
    }
}

运行正常,一切看起来挺正常的,下边我们试试看在aspx页中使用Input.CutStr:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" TraceMode="SortByCategory" Trace="false" %>
<!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>Mzwu.Com</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <% Response.Write([color=Brown]Input.CutStr("abcdef", 2)[/color]); %>
    </div>
    </form>
</body>
</html>

运行出错:当前上下文中不存在名称“Input”!Why?问题暂且放在一边,我们做个让人惊奇的小实验:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" TraceMode="SortByCategory" Trace="false" %>
<!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>Mzwu.Com</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    当前页面类型:<% Response.Write(this.GetType()); %><br />
    父类型:<% Response.Write(this.GetType().BaseType); %>
    </div>
    </form>
</body>
</html>

运行结果:

当前页面类型:ASP.default_aspx
父类型:_Default

My God!这说明在运行的时候aspx页被动态生成为_Default类的一个子类ASP.default_aspx[1]!OK,那这就不难解释为什么在aspx页中不能使用Input.CutStr了[2],如果一定非要使用的话可改成如下形式:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" TraceMode="SortByCategory" Trace="false" %>
<!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>Mzwu.Com</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <% Response.Write([color=Brown]MzwuCom.Common.Input.CutStr("abcdef", 2)[/color]); %><br />
    </div>
    </form>
</body>
</html>


[1]aspx中第一行:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" TraceMode="SortByCategory" Trace="false" %>
这已经表明aspx将会是_Default的子类。
[2].父类中引用的命名空间如果在子类中要使用必须重新引用。

2008-09-18补充:上边的问题也只在代码分离时会出现,不使用代码分离时运行正常。

<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="[color=Brown]MzwuCom.Common[/color]" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write([color=Brown]Input.CutStr("abcdef",2)[/color]);
    }
</script>

<!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>Mzwu.Com</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <% Response.Write([color=Brown]Input.CutStr("abcdef", 2)[/color]); %><br />
    </div>
    </form>
</body>
</html>


参考文章:

1.aspx页面在运行的时候会自动产生当前页面类的一个子类

评论(0)

暂无评论。

计算题
评论需审核通过后显示
← 上一篇 使用trace跟踪调试ASP.NET页面 下一篇 → C#错误处理类型总结