Specified cast is not valid in user controls

Discussion in 'ASP .Net Web Controls' started by Steve, Jan 15, 2004.

  1. Steve

    Steve Guest

    I have been trying to add a control that I designed in one project
    into another. I come to adding the thing and get the casting error
    but I have just copied the file from one project to the other and just
    changed the namespace name. Here is the code that is puzzling me.

    Control catmenu1 = LoadControl("CatMenu.ascx");
    HoldCatMenu.Controls.Add(catmenu1); // -- adds to container
    shop.CatMenu mm = (shop.CatMenu)HoldCatMenu.Controls[0]; // -- the
    class where
    the control lives

    And the last line is the one where the cast exception is being thrown
    - it seems to work fine on another project.

    Does anyone know if this is the EnableViewState problem that seems to
    be going about? I've set that to false in the attributes in design
    and in the Page_Load method like has been suggested.

    Any suggestions anyone?
    Steve
     
    Steve, Jan 15, 2004
    #1
    1. Advertising

  2. Steve

    Scot Meyer Guest

    Are you sure that the item you added is infact at position [0]

    You would get this error if you attempted to cast to an object that cannot be casted to..
    i.e. long info=(long)DateTime //suedo cod

    :please keep the responses in the thread as this will help others that may have similar problem
    I make no warranties on the code (if any) listed her
    mailmeat: scotchy32000 at yahoo nospam dot com <- remove nospam add @ .
     
    Scot Meyer, Jan 15, 2004
    #2
    1. Advertising

  3. Steve

    Steve Steve Guest

    Not sure about the array position but I got this working in another
    project with identical code and it all works fine. (It's actually
    somebody else's code that is adding the controls to the web form - I was
    doing the dynamic controls)

    I have fixed this problem but not in a satisfactory way - I had to
    create a new .ascx file and cut and paste the code from one to the
    other, then I copied the web form (just a panel because the control
    content is created dynamically) and then changed the name of the
    original file and renamed the new one to the old file and it worked -
    I'm seriously hoping I don't have to do this with every control because
    it defeats the object of drag and drop controls.

    I will look into the array position if I have time but it's fixed for
    now - and I will post if I figure it out.

    If anybody knows why because they've fixed a similar problem I'd be
    interested to hear

    Thanks
    Steve


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Steve Steve, Jan 16, 2004
    #3
  4. Steve

    Scot Meyer Guest

    To iterate .net Documentatio

    System.Web.UI.Page.Controls.Add(
    The new control is added to the end of an ordinal index array. The control can be an instance of any ASP.NET server control, a custom server control you create, or a literal control

    To add a control to the collection at a specific index location, use the AddAt method

    So if you were to controls.add(obj) to an collection that contained two items already then you must refer to that item b
    obj = controls(2) which is the obhect you just adde
    controls(0) = original ite
    controls(1) = original 2nd ite
    controls(2) = your new ite

    HTH
     
    Scot Meyer, Jan 16, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ajit
    Replies:
    1
    Views:
    3,621
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
  2. Floela
    Replies:
    3
    Views:
    578
    Jon Skeet
    Jul 10, 2003
  3. Alan Silver
    Replies:
    5
    Views:
    2,594
    Kevin Spencer
    Feb 15, 2005
  4. Ajit
    Replies:
    1
    Views:
    188
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
  5. Ajit
    Replies:
    1
    Views:
    243
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
Loading...

Share This Page