Pass data from a ASP.NET page to ASCX user controls loaded dynamically

61,526

Solution 1

Create a property on your user control with the datatype of the data you want to pass to it, and populate it in your page on creation of the control.

public class myControl : Control
{
  ...
  public int myIntValue {get; set;}
  ...
}

In the code behind:

myControl ctrl = new myControl();
ctrl.myIntValue = 5;

You can also do this directly in markup:

<uc1:myControl ID="uc1" runat="server" myIntValue="5" />

Solution 2

Setup public properties within your user control.

public string TestValue { get;set;};

And then when you put your user control in your aspx page:

<uc1:UserControl ID="uc1" runat="server" TestValue="Testing" />

You can also change the values within your code behind:

uc1.testValue = "some value";

Solution 3

You'll have to re-load those controls on each post-back... Give this a read. It might help.

Dynamic Web Controls, Postbacks, and View State

Solution 4

To actually answer your quesiton, as much as everyone else seems to not want you to do this, and I agree...I have done this sort of thing before.

The first thing I'd do is make your page implement an interface.

In the control:

IVansFannelDataProviderPage provider = this.Page as IVansFannelDataProviderPage; if (provider != null) { //grab data from interface } else throw YouCantPutThisControlOnThisKindOfPageException();

It's not the most elegant way to do it, but when we had a lot of controls wanting to share a very expensive object this fit the bill.

This works well, but it makes your control unusable on pages that don't implement the interface--and that makes the controls too tightly coupled to your page. Everyone else saying to have the page get data from the controls is correct; you put controls on the page, not pages in controls.

You should have a very good reason in order to do it. For us: the load of the shared object was very expensive, and having the page load/save it no matter what control was working on that object was quite useful.

It was too bad that a lot of pages that didn't really implement the interface had to be shoehorned to provide some sort of support or proxy support just to get the controls to work, and made the pages and controls that much less reusable.

If I had to do this over again, I'd have the page send data to the controls with events, probably through reflection if I needed to be lazy.

Share:
61,526
VansFannel
Author by

VansFannel

I'm software architect, entrepreneur and self-taught passionate with new technologies. At this moment I am studying a master's degree in advanced artificial intelligence and (in my free time ) I'm developing an immersive VR application with Unreal Engine. I have also interested in home automation applying what I'm learning with Udacity's nanodegree course in object and voice recognition.

Updated on July 08, 2020

Comments

  • VansFannel
    VansFannel almost 4 years

    I'm developing an ASP.NET application with C# and Ajax.

    I have a page that holds user controls loaded dynamically. I need to pass some data (integer values and some strings) to the user control that has been loaded dynamically.

    Now I use Session to pass these values, but I think I can use another way; something like VIEWSTATE or hidden input.

    What do you recommend me?

    UPDATE:

    The fact that I load the controls dynamically is important because controls are loaded on every postback, and I can't store any value on controls.