How to set properties in dynamically loaded user controls RRS feed

  • Question

  • User-501461518 posted

    Hi. I've got a complex situation but will try to explain a very simplified version of what I'm trying to do.

    I have a page with a placeholder where I want to load a user control. I have a number of user controls (lets just say ControlA and ControlB) but each type of control has different properties. So ControlA may have a "text" property and ControlB a "date" property.

    I have a data source that I am reading (I can write the reader but easiest to assume its xml or json). The data source defines which control to load and the property value. Thus the data source may define "[type=ControlA, text="something"]" or "[type=ControlB, date=20/5/2013]"

    Clearly it's easy to determine the type of control to load and use LoadControl to load it. However, I'm not sure how to deal with the properties and each type of control may have multiple properties. Thus I need to read and parse the data source to determine the property name and value and then need to set the corresonding property on the control. How can I check whether the property exists (although the data source is under my control and should always match and be correct I just want to double check)? Then how can I set the property?

    ie if the data source contains  "[type=ControlA, text="something"]"  I want to do LoadControl("ControlA.aspx"); and then want to do the equivalent of myControl.text = "something"

    I understand how to create the user control is just a question of how to access the properties and method programmatically where I know the property name and value only at run time.

    Monday, May 20, 2013 8:28 AM