Asp.NET 从另一个 project/dll 引用的用户控件具有 NULL 属性
Asp.NET user control referenced from another project/dll has NULL properties
试图将一个普通的自定义控件移动到一个新的class库中(也尝试了一个新的web项目),以便其他项目可以使用它,但是在另一个项目中使用时它的属性总是NULL。不幸的是,搜索类似问题无助于解决我的问题。
我将我们web.base.config中的新控件注册到Project-A
<add tagPrefix="Controls" namespace="Comp.UserWebControls.Controls" assembly="Comp.UserWebControls" />
Comp.UserWebControls被添加为Project-A中的项目引用(我也尝试将构建dll添加为Project-A中的引用,没有区别)
标记看起来像
<asp:Content ID="Content4" ContentPlaceHolderID="RightPlaceHolder" runat="server">
<Controls:uxMyControl ID="TestingThing" runat="server" />
在后面的代码中,实例化了 "TestingThing" 控件,但所有属性(标签、文本框等)都为空
通常,如果控件在同一个项目 (Project-A) 中,那么我会像这样注册控件:
<add tagPrefix="Contols" tagName="uxMyControl" src="~/Controls/uxMyControl.ascx"/>
并且在代码隐藏中,控制对象看起来很好,属性已实例化并按预期工作。
我无法在使用程序集时执行 src="myPath"
所以我认为这与它有关。
如何将此控件分离到一个新的 assembly/project/solution 以便其他项目可以使用它。
如有任何帮助,我们将不胜感激!
我认为您不能将 User Control
(例如 .ascx 文件)放入 class 库中以在不同应用程序之间共享。
相反,您需要写一个Custom Control
。这基本上是一个 class(单个 .cs 文件,没有关联的 ascx)派生自 Control
(或 Control
的变体 - 例如,您可以派生自 DropDownList
如果您想制作自己的自定义下拉列表控件)。
差异:https://msdn.microsoft.com/en-us/library/aa651710(v=vs.71).aspx
此处还有另一篇 MSDN 文章:https://msdn.microsoft.com/en-us/library/aa479318.aspx,其中讨论了如果您对用户控件的想法不太满意,如何从用户控件创建自定义控件。
大致步骤如下:
- Write your user control as you normally would, typically using the
Visual Studio designer.
- Test it using a simple page before trying to
deploy it.
- Deploy the application to precompile it.
- Grab the user
control's assembly produced by the deployment step, and you're
essentially done: You have your custom control.
- Finally, use your
custom control in other apps the same way as you always use custom
controls.
如果您想要一组控件并且不希望每个控件都有一个单独的 DLL,那么您可以使用反编译器(例如 Reflector)来查看已生成的内容,然后 copy/paste 将其放入您的class 共享控件库。
然后您应该能够注册 tagPrefix 和命名空间,正如您已经展示的那样:
<add tagPrefix="Controls" namespace="Comp.UserWebControls.Controls" assembly="Comp.UserWebControls" />
你应该准备好了。
如果用户控件不是特别复杂,那么我可能会想阅读有关纯代码的内容 Custom Controls
并尝试像这样编写它,而不是尝试抓取和反编译一个DLL.
我认为您返回空值的原因很可能与视图状态和页面生命周期有关。这一直是我在 Webforms 中最纠结的事情,尤其是动态加载的控件,弄清楚你应该在生命周期中的什么地方加载它们。我似乎记得,例如,如果您在 Page_Load
中动态添加它们,它们似乎总是有效,但不会在回传中保留值,但 Page_Init
工作正常。
所以我想说 - 创建一个自定义控件,一个用于用户控件的 class 文件,去掉任何 .ascx
的痕迹,你应该可以开始了。
试图将一个普通的自定义控件移动到一个新的class库中(也尝试了一个新的web项目),以便其他项目可以使用它,但是在另一个项目中使用时它的属性总是NULL。不幸的是,搜索类似问题无助于解决我的问题。
我将我们web.base.config中的新控件注册到Project-A
<add tagPrefix="Controls" namespace="Comp.UserWebControls.Controls" assembly="Comp.UserWebControls" />
Comp.UserWebControls被添加为Project-A中的项目引用(我也尝试将构建dll添加为Project-A中的引用,没有区别)
标记看起来像
<asp:Content ID="Content4" ContentPlaceHolderID="RightPlaceHolder" runat="server">
<Controls:uxMyControl ID="TestingThing" runat="server" />
在后面的代码中,实例化了 "TestingThing" 控件,但所有属性(标签、文本框等)都为空
通常,如果控件在同一个项目 (Project-A) 中,那么我会像这样注册控件:
<add tagPrefix="Contols" tagName="uxMyControl" src="~/Controls/uxMyControl.ascx"/>
并且在代码隐藏中,控制对象看起来很好,属性已实例化并按预期工作。
我无法在使用程序集时执行 src="myPath"
所以我认为这与它有关。
如何将此控件分离到一个新的 assembly/project/solution 以便其他项目可以使用它。
如有任何帮助,我们将不胜感激!
我认为您不能将 User Control
(例如 .ascx 文件)放入 class 库中以在不同应用程序之间共享。
相反,您需要写一个Custom Control
。这基本上是一个 class(单个 .cs 文件,没有关联的 ascx)派生自 Control
(或 Control
的变体 - 例如,您可以派生自 DropDownList
如果您想制作自己的自定义下拉列表控件)。
差异:https://msdn.microsoft.com/en-us/library/aa651710(v=vs.71).aspx
此处还有另一篇 MSDN 文章:https://msdn.microsoft.com/en-us/library/aa479318.aspx,其中讨论了如果您对用户控件的想法不太满意,如何从用户控件创建自定义控件。
大致步骤如下:
- Write your user control as you normally would, typically using the Visual Studio designer.
- Test it using a simple page before trying to deploy it.
- Deploy the application to precompile it.
- Grab the user control's assembly produced by the deployment step, and you're essentially done: You have your custom control.
- Finally, use your custom control in other apps the same way as you always use custom controls.
如果您想要一组控件并且不希望每个控件都有一个单独的 DLL,那么您可以使用反编译器(例如 Reflector)来查看已生成的内容,然后 copy/paste 将其放入您的class 共享控件库。
然后您应该能够注册 tagPrefix 和命名空间,正如您已经展示的那样:
<add tagPrefix="Controls" namespace="Comp.UserWebControls.Controls" assembly="Comp.UserWebControls" />
你应该准备好了。
如果用户控件不是特别复杂,那么我可能会想阅读有关纯代码的内容 Custom Controls
并尝试像这样编写它,而不是尝试抓取和反编译一个DLL.
我认为您返回空值的原因很可能与视图状态和页面生命周期有关。这一直是我在 Webforms 中最纠结的事情,尤其是动态加载的控件,弄清楚你应该在生命周期中的什么地方加载它们。我似乎记得,例如,如果您在 Page_Load
中动态添加它们,它们似乎总是有效,但不会在回传中保留值,但 Page_Init
工作正常。
所以我想说 - 创建一个自定义控件,一个用于用户控件的 class 文件,去掉任何 .ascx
的痕迹,你应该可以开始了。