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.
The user control dynamically loaded at runtime is strategically placed in a table cell in the ItemTemplate . This loading takes place in the method that handles the ItemDataBound event for each row of the Repeater .
User controls. User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it. Custom controls. A custom control is a class that you write that derives from Control or WebControl.
ascx file, also known as a user control, requested from a server that hosts an ASP.NET Web application. The file must be called from a Web Forms page or a parser error will occur.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With