I am not sure if I have asked the question correctly. Please correct me if I am wrong.
Anyways, we would like to use a variable's value on a different phases of the page's life cycle.
So for example,
public partial class TestUserControl: UserControl{
public TestUserControl(){
Objects = new List<object>(){
Property1,
Property2,
Property3
};
}
public int Property1 { get; set; }
public bool Property2 { get; set; }
public string Property3 { get; set; }
public List<object> Objects { get; set; }
protected override OnLoad(EventArgs e){
foreach(var item in Objects){
Page.Controls.Add(new LiteralControl(item.ToString() + "<br/>"));
}
}
}
So if we say the values of Property1, Property2, Property3 are set as they were written on the tag, how could we use the properties' values just as when needed?
That on the constructor, instead of the values of the properties will be listed down on the List, only the Properties' names will be listed down so their current values will be used on OnLoad.
Thanks a lot.
Edit: I have adopted deerchao's and Jon's approach, but there seems to be another problem we will be facing... It's like:
public partial class TestUserControl: UserControl{
public TestUserControl(){
Objects = List<Func<object>>{
() => Property1,
() => Property2,
() => Property3
};
}
public int Property1 { get; set; }
public bool Property2 { get; set; }
public string Property3 { get; set; }
public List<Func<object>> Objects { get; set; }
protected override OnLoad(EventArgs e){
foreach (var item in Objects) {
//Here is the problem... can we get the variable name using this approach?
//We have tried some other ways like reflection and expressions but to no avail. Help please =)
string key = GetVariableName(item());
string value = item() == null ? string.Empty : item().ToString();
Page.Controls.Add(new LiteralControl(key + " : " + value + "<br/>"));
}
}
}