User Controls and CodeBehind

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

  1. #1

    Default Re: User Controls and CodeBehind

    Hi,

    It may sound stupid, but this happens when you forget to compile the .cs
    files first. If this is not the problem and the compile went ok, then you'd
    have to check all the namespaces and files again, but I am sure you did that
    already. Do you use VS.NET?

    Best regards,

    Marc Höppner
    NeoGeo

    "Dave" <lyd@cambridgemedia.com.au.nospam> wrote in message
    news:eh9Ra.451$FZ6.6743@nnrp1.ozemail.com.au...
    > Hi,
    > I'm having trouble trying to get a UserControl (ascx page) to be
    recognised
    > in the codebehind of an aspx.cs page. From what I've been reading in this
    > newsgroup, all I have to do is register my ascx page in my aspx page then
    in
    > my aspx.cs page add a reference to the control's ID like so:
    >
    > index.aspx:
    > ------------------------------------------------------------------------
    > <%@ Page Language="C#" Src="index.aspx.cs" Inherits="Site.Home" %>
    > <%@ Register TagPrefix="Menu" TagName="MyMenu" Src="menu.ascx" %>
    > ..
    > ..
    > <Menu:MyMenu id="ctlControl" runat="server">
    >
    >
    > index.aspx.cs
    > ------------------------------------------------------------------------
    > namespace Site
    > {
    > public class Home : System.Web.UI.Page
    > {
    > protected Site.Controls.Menu ctlControl;
    > ....
    > }
    > }
    >
    >
    >
    > menu.ascx
    > ------------------------------------------------------------------------
    > <%@ Control Language="C#" Inherits="Site.Controls.Menu" Src="menu.ascx.cs"
    > %>
    > ..
    > ..
    >
    >
    >
    > menu.ascx.cs
    > ------------------------------------------------------------------------
    > namespace Site.Controls
    > {
    > public class Menu : System.Web.UI.UserControl
    > {
    > ....
    > }
    > }
    >
    >
    >
    > However, I keep getting an error message specifying that the class or
    > namespace does not exist. I have tried everything from changing the
    > namespace and class names but I cannot get the usercontrol to be
    referenced
    > in the codebehind page. When I reference the user control in a script
    block
    > inside the aspx page, it works fine. Does anyone know what I'm doing wrong
    > or how to fix this?
    >
    > Thanks
    > Dave
    >
    >

    Marc Hoeppner Guest

  2. Similar Questions and Discussions

    1. Why the properties of web user controls which inherted from my custom base UI controls MISSED?
      Why the properties of web user controls which inherted from my custom base UI controls MISSED? How should I to set enable?
    2. using javascript in User controls to access server controls of the user control
      Hello all, I have an asp.net textbox (named txtHidden) and an HtmlButton(named btnAction). I wanted to write a javascript function which will get...
    3. user controls: dynamiclly added child controls dont survive post back ?
      hi, i have some strange behaviour: i've created a web user control that add's some child controls (e.g: textbox, image buttons) to its control...
    4. automatic insertion of controls on codebehind
      I create a new webform (I always use codebehind), and I insert some server-side controls into the form (the ASPX) Later, I switch to codeview, and...
    5. Refrencing user controls in codebehind
      Hi Rory, You will need to call LoadControl everytime no matter which value IsPostBack is set to. From your post it seems like you're calling it...
  3. #2

    Default Re: User Controls and CodeBehind

    Hmmmm.. that sounds strange. I thought the pages are compiled when they are
    first loaded. The funny thing is that when I reference the control in the
    script block of the aspx page it works fine and if I load the control in the
    aspx page by the just it's tag name without referencing it that also works
    fine. But why can't I reference the control in the codebehind of an aspx
    page? I need to be able to reference the control as I need to access some
    properties inside the control. Btw, I'm not using VS.NET.

    Thanks
    Dave




    "Marc Hoeppner" <marchoeppner@hotmail.com> wrote in message
    news:#lkCvG4SDHA.1552@TK2MSFTNGP12.phx.gbl...
    > Hi,
    >
    > It may sound stupid, but this happens when you forget to compile the .cs
    > files first. If this is not the problem and the compile went ok, then
    you'd
    > have to check all the namespaces and files again, but I am sure you did
    that
    > already. Do you use VS.NET?
    >
    > Best regards,
    >
    > Marc Höppner
    > NeoGeo
    >
    > "Dave" <lyd@cambridgemedia.com.au.nospam> wrote in message
    > news:eh9Ra.451$FZ6.6743@nnrp1.ozemail.com.au...
    > > Hi,
    > > I'm having trouble trying to get a UserControl (ascx page) to be
    > recognised
    > > in the codebehind of an aspx.cs page. From what I've been reading in
    this
    > > newsgroup, all I have to do is register my ascx page in my aspx page
    then
    > in
    > > my aspx.cs page add a reference to the control's ID like so:
    > >
    > > index.aspx:
    > > ------------------------------------------------------------------------
    > > <%@ Page Language="C#" Src="index.aspx.cs" Inherits="Site.Home" %>
    > > <%@ Register TagPrefix="Menu" TagName="MyMenu" Src="menu.ascx" %>
    > > ..
    > > ..
    > > <Menu:MyMenu id="ctlControl" runat="server">
    > >
    > >
    > > index.aspx.cs
    > > ------------------------------------------------------------------------
    > > namespace Site
    > > {
    > > public class Home : System.Web.UI.Page
    > > {
    > > protected Site.Controls.Menu ctlControl;
    > > ....
    > > }
    > > }
    > >
    > >
    > >
    > > menu.ascx
    > > ------------------------------------------------------------------------
    > > <%@ Control Language="C#" Inherits="Site.Controls.Menu"
    Src="menu.ascx.cs"
    > > %>
    > > ..
    > > ..
    > >
    > >
    > >
    > > menu.ascx.cs
    > > ------------------------------------------------------------------------
    > > namespace Site.Controls
    > > {
    > > public class Menu : System.Web.UI.UserControl
    > > {
    > > ....
    > > }
    > > }
    > >
    > >
    > >
    > > However, I keep getting an error message specifying that the class or
    > > namespace does not exist. I have tried everything from changing the
    > > namespace and class names but I cannot get the usercontrol to be
    > referenced
    > > in the codebehind page. When I reference the user control in a script
    > block
    > > inside the aspx page, it works fine. Does anyone know what I'm doing
    wrong
    > > or how to fix this?
    > >
    > > Thanks
    > > Dave
    > >
    > >
    >
    >

    Dave 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