468,514 Members | 1,631 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,514 developers. It's quick & easy.

Help! how to make fields on the fly?

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

Nov 2 '06 #1
2 1040
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[i].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" <ja********@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
>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

Nov 2 '06 #2
It works great -- Thanks Mark

James
Mark Fitzpatrick wrote:
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[i].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" <ja********@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
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
Nov 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tom | last post: by
6 posts views Thread by wukexin | last post: by
3 posts views Thread by Colin J. Williams | last post: by
7 posts views Thread by Corepaul | last post: by
5 posts views Thread by Steve | last post: by
8 posts views Thread by Mark | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.