Specified cast is not valid in user controls

S

Steve

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
 
S

Scot Meyer

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 @ .
 
S

Steve Steve

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
 
S

Scot Meyer

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top