当我尝试使用自定义服务器控件(在 App_Code 目录中)时,全局命名空间出现问题

发布于 2024-09-29 10:50:10 字数 3113 浏览 4 评论 0原文

传统上我使用常规的 asp.net 网站(使用“文件”>“新网站”创建)。最近,我选择开发一个成熟的项目(使用“文件”>“新建项目”>“ASP.net Web 应用程序”创建)。

多年来我一直使用相同的自定义控件,没有发生任何事故。我只需创建新网站,将 CustomControls.cs 文件放在 App_Code 目录中,向 web.config 文件添加一行,然后我就可以使用所有自定义服务器控件。 当我在我的网络项目中尝试这样做时,出现以下错误

错误 225 类型或命名空间名称“DTF”无法 可以在全局命名空间中找到(是 你缺少一个程序集 参考?)D:[项目位置在 驱动器]\AgIn02.aspx.designer.cs

我的自定义控制文件看起来像这样

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace DTF.Web.UI
{

    public class IntOnlyBox : TextBox
    {
        private RequiredFieldValidator rfv;
        private ValidatorCalloutExtender vce;
        private AjaxControlToolkit.FilteredTextBoxExtender ftb;
        private string strInvalidMessage = "";
        private string strValidationGroup = "";

        public string ValidationGroup
        {
            get
            {
                return strValidationGroup;
            }
            set
            {
                strValidationGroup = value;
            }
        }


        public string InvalidMessage
        {
            get
            {
                return strInvalidMessage;
            }
            set
            {
                strInvalidMessage = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            rfv = new RequiredFieldValidator();
            rfv.ControlToValidate = this.ID;
            rfv.ErrorMessage = "<span style=\"color:black\"><b>Required Field Missing</b><br />" + this.InvalidMessage + "</span>";
            //rfv.ErrorMessage = this.InvalidMessage;
            rfv.ID = "rfv" + this.ID;
            rfv.Display = ValidatorDisplay.None;
            rfv.SetFocusOnError = true;
            rfv.EnableClientScript = true;
            rfv.ValidationGroup = this.ValidationGroup;

            vce = new AjaxControlToolkit.ValidatorCalloutExtender();
            vce.ID = "vce" + this.ID;
            vce.TargetControlID = "rfv" + this.ID;
            vce.Width = 300;

            ftb = new FilteredTextBoxExtender();
            ftb.ID = "ftb" + this.ID;
            ftb.TargetControlID = this.ID;
            ftb.FilterType = FilterTypes.Numbers;

            Controls.Add(rfv);
            Controls.Add(vce);
            Controls.Add(ftb);
        }

        protected override void Render(HtmlTextWriter w)
        {
            //w.Write(this.InvalidMessage);
            base.Render(w);
            rfv.RenderControl(w);
            vce.RenderControl(w);
            ftb.RenderControl(w);
        }
    }

}

我的 web.config 条目看起来像这样(在页面/控件区域)

<add tagPrefix="DTF" namespace="DTF.Web.UI" />

我已经尝试了我能想到的一切来让它工作和编译。自定义服务器控件的智能感知工作正常,只是无法编译。

我还收到错误“基类 包括字段“blanro”,但它 类型 (DTF.DateBoxFull) 不是 与控制类型兼容 (DTF.DateBoxFull)。”

知道如何解决这个问题,以及为什么它可以在常规 asp.net 网站中工作,但不能在 Web 项目中工作?

谢谢大家。

Traditionally I use the regular asp.net website (created using the File > New Website). Recently, I opted to work off of a full fledged project (created using File > New Project > ASP.net Web Application).

I've been using the same custom controls for years without incident. I simply create the new website, place my CustomControls.cs file in the App_Code directory, add one line to the web.config file and I can use all of my custom server controls.
When I try that with my web project I get the following error

Error 225 The
type or namespace name 'DTF' could not
be found in the global namespace (are
you missing an assembly
reference?) D:[Project Location On
Drive]\AgIn02.aspx.designer.cs

My custom control file looks like this

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace DTF.Web.UI
{

    public class IntOnlyBox : TextBox
    {
        private RequiredFieldValidator rfv;
        private ValidatorCalloutExtender vce;
        private AjaxControlToolkit.FilteredTextBoxExtender ftb;
        private string strInvalidMessage = "";
        private string strValidationGroup = "";

        public string ValidationGroup
        {
            get
            {
                return strValidationGroup;
            }
            set
            {
                strValidationGroup = value;
            }
        }


        public string InvalidMessage
        {
            get
            {
                return strInvalidMessage;
            }
            set
            {
                strInvalidMessage = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            rfv = new RequiredFieldValidator();
            rfv.ControlToValidate = this.ID;
            rfv.ErrorMessage = "<span style=\"color:black\"><b>Required Field Missing</b><br />" + this.InvalidMessage + "</span>";
            //rfv.ErrorMessage = this.InvalidMessage;
            rfv.ID = "rfv" + this.ID;
            rfv.Display = ValidatorDisplay.None;
            rfv.SetFocusOnError = true;
            rfv.EnableClientScript = true;
            rfv.ValidationGroup = this.ValidationGroup;

            vce = new AjaxControlToolkit.ValidatorCalloutExtender();
            vce.ID = "vce" + this.ID;
            vce.TargetControlID = "rfv" + this.ID;
            vce.Width = 300;

            ftb = new FilteredTextBoxExtender();
            ftb.ID = "ftb" + this.ID;
            ftb.TargetControlID = this.ID;
            ftb.FilterType = FilterTypes.Numbers;

            Controls.Add(rfv);
            Controls.Add(vce);
            Controls.Add(ftb);
        }

        protected override void Render(HtmlTextWriter w)
        {
            //w.Write(this.InvalidMessage);
            base.Render(w);
            rfv.RenderControl(w);
            vce.RenderControl(w);
            ftb.RenderControl(w);
        }
    }

}

my web.config entry looks like this (in the pages/controls area)

<add tagPrefix="DTF" namespace="DTF.Web.UI" />

I've tried everything I can think of to get this to work and compile. The intellisense for the custom server controls works fine, it simply won't compile.

I also get the error "The base class
includes the field 'blanro', but its
type (DTF.DateBoxFull) is not
compatible with the type of control
(DTF.DateBoxFull)."

Any idea how to fix this, and why it would work in the regular asp.net website but not in a web project?

Thanks Everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

杀手六號 2024-10-06 10:50:10

将自定义类编译为 .dll 并添加对项目的引用。

Compile your custom classes to a .dll and add a reference to your project.

纵山崖 2024-10-06 10:50:10

好吧,简单地回答一下,WAP 和网站有不同的方式来编译应用程序,因此您可能会看到这种行为。

Well, to answer it simply WAP and Web Site have a different way of compiling the applications and hence you may be seeing this behavior.

初心未许 2024-10-06 10:50:10

使用 WAP 时,最好不要将自定义控件放在 App_Code 中。相反,只需将该代码放在项目中的其他位置,您就应该能够毫无问题地使用页面中的控件。

When using a WAP, it's best not to put your custom control in App_Code. Instead, simply put that code somewhere else in your project, and you should be able to use the control from your pages without problems.

风流物 2024-10-06 10:50:10

WebApplication 项目中的 App_Code 目录不起作用。所有代码文件,无论其所在的目录如何,都被编译为单个程序集,并且该程序集放置在 bin 目录中。

只需将所有文件移出 App_Code,然后删除 App_Code

The App_Code directory in a WebApplication project has no effect. All code files, regardless of the directory in which it resides, are compiled into a single assembly, and that assembly is placed in the bin directory.

Just move all your files out of App_Code and then delete App_Code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文