Asp.NET user control referenced by another / dll project has NULL properties

Trying to move a generic custom control to a new class library (also tried a new web project) so that other projects can use it, but its properties are always NULL when used in another project. Finding similar questions, unfortunately, does not help solve my problem.

I will register a new control in our web.base.config for Project-A

<add tagPrefix="Controls" namespace="Comp.UserWebControls.Controls" assembly="Comp.UserWebControls" /> 

Comp.UserWebControls is added as a link to the project in Project-A (I also tried to add the assembly dll as a link in Project-A, no difference)

The markup looks like

 <asp:Content ID="Content4" ContentPlaceHolderID="RightPlaceHolder" runat="server"> <Controls:uxMyControl ID="TestingThing" runat="server" /> 

The TestingThing control is created in the code behind, but all the properties are zero (shortcuts, text fields, etc.).

Usually, if the control was within the same project (Project-A), then I would register this element as follows:

 <add tagPrefix="Contols" tagName="uxMyControl" src="~/Controls/uxMyControl.ascx"/> 

And in Codebehind, the control object looks great, properties are created and work as expected.

I cannot do src="myPath" when using the assembly, so I think this has something to do with it.

How can I separate this control from a new assembly / project / solution so that other projects can use it.

Any help would be greatly appreciated!

+5
source share
1 answer

I do not think that you can take User Control (for example, a .ascx file) and put it in a class library for sharing among different applications.

Instead, you need to write Custom Control . This is basically a class (one .cs file not related to ascx) that comes from Control (or the Control option), for example, you can get it from DropDownList if you want to create your own drop-down list box).

Differences: https://msdn.microsoft.com/en-us/library/aa651710(v=vs.71).aspx

There is another MSDN article here: https://msdn.microsoft.com/en-us/library/aa479318.aspx , which discusses how to create a user control from a user control if you do not feel very comfortable with the idea of ​​this .

It describes the following steps:

  • Write your user control as usual using Visual Studio Designer.
  • Test it with a simple page before trying to expand it.
  • Deploy the application to precompile it.
  • Grab the user control assembly created during the deployment phase, and you are essentially done: you have your own control.
  • Finally, use user control in other applications the same way you always use user controls.

If you want to have a set of controls and don't want to have a separate DLL for each, then you can use a decompiler such as Reflector to see what was created and just copy / paste it into your class library together controls.

Then you should be able to register tagPrefix and the namespace, as you have already shown: <add tagPrefix="Controls" namespace="Comp.UserWebControls.Controls" assembly="Comp.UserWebControls" />

and you must be ready to go.

If the user control was not particularly complex, then I probably wanted to read about the clean code of Custom Controls and try and just write it so that instead of trying to capture and decompile the DLL.

I think the reason you return null values ​​is most likely related to the viewstate and page life cycle. This is always what I struggled with most of Webforms, especially with dynamically loaded controls, figuring out where in the life cycle you should load them. I seem to remember that if you added them dynamically to Page_Load , for example, they would always seem to work, but then values ​​would never be saved through callbacks, but then Page_Init worked fine.

So, I would say - create a user control, one class file for the user control, get rid of any traces of .ascx , and you should be good to go.

+2
source

All Articles