Click here to Skip to main content
13,049,814 members (63,035 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 16 Jun 2005

WebForms Automatic Generation Using Reflection (1)

, 22 Jun 2005
Rate this:
Please Sign up or sign in to vote.
Describes how to generate a WebForm using reflection from a class.


This is the first of a series of articles about code generation of Web Forms.

The idea is very simple. For the following class:

enum ArticleRating
   Bad, Good, Excelent
class Feedback 
    public string Name;
    public string Email;
    public string Comments;
    public ArticleRating Rating;

we want the following Web Form to be generated:

<TR><TD>Name:</TD><TD><INPUT id="name"></TD></TR>
<TR><TD>Email:</TD><TD><INPUT id="email"></TD></TR>
<TR><TD>Comments:</TD><TD><input id="comments"></input></TD></TR>
<TR><TD>Ratings:</TD><TD><SELECT id="ratings">
<OPTION selected value="0">Bad</OPTION>
<OPTION value="1">Good</OPTION>
<OPTION value="2">Excellent</OPTION>

How can we do this?

There are two ways of doing this:

  • Using Serialization, you can serialize this class and use XSLT for formatting, or just play with the DOM generated.
  • Using Reflection, to explore the class members and infer the type of control to generate.

I prefer to use Reflection because I can add flexibility to it. For example:

enum ArticleRating { Bad, Good, Excelent } 
class Feedback 
   public string Name; 
   public string Email; 
         Caption="Please Write Your Comments", 
   public string Comments; 
   public ArticleRating Rating; 

This will generate the following form:

<TR><TD>Name:</TD><TD><INPUT id="name"></TD></TR>
<TR><TD>Email:</TD><TD><INPUT id="email"></TD></TR>
<TR><TD colspan="2">Please Write Your Comments:</TD></TR>
<TD><textarea id="comments"></textarea></TD></TR>
<input type="radio" value="0">Bad</input>
<input type="radio" value="0">Good</input>
<input type="radio" value="0">Excelent</input>

That's the idea....

I request you to read the second part of this series.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Chile Chile
Eduardo Diaz
personal blog

You may also be interested in...


Comments and Discussions

Generalag_vbnetguy Pin
SomeGuyFrom Toronto, Canada23-Jun-05 17:31
sussSomeGuyFrom Toronto, Canada23-Jun-05 17:31 
GeneralRe: ag_vbnetguy Pin
ediazc24-Jun-05 3:47
memberediazc24-Jun-05 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Jun 2005
Article Copyright 2005 by ediazc
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid