I was just wondering if there is any easier ways to assign values to html controls from an object or vice versa in asp.net. To do it manually is just painful
//assign value
txtFirstName.Text = person.FirstName;
//retrieve value
var p = new Person();
p.FirstName = txtFirstName.Text;
However to do it "pragmatically" is also kind of painful because it's got so many cases as you can see in this code, which is based on the ideas from this article.
if ((ctrl as TextBox) != null)
{
var txt = ctrl as TextBox;
if (p.PropertyType == typeof (string))
txt.Text = p.GetValue(data, null).ToString();
else if (p.PropertyType == typeof (decimal))
txt.Text = p.GetValue(data, null).ToString();
}
else if ((ctrl as DropDownList) != null)
{
var dropdown = ctrl as DropDownList;
dropdown.SelectedValue = p.GetValue(data, null).ToString();
}
I couldn't find any other clever ways to automate this. Can some one point me a clever way of doing it because the form I am doing has literally more than 100 fields I just don't want code it manually.
Thanks!