With what methods can I dynamically set an ASPX control property before initializing it?

I need to set up a property of a custom control that I wrote before the control will call OnInit

. If I assign it as part of the ASPX file, it naturally works, but if I translate the assignment to different parts of the code instead, it throws an error for an empty and invalid value. The following looks like how it works in an ASPX page.

<MyCustomControls:SingleCascadeLookupMulti FilterString="Seventh" FilterByStatus="true" ControlMode="New" FieldName="Speciality" EnableViewState="true" ID="sclmDocuments" runat="server" TemplateName="SingleCascadeLookupMulti" />

      

The corresponding property FilterString

, which is a simple string. It should also be noted that the property FieldName

(inherited from the base control class "BaseFieldControl") would also throw an error if it is not initialized, but if I set it FieldName

in the code behind OnInit

it works correctly. FilterString

not, it will not actually be assigned. So I know that some methods on certain properties will work to set the property value, but that won't always work. I have also tried putting it in OnPreInit, to no avail.

I'm using a dynamic approach to the assignment, because the functionality of the page is to be replicated for a number of different lists, and properties FilterString

and FieldName

will be different in each case. Rather than writing half a dozen mostly identical ASPX pages with the same code but differing only in that the two properties are, I decided that it would be wiser to use the page query string and some derived parameters to dynamically set these properties. So what methods are available to accomplish this task?

+2


a source to share


1 answer


One of the methods I have used to do this is to create a constructor in the control that takes the parameters I need to set (in your case, a FilterString). Then I use the following function to dynamically load the control by passing parameters to it:

protected UserControl LoadControl(string UserControlPath, params object[] constructorParameters)
{
    List<System.Type> constParamTypes = new List<System.Type>();
    foreach (object constParam in constructorParameters)
    {
        constParamTypes.Add(constParam.GetType());
    }

    UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;

    // Find the relevant constructor
    ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());

    //And then call the relevant constructor


      if (constructor == null)
        {
            throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString());
        }
        else
        {
            constructor.Invoke(ctl, constructorParameters);
        }

        // Finally return the fully initialized UC
        return ctl;
    }

      



and call the method like this:

Control ctlToAdd = this.LoadControl("UserControls/MyControl.ascx", Parameter1, "Parameter2");
        MyDiv.Controls.Add(ctlToAdd);

      

+1


a source







All Articles