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.

0 comments:

Post a Comment