How to pass parameters to dynamically loaded user controls?

Hi,

I have a set of user controls that loads dynamically. This codes works fine:

UserControl UC;
for(int i = 0; i < oObj.Blocks.Count; i++)
{
      UC =(UserControl)LoadControl( Items.getBlock(oObj.Block(i)) );
      Panel1.Controls.Add(UC);
}

what I don't ge working is sending in parameters. I've tried:
UC.ObjID = "1234";

Then I get the message: 'System.Web.UI.UserControl' does not contain a definition for 'ObjID'

while the ascx does have the field: public string ObjID = "";


thanks, GL      


GoldyLockAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

andrewjbCommented:
You'll need to cast UC to whatever the actual type of the control is - the one that's defining the ObjID property.

So, something like

((MyControl)UC).ObjID = "1234";

or, if you're not sure whether it always will be a MyControl,

MyControl myCtl = UC as myCtl;
if ( myCtl != null )
{
  myCtl.ObjID = "1234";
}
adatheladCommented:
Hi,

Your user control inherits from UserControl, and adds in custom properties/methods. Because UC in your code is declared as type usercontrol, it cannot find your custom property because it doesn't exist in that base class. It only exists in your customised class. Try converting UC as demonstrated below:

((MyUserControlType)UC).ObjID = "1234";
adatheladCommented:
Sorry for the duplicate response!
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

GoldyLockAuthor Commented:
My problem is that I have huge set of ascx-files and that I dont know before which one will be loaded:

UserControl UC;
for(int i = 0; i < oObj.Blocks.Count; i++)
{
     UC =(UserControl)LoadControl( Items.getBlock(oObj.Block(i)) );
     Panel1.Controls.Add(UC);
}

through the method getBlock() the right ascx gets loaded. this works as long as i don't try to add parameters.

I can get the name from the Block (which will be the asx file name without .ascx), so I've tried this:

for(int i = 0; i < oObj.Blocks.Count; i++)
{
     UC = (UserControl)LoadControl( Items.getBlock(oObj.Block(i)) );
     ((oObj.Block(i).Name)UC).ObjID = "4321";
     Panel1.Controls.Add(UC);
}

translated this would then for example be:
      UC = (UserControl)LoadControl("FAQ.ascx");
     ((FAQ)UC).ObjID = "4321";

But this still gives the error that namespace 'FAQ' couldn't be found.

thanks
andrewjbCommented:

What does LoadControl() return, then? Isn't there some base class you've defined with an 'ObjID' that's common to all your created controls? Then just cast to that..
adatheladCommented:
Create an Interface (e.g. IMyControl) that includes a signature for the ObjID property.

Then each control would need to implement this interface.

Which would allow you to do the below because by implementing this interface, all your controls would have to have this property:
((IMyControl)UC).ObjID="4321"
GoldyLockAuthor Commented:
Sorry, I'm obviously a bit green here. A piece of code for the casting and where it should be placed would help me. I've tried to place it in the user control but then i still get the message that the namespace can't be found.

Simplified:

<script language="C#" runat="server">
    public class FAQ : System.Web.UI.UserControl
    {
           public string ObjID = "";
           public FAQ(){}
    }
</script>

code in the aspx still as above:
UserControl UC;
UC = (UserControl)LoadControl("FAQ.ascx");
((FAQ)UC).ObjID = "4321";

I should add that this is a part of an application being set over from ASP to ASP.Net. The application can not use compilation for the templates. This was before simply solved with server side includes and there wouldn't have to be any passing of parameters since includes gets completely emebedded in a page.

Now every template in the application is a ascx-file that can't have codebehind (goes for the page as well) I could make it quick and dirty and continue using includes for this part. Also the fact that all controls would be tied to the same properties doesn't sound very attractive for the flexiblility of the functionality of the templates, since the parameters send in can be different.


andrewjbCommented:
AH.. ok...

I hadn't noticed you're web and not WinForms... And I wasn't familiar with LoadControl,

I would think you've two options:

1) Change your .ascx files. Make all the controls derive from MyBaseControl rather than Web.UI.UserControl
Then write a MyBaseControl which derives from Web.UI.UserControl and has a string 'ObjID' property
Thus, after calling LoadControl, you can always cast to MyBaseControl, and set the property.


2) Use reflection
After calling LoadControl, you need to use reflection to find the 'ObjID' property/field on the control that's been created. Then set it.
You'll use things like GetProperty(..) on your UC, which returns a PropertyInfo etc.

(1) is better. (2) will be slower.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.