Help! how to make fields on the fly?

  • Thread starter Thread starter james
  • Start date Start date
J

james

I recieve a business object and need to create a form to edit each
property. This object is huge (like over 100 properties), and I dont
want to make 100 label/textboxes by hand. Does anyone know how to
easily generate the form?

By properties I mean the business object looks like:

public class BusinessObject {
public string prop1;
public string prop2;
....
}

Thanks!
James
 
James,
There's nothing automatic, but if you used reflection you could load
the object, find all the properties, and then spit out the names based on
the list of properties.

If you use the GetType() method of an object, you can then call
another method called GetProperties(). This method returns an PropertyInfo
array like so:

System.Web.UI.WebControls.TextBox box = new TextBox();
PropertyInfo[] properties = box.GetType().GetProperties();

If this were a web page, I could just response.write out the list name to
make it easy as so:
for (int i = 0; i < properties.Length; i++)

{

Response.Write(properties.Name + "\n");

}

The "\n" just adds a linebreak on the end to make it easier to read the
output.

Next you could just create labels or textboxes based on the property list
and tweak the names/values and eliminate the ones that come from a parent or
base class.
 
It works great -- Thanks Mark

James


Mark said:
James,
There's nothing automatic, but if you used reflection you could load
the object, find all the properties, and then spit out the names based on
the list of properties.

If you use the GetType() method of an object, you can then call
another method called GetProperties(). This method returns an PropertyInfo
array like so:

System.Web.UI.WebControls.TextBox box = new TextBox();
PropertyInfo[] properties = box.GetType().GetProperties();

If this were a web page, I could just response.write out the list name to
make it easy as so:
for (int i = 0; i < properties.Length; i++)

{

Response.Write(properties.Name + "\n");

}

The "\n" just adds a linebreak on the end to make it easier to read the
output.

Next you could just create labels or textboxes based on the property list
and tweak the names/values and eliminate the ones that come from a parent or
base class.


--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

james said:
I recieve a business object and need to create a form to edit each
property. This object is huge (like over 100 properties), and I dont
want to make 100 label/textboxes by hand. Does anyone know how to
easily generate the form?

By properties I mean the business object looks like:

public class BusinessObject {
public string prop1;
public string prop2;
....
}

Thanks!
James
 
Back
Top