I’m dynamically adding a custom user control to the page as per this post.
However, i need to set a property on the user control as i add it to the page. How do I do that?
Code snippet would be nice 🙂
Details:
I have a custom user control with a public field (property … e.g. public int someId;)
As I add a UserControl to a page, its type is UserControl. Do i just cast it into MyCustomUCType and set a property on a cast control?
p.s. looks like I’ve answered my own question after all.
Ah, I answered before you added the additional clarification. The short answer is, yes, just cast it as your custom type.
I’ll leave the rest of my answer here for reference, though it doesn’t appear you’ll need it.
Borrowing from the code in the other question, and assuming that all your User Controls can be made to inherit from the same base class, you could do this.
Create a new class to act as the base control:
Then update your user controls to inherit from your base class instead of UserControl:
Then, in the place where you load the controls, change this:
to: