Displaying user interface in asp.net
I'm just wondering if there are simpler ways to assign values โโto html controls from an object or vice versa in asp.net. Doing it manually just hurts
//assign value
txtFirstName.Text = person.FirstName;
//retrieve value
var p = new Person();
p.FirstName = txtFirstName.Text;
However, doing it "pragmatically" is also painful because it has so many cases, as you can see in this code , which is based on the ideas in 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 way to automate this. Can anyone point me to a clever way to do this, because the form I'm making has literally over 100 fields, I just don't want the code to be written by hand.
Thanks!
a source to share
We use mygenerationsoftware to generate a basic UI-DB mapping to avoid this manually, and then some tweak is required somewhere, we change the code manually.
a source to share
To get postback data you can go back to Request object and just
string [] _ArrayOfFields = new line [] {"FirstName"};
string _FirstNameValue = Request.Form [_ArrayOfFields [0]];
etc.
Then you can do something like this when setting values โโfor your controls
((TextBox) Page.Controls [Page. FindControl (ArrayOfFields [0])]). Text = "New Value";
a source to share