繁体   English   中英

@aspx中的@ Reference Control,但在各自的aspx.cs中找不到类

[英]@ Reference Control in .aspx, but class not found in respective aspx.cs

希望标题至少有一定道理。

我今天第一次使用ASP.NET和C# - 在教程之后。 我很舒服地得到了这个 ; 但是,当我尝试实现时:

<%@ Reference Control="~/UserInfoBoxControl.ascx" %>

UserInfoBoxControl userInfoBoxControl = (UserInfoBoxControl)LoadControl("~/UserInfoBoxControl.ascx");
userInfoBoxControl.UserName = "John Doe";
userInfoBoxControl.UserAge = 78;
userInfoBoxControl.UserCountry = "Spain";
UserInfoBoxPH.Controls.Add(userInfoBoxControl);

我明白了

The type or namespace name 'UserInfoBoxControl' could not be found...

我的Java背景使得现在我要“进口”与该控件类using ,虽然我明白@ Reference做这件工作。 我通过删除本教程前面的代码尝试了一些基本的故障排除,但没有运气。 从我在其他地方读到的,代码看起来都没问题,所以我有点困惑,并且热衷于继续我的教程:)

谢谢!

编辑:

我仍然处于这个障碍,所以我认为最好只转储我的所有代码:

Default.aspx的

Default.aspx.cs (声明userInfoBoxControl时发生错误)

UserInfoBoxControl.ascx

UserInfoBoxControl.ascx.cs

您将在页面指令中导入名称空间。 您注册用户控件:

<%@ Register TagPrefix="uc1" TagName="UserInfoBox" Src="UserInfoBoxControl.ascx" %>

编辑:(从我的评论)引用命名空间ASP:

using ASP;

如果你正确地声明了一切,那么你的代码应该可行。

需要Register指令来引用页面,用户控件等中的控件。

相反, Reference指令 “表示位于某个虚拟路径的另一个用户控件,页面源文件或任意文件应该动态编译并链接到当前的ASP.NET文件(网页,用户控件或母版页)中声明了这个指令。“

这只是一个猜测,但你是否有可能在单个文件中声明你的cotrol(即C#代码不在一个单独的文件中)而你忘记了@ Control指令中的“className”属性?

编辑 :我已经看到你的代码,问题是文件名为“UserInfoBoxControl”,但类的名称是WebUserControl

public partial class WebUserControl : System.Web.UI.UserControl {

您的@Reference指令正在导入文件 “〜/ UserInfoBoxControl.ascx”,但类型名称是WebUserControl。
试试这个,它应该编译:

WebUserControl userInfoBoxControl = (WebUserControl)LoadControl("~/UserInfoBoxControl.ascx");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM