Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I register the new control in our web.base.config to Project-A

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

The Comp.UserWebControls is added as a project reference in Project-A (I also tried adding the build dll as a reference in Project-A, no difference)

The markup looks like

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

In the code behind, the "TestingThing" control is instantiated, but all properties are null (Labels, textboxes, etc)

Normally, if the control was within the same project (Project-A) then i would register the control like this:

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

And in codebehind the control object looks fine, properties are instantiated and working as expected.

I cant do the src="myPath" when using an assembly so i think that has something to do with it.

How can i seperate out this control to a new assembly/project/solution so that other projects can use it.

Any help would be appreciated thanks!

like image 226
Jerrold Avatar asked Jun 19 '15 14:06

Jerrold


1 Answers

I don't think you can take a User Control (e.g. a .ascx file) and put it into a class library to be shared amongst different apps.

Instead, you need to write a Custom Control. This is basically a class (a single .cs file, no associated ascx) which derives from Control (or a variant of Control - for example, you could derive from DropDownList if you wanted to make your own custom dropdown list control).

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

There's another MSDN article here: https://msdn.microsoft.com/en-us/library/aa479318.aspx which discusses how you would create a Custom Control from a User Control if you aren't feeling very comfortable with the idea of it.

It outlines the steps as follows:

  1. Write your user control as you normally would, typically using the Visual Studio designer.
  2. Test it using a simple page before trying to deploy it.
  3. Deploy the application to precompile it.
  4. Grab the user control's assembly produced by the deployment step, and you're essentially done: You have your custom control.
  5. Finally, use your custom control in other apps the same way as you always use custom controls.

If you wanted to have a collection of controls and don't want a separate DLL for each, then you could use a decompiler such as Reflector to look at what's been generated and just copy/paste it into your class library of shared controls.

You should then be able to register the tagPrefix and namespace as you already showed: <add tagPrefix="Controls" namespace="Comp.UserWebControls.Controls" assembly="Comp.UserWebControls" />

and you should be ready to go.

If the user control wasn't particularly complicated, then I'd probably be tempted to read about about the pure code Custom Controls and try and just write it like that instead of trying to grab and decompile a DLL.

I think the reason you're getting null values back is most likely something to do with viewstate and page lifecycle. This is always the thing I struggled with most in Webforms, especially dynamically loaded controls, figuring out where in the lifecycle you're supposed to load them. I seem to recall that if you were adding them dynamically in Page_Load for example, they'd always seem to work but then never persist values across postbacks, but then Page_Init worked fine.

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

like image 90
bgs264 Avatar answered Oct 13 '22 13:10

bgs264