Showing posts with label usercontrols. Show all posts
Showing posts with label usercontrols. Show all posts

Thursday, March 29, 2012

Passing values between two usercontrols?

Hi

How do I pass values between two UserControls?

Can I just register the one usercontrol inside the other usercontrol and then use a public property?

ThanksThis is a very common question, but there is a good and a bad way to answer it.
The short, bad way is to expose a public property, then have one of the controls find the other using FindControl and then set the property after having cast to the right type.
The problem with this is that it introduces a strong coupling between your two controls. What if one of the controls is not on the page? What if it's been replaced with another control with the same ID? What if one control evolves and not the other?

Which leads to the good way to do it: not to do it. The page or a higher container control should act as a global controller here and manage the relationships between the controls it contains. The controls themselves should be self-contained and not depend on any external control to function. The page should handle the events from the controls and manage the communication between controls from there.

Does this make sense?
Hi

Making sense.

Thanks

Monday, March 26, 2012

Passing Values To and From UserControls

Hi All,
Very new to ASP.NET.
I'm building several usercontrols that need to accept and pass back various values to and from the calling page. Is there an easy way to accomplish this?? (C# if possible)

thxYou can expose them as properties on your user control just as you might write a property in one of your own custom classes.

Saturday, March 24, 2012

passing variables between usercontrols

Is there an easy way to pass variables between two different usercontrols within an aspx page?In fact, i've found a way to get the initial value of a variable within a user control modifying some code I found off a tutorial on the net, but the problem is that it can only retrieve the initial value, no other value. Is there any way to modify this? Here is the code i've used

Control _myControl = Page.FindControl("MyControl1");
Type _myControlType = _myControl.GetType();
PropertyInfo _myUC_EndDate = _myControlType.GetProperty("EndDate");
EndDate.Text = _myUC_StartDate.GetValue(_myControl, null).ToString();
You can create public properties on the user control, then access the property via UserControl:Property...

/*
example:

myControl.ascx

*/

private string name; // the name field
public string Name{
get{
return name;
}
set{
name = value;
}
}
i've actually tried that method, but that seems to return the initial value of the variable if there is any.
Are you getting the value at Page_Load?

If you are setting the value on a click method for instance, it will be one step behind. Click it twice in a row and see if it has the results you were expecting after the second click.

:-)
yes, i am setting the value on a click method. But i do not really want to have to click the button twice in a row to be able to retrieve the new values. Is there no way to change this?
LOL,

no no, just checking if that was the case. I have the same problems sometimes --

ok, so what you need to do on the in the click event is make sure you rebind the data to the control.
thanks for that, I may be tempted to change how my code currently works at the moment (coz it's currently working but not exactly to my preferred choice), and implement this later. I had thought before that I would have needed to pass the variables across as events.