cannot access user control properties within a webform code-behind

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default cannot access user control properties within a webform code-behind

    Hello, I am trying to access the properties and methods from a user
    control within the code-behind file for a webform but I am receiving
    the message:

    Name 'MenuBar1' is not declared

    It does not recognize the user control in the code behind...
    Here is the code for the user control:

    MenuBar.ascx:
    <%@ Control Language="vb" AutoEventWireup="false"
    Codebehind="MenuBar.ascx.vb" Inherits="TestApp.MenuBar"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

    ....

    MenuBar.ascx.vb:
    Public MustInherit Class MenuBar
    Inherits System.Web.UI.UserControl

    Dim x As String

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    Public Property testX() As String
    Get
    testX = x
    End Get
    Set(ByVal Value As String)
    x = Value
    End Set
    End Property
    End Class

    This is the page that is using the code:

    test.aspx:
    <%@ Register TagPrefix="uc1" TagName="MenuBar" Src="MenuBar.ascx" %>
    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="test.aspx.vb" Inherits="TestApp.test"%>

    ....
    <uc1:menubar id="MenuBar1" runat="server"></uc1:menubar>

    Then in my code behind:
    test.aspx.vb

    Dim y As String
    y = MenuBar1.testX()

    I receive the message:
    Name 'MenuBar1' is not declared

    ....and MenuBar1 has the blue squiggly line....

    I know the problem is that it does not understand what 'MenuBar1' is,
    my question is, what am I missing so that i can get the code behind to
    recognize 'MenuBar1'...

    Thanks Before Hand,
    Adiel
    adiel Guest

  2. Similar Questions and Discussions

    1. Help: ASP.NET Webform using VB.NET User Control
      I created a VB.NET user control which uses a nonmanaged ActiveX control. I built it as 'mycontrol.dll'. In my ASP.NET webform I'm trying to add...
    2. User Control properties
      Hi all, I'm a total newbie, so this might be stupid... Anyway, I've created an expanding tree of categories control, based on DataList. It works...
    3. Can not access properties of controls of user control???
      Hi, I have created a user control, and need to change the properties of controls on the user control. However, if the form of user control are...
    4. Including WebForm Image Control in a Webform Table Control
      What is the code for including an image control in a Table control of a WebForm ???? regards
    5. Access properties of parent page from user control
      I am trying to build a control that can only be used on a page that inherits from a custom class. This base class as a series of public...
  3. #2

    Default Re: cannot access user control properties within a webform code-behind

    try to declare menubar1 from code-behind before using it.


    "adiel" <adiel_g@hotmail.com> wrote in message
    news:fed844cc.0307010537.5c4bb7d6@posting.google.c om...
    > Hello, I am trying to access the properties and methods from a user
    > control within the code-behind file for a webform but I am receiving
    > the message:
    >
    > Name 'MenuBar1' is not declared
    >
    > It does not recognize the user control in the code behind...
    > Here is the code for the user control:
    >
    > MenuBar.ascx:
    > <%@ Control Language="vb" AutoEventWireup="false"
    > Codebehind="MenuBar.ascx.vb" Inherits="TestApp.MenuBar"
    > TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    >
    > ...
    >
    > MenuBar.ascx.vb:
    > Public MustInherit Class MenuBar
    > Inherits System.Web.UI.UserControl
    >
    > Dim x As String
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > End Sub
    >
    > Public Property testX() As String
    > Get
    > testX = x
    > End Get
    > Set(ByVal Value As String)
    > x = Value
    > End Set
    > End Property
    > End Class
    >
    > This is the page that is using the code:
    >
    > test.aspx:
    > <%@ Register TagPrefix="uc1" TagName="MenuBar" Src="MenuBar.ascx" %>
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Codebehind="test.aspx.vb" Inherits="TestApp.test"%>
    >
    > ...
    > <uc1:menubar id="MenuBar1" runat="server"></uc1:menubar>
    >
    > Then in my code behind:
    > test.aspx.vb
    >
    > Dim y As String
    > y = MenuBar1.testX()
    >
    > I receive the message:
    > Name 'MenuBar1' is not declared
    >
    > ...and MenuBar1 has the blue squiggly line....
    >
    > I know the problem is that it does not understand what 'MenuBar1' is,
    > my question is, what am I missing so that i can get the code behind to
    > recognize 'MenuBar1'...
    >
    > Thanks Before Hand,
    > Adiel

    zPaul Guest

  4. #3

    Default Re: cannot access user control properties within a webform code-behind

    Thanks zPaul, I have two questions concerning that suggestion:

    1. Do you have a sample code on how I would declare this user control?
    2. If I declare it from code behind (test.aspx.vb), how would .NET
    know that this is the same control being used in the webform
    (test.aspx)?

    Thanks again,
    Adiel Gonzalez
    > try to declare menubar1 from code-behind before using it.
    adiel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139