Custom control becomes generic "UserControl" and not its actual type in Designer class
Asked Answered
P

2

11

I have a custom control in ASP.NET (VB.NET in code behind), defined with an ASCX:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="MyControl.ascx.vb" Inherits="Mynamespace.Controls.MyControl" %>

<!-- some html and other custom controls-->

And in code behind:

Namespace Controls

    Public Class MyControl
        Inherits System.Web.UI.UserControl

This is set in a library. A different project uses that control in a page:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="mypage.aspx.vb" 
    Inherits="myproject.mypage" culture="auto" meta:resourcekey="Page" uiculture="auto" 
    Transaction="RequiresNew" MasterPageFile="Mynamespace.Master" 
    Theme="ThemeBase2" StylesheetTheme="ThemeBase2" %>

<%@ Register tagprefix="Controls" tagname="MyControl" src="../Controls/MyControl.ascx" %>

<%-- some asp.net --%>

<Controls:MyControl ID="mycontrol1" runat="server" 
                    MyCustomProperty="value" />

However, when I build, I get an error saying

'MyCustomProperty' is not a member of 'System.Web.UI.UserControl'.

And in the designer.vb page I see:

Protected WithEvents mycontrol1 As Global.System.Web.UI.UserControl

How do I ensure it becomes:

Protected WithEvents mycontrol1 As Global.Mynamespace.Controls.MyControl

?

Phocine answered 14/12, 2015 at 22:25 Comment(1)
I'm not sure referencing user controls (as opposed to regular server controls) in another project is a supported scenario.Dannadannel
C
1

Your ascx file is not accessible because it is in a library

You need to save the ascx file as an embedded resource of your library and load it as extern resource in your web application.

You can consult this link for more informations.

If you want share yours controls, i advise to create UserControl instead of CustomControl. Unfortunately, there is more work because the designer is not usable

Conscientious answered 24/12, 2015 at 11:12 Comment(0)
I
2

Make sure that MyControl is defined inside Global.Mynamespace.Controls.MyControl. It inherits this namespace, but it seems that this is supposed to be the namespace in which it is defined. Also, make sure that MyCustomProperty is defined, of course.

Indiscrimination answered 17/12, 2015 at 21:3 Comment(3)
I tried specifying Global.Mynamespace.Controls.MyControl explicitely in the code behind of the control, no dice.Phocine
@Phocine I have edited my answer. I hope this helps.Indiscrimination
I can't do that. That's my page's code behind. I need that. And besides, I have other custom controls.Phocine
C
1

Your ascx file is not accessible because it is in a library

You need to save the ascx file as an embedded resource of your library and load it as extern resource in your web application.

You can consult this link for more informations.

If you want share yours controls, i advise to create UserControl instead of CustomControl. Unfortunately, there is more work because the designer is not usable

Conscientious answered 24/12, 2015 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.