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

6

29

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.

Dolliedolloff answered 4/2, 2010 at 16:29 Comment(0)
M
49

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" />
Metamorphism answered 4/2, 2010 at 16:30 Comment(3)
This is a great answer but I can't use it because controls are loaded dynamically. Maybe I'm going to use hidden fields. Thank you.Dolliedolloff
You should use ViewState to hold the control state.Metamorphism
And how can I hold public proterties' values on ViewState? I'm trying to do it, but myIntValue is always zero.Dolliedolloff
P
13

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";
Pollypollyanna answered 4/2, 2010 at 16:31 Comment(1)
This is a great answer but I can't use it because controls are loaded dynamically. Maybe I'm going to use hidden fields. Thank you.Dolliedolloff
D
3

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.

Delenadeleon answered 4/2, 2010 at 16:55 Comment(4)
And what about using public properties on user controls?Dolliedolloff
That would work. Events or public properties are pretty much the same in terms of coupling.Delenadeleon
I'm so sorry, but if I load controls dynamically I can't use public properties.Dolliedolloff
You can but you have to do it by strong typing/interfaces...but that brings back coupling issues.Delenadeleon
K
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

Koren answered 5/2, 2010 at 4:6 Comment(0)
V
0

You could set values in the HttpContext.Items collection and read them in your controls. This is like using Session except that it's only available per request not for the whole session lifetime.

https://web.archive.org/web/20201202215202/https://www.4guysfromrolla.com/articles/060904-1.aspx

IMHO this is a bit lazy but it might be a good solution in some situations.

Viscacha answered 4/2, 2010 at 17:22 Comment(0)
A
-1

Kind of defeats the purpose of using an ascx control IMO since this breaks control encapsulation. Your page should be -getting- data from the control through subscribing to events published by the control.

Assemblage answered 4/2, 2010 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.