Tech Off Thread

3 posts

ASP.Net User control with codebehind not working

Back to Forum: Tech Off
  • User profile image

    I have several pages that use the same collection of controls, an Img, and a couple of labels. I figured I could write a Usercontrol that would wrap everything up. I also wanted to programmatically read properties of this control to tell me whether the image was horizontal (width>height) or vertical (square counts as vertical).

    So I lay my controls into the ASCX file, and then go into the codebehind create some public (readonly properties). No compile errors.
    In my ascx file at the top I put this:
    <%@ Control Inherits="uCtrlPreview" src="uCtrlPreview.vb" %>

    I go into my first page (page1.aspx), give it a register tag, go down, put in my usercontrol tag. So far so good.
    <%@ Register TagPrefix="UserControl" TagName="PrevImg" Src="previewImg.ascx"
    USERCONTROL:PrevImg id="Preview1" runat="Server"></USERCONTROL:PrevImg>

    I go into my page's (page1.aspx) codebehind and put this line:
    Protected WithEvents Preview1 As uCtrlPreview

    I get this message:
    The base class includes the field 'Preview1', but its type
    (ICUser.uCtrlPreview) is not compatible with the type of control

    Any ideas?

  • User profile image

    I had the same problem today, but it is in C#.

    all you need to do is declare the control with it's name instead of declaring it as a user control. For instance, in my case:

    public class sniff : System.Web.UI.UserControl {

    code here


    is then instantiated on my page thus:

    protected sniff ctrl_Sniff;

    I had tried to do it this way unsuccessfully:

    protected System.Web.UI.UserControl ctrl_Sniff;


    ~ Knute

  • User profile image

    Nah, that's how I was instantiating it, as a new sniff.

    Now, in the book i have they say to put a ClassName="" attribute in the ascx tag, like such:

    <%@ Control Inherits="uCtrlPreview" src="uCtrlPreview.vb" ClassName="SomeOtherName" %>

    saying that it is needed in case we dynamically create the control on a page.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.