我正在尝试在其他项目中重复使用我的ASP用户控件,并遇到了问题。我已经在http://weblogs.asp.net/scottgu/archive/2005/08/28/423888.aspx上阅读了ScottGu的帖子,并试图遵循他的模式,但运行时我的控件的子控件没有被初始化。
我的设置: 我正在使用Visual Studio 2008 我已经创建了一个解决方案来保存我想要分发给其他几个解决方案的控件。该解决方案包含:
这背后的想法是Innermost包含功能,而Outer包含使Innermost -controls在特定框架中工作所需的任何特殊修改。
ClassLibrary有这样的代码(这是我做的一个演示设置,以确保它是一个ASP问题,而不是我正在做的其他事情):
using System;
namespace ClassLibrary {
public class LibraryClass {
public long getLong() {
return DateTime.Now.Ticks;
}
public string getString() {
return DateTime.Now.ToString();
}
}
}
Innermost引用库项目并且工作正常。这是此项目中UserControl的示例:
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="WebUserControl1.ascx.cs"
Inherits="Innermost.UserControls.WebUserControl1" %>
<asp:Label ID="testLabel" runat="server"/>
代码隐藏:
using System;
using System.Web.UI;
using ClassLibrary;
namespace Innermost.UserControls {
public partial class WebUserControl1 : UserControl {
protected void Page_Load(object sender, EventArgs e) {
LibraryClass c = new LibraryClass();
testLabel.Text = "Your number is " + c.getLong();
}
}
}
我在Innermost项目中有一个测试页面,它使用Innermost项目的用户控件,并使用以下语法注册它们:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test1.aspx.cs"
Inherits="Innermost.Test1" %>
<%@ Register TagPrefix="inner" TagName="Control1"
Src="~/UserControls/WebUserControl1.ascx" %>
<%@ Register TagPrefix="inner" TagName="Control2"
Src="~/UserControls/WebUserControl2.ascx" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Control 1:
<inner:Control1 ID="control1" runat="server" />
<br />
Control 2:
<inner:Control2 ID="control2" runat="server" />
</div>
</form>
</body>
</html>
外部项目引用Innermost和ClassLibrary;并包含UserControls,其中注册了Innermost控件:
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="TopControl.ascx.cs" Inherits="Outer.Control.TopControl" %>
<%@ Register TagPrefix="inner" Assembly="Innermost"
Namespace="Innermost.UserControls" %>
<inner:WebUserControl1 ID="Control1" runat="server"/>
<inner:WebUserControl2 ID="Control2" runat="server"/>
使用简单的代码隐藏: 使用系统; 使用System.Web.UI;
namespace Outer.Control {
public partial class TopControl : UserControl {
public bool ShowType1 { get; set; }
protected void Page_Load(object sender, EventArgs e) {
Control1.Visible = ShowType1;
Control2.Visible = !ShowType1;
}
}
}
当我在Innermost项目中运行测试页面时,我没有错误,一切正常。当我运行Outer项目时,我得到NullReferenceException
告诉我Innermost.UserControls.WebUserControlX
的子控件尚未设置。
堆栈跟踪:
[NullReferenceException: Object reference not set to an instance of an object.]
Innermost.UserControls.WebUserControl1.Page_Load(Object sender, EventArgs e) in c:\Test\ASP\Innermost\Innermost\UserControls\WebUserControl1.ascx.cs:9
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Control.LoadRecursive() +141
System.Web.UI.Control.LoadRecursive() +141
System.Web.UI.Control.LoadRecursive() +141
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
如果有人想仔细研究一下,我可以提供测试设置作为解决方案。
所以,最后,我的问题是:为什么控件的ChildControls没有按预期设置?即使我在外部控件的Init中运行CreateChildControls()
,也不会创建它们。我是否需要编译Innermost项目以便在Outer项目中使用?
非常感谢任何帮助!
答案 0 :(得分:7)
我之前没有尝试过这个设置,但我可以猜测发生了什么。 UserCotnrols旨在通过路径加载,而不是通过引用加载。您正尝试通过引用创建它们。请注意在内部项目中引用它们的方式与它与外部项目的不同之间的区别。我相信要让它工作,你需要参考.ascx文件的路径位置。
快速搜索显示如何在VS 2005中完成此操作,但VS 2008中的概念应该相同。您可以找到该信息here。
另一种选择是不使用设计图面,而是使用LoadControl方法加载控件,并将路径传递给.ascx文件。
修改强> 从我上面提到的链接中获取的重要一点是,您必须使.ascx文件可以访问您的外部应用程序。上面的链接通过使用预构建命令将.ascx文件复制到外部项目中的子文件夹来完成此操作。把内部项目想象成你要重新分配它。您不仅必须放弃程序集,还要放弃.ascx文件。要让用户使用它,他们必须引用.ascx文件并让他们的项目引用您的程序集。你也必须做同样的事情。
我希望这更清楚一点。