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!
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:
- 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With