Help! how to make fields on the fly?

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
 
M

Mark Fitzpatrick

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.
 
J

james

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top