简体   繁体   中英

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

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.

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" />

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";

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM