views:

525

answers:

6

Hello.

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.

+4  A: 

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" />
Oded
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.
VansFannel
You should use ViewState to hold the control state.
Oded
And how can I hold public proterties' values on ViewState? I'm trying to do it, but myIntValue is always zero.
VansFannel
+2  A: 

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";
Jack Marchetti
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.
VansFannel
A: 

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.

Pierreten
I need to pass data from PAGE to CONTROL.
VansFannel
How does this break encapsulation?
John Saunders
+1  A: 

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.

Broam
And what about using public properties on user controls?
VansFannel
That would work. Events or public properties are pretty much the same *in terms of coupling.*
Broam
I'm so sorry, but if I load controls dynamically I can't use public properties.
VansFannel
You can but you have to do it by strong typing/interfaces...but that brings back coupling issues.
Broam
A: 

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.

http://www.4guysfromrolla.com/articles/060904-1.aspx

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

OrionRobillard
A: 

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

Coov