?
Solved

How to pass parameters to dynamically loaded user controls?

Posted on 2006-03-31
8
Medium Priority
?
605 Views
Last Modified: 2008-01-09
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      


0
Comment
Question by:GoldyLock
  • 3
  • 3
  • 2
8 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 16340563
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";
}
0
 
LVL 23

Expert Comment

by:adathelad
ID: 16340580
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";
0
 
LVL 23

Expert Comment

by:adathelad
ID: 16340585
Sorry for the duplicate response!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:GoldyLock
ID: 16340872
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
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 16340985

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..
0
 
LVL 23

Expert Comment

by:adathelad
ID: 16341019
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"
0
 

Author Comment

by:GoldyLock
ID: 16341808
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.


0
 
LVL 12

Accepted Solution

by:
andrewjb earned 1500 total points
ID: 16341880
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.



0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question