• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 610
  • Last Modified:

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      


0
GoldyLock
Asked:
GoldyLock
  • 3
  • 3
  • 2
1 Solution
 
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";
}
0
 
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";
0
 
adatheladCommented:
Sorry for the duplicate response!
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
0
 
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..
0
 
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"
0
 
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.


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



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now