Question

I load the content of some User Controls into my page with the use of jQuery. So I have this function that extracts the content from my User Control, and it works like a charm.

    public string GetObjectHtml(string pathToControl)
    {
        // Create instance of the page control
        Page page = new Page();

        // Create instance of the user control
        UserControl userControl = (UserControl)page.LoadControl(pathToControl);

        //Disabled ViewState- If required
        userControl.EnableViewState = false;

        //Form control is mandatory on page control to process User Controls
        HtmlForm form = new HtmlForm();

        //Add user control to the form
        form.Controls.Add(userControl);

        //Add form to the page
        page.Controls.Add(form);

        //Write the control Html to text writer
        StringWriter textWriter = new StringWriter();

        //execute page on server
        HttpContext.Current.Server.Execute(page, textWriter, false);

        // Clean up code and return html
        string html = CleanHtml(textWriter.ToString());

        return html;
    }

But I would really like to send in some parameters into my User Control when I create it. Is this possible, and how can i do that?

I can see that LoadControl() can take some parameters with object[] parameters but im really not sure on how to use it, help much appreciated!

Was it helpful?

Solution

You can implement an interface on your usercontrol for the appropriate params.

public interface ICustomParams
{
    string UserName { get; set; }
    DateTime SelectedDate { get; set; }
}

implement the interface in the usercontrol, like this

public partial class WebUserControl : System.Web.UI.UserControl , ICustomParams
{
    public string UserName { get; set; }
    public DateTime SelectedDate  { get; set; }
}

then load your control:

  UserControl userControl = (UserControl)page.LoadControl(pathToControl);

Acces the control via the interface

  ICustomParams ucontrol = userControl as ICustomParams;
  if(ucontrol!=null)
  {
       ucontrol.UserName = "henry";
       ucontrol.SelectedDate = DateTime.Now;
  }

Done,

you can add multiple interfaces for multiple purposes there. If the usercontrol hasn't the interface implemented, the if statement will avoid using it

But if you really cannot access the usercontrols and you know "a bit" of the properties you want to set and what type they are try a more dynamic way with reflection:

load the usercontrol:

    UserControl userControl = (UserControl)Page.LoadControl(@"~/WebUserControl.ascx");

get the properties of the loaded usercontrol:

    PropertyInfo[] info = userControl.GetType().GetProperties();

loop trough it:

    foreach (PropertyInfo item in info)
    {
        if (item.CanWrite)
        {
             switch (item.Name)
             {
                 case "ClientName"
                     // A property exists inside the control": //
                     item.SetValue(userControl, "john", null); 
                     // john is the new value here
                 break;
             }
        }
    }

I would only encourage you this if you CANNOT access the usercontrols and there are dozens of them with lots and lots of variable properties per usercontrol. (It can get very ugly, slow and not fail-safe)

OTHER TIPS

I'm not sure how it can be done generically, but it looks like you're loading your own user control anyway. Try casting the the UserControl to the type of the control you're using, for example.

 // Create instance of the user control 
    MyUserControl userControl = (MyUserControl)page.LoadControl("/MyUserControl.ascx"); 

  userControl.Param1="my param";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top