Click here to Skip to main content
Licence 
First Posted 16 Jun 2005
Views 22,169
Bookmarked 24 times

WebForms Automatic Generation Using Reflection (1)

By | 22 Jun 2005 | Article
Describes how to generate a WebForm using reflection from a class.

Introduction

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:

<TABLE><TBODY>
<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>
</SELECT>
</TD>
</TR>
</TBODY></TABLE>

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:

[FormControl(Type="RadioGroup")] 
enum ArticleRating { Bad, Good, Excelent } 
[Form(Submit="OK",Clear="Clear",Action="dofeedback.aspx")] 
class Feedback 
{ 
   public string Name; 
   public string Email; 
   [FormControl(Type="TextArea", 
         Caption="Please Write Your Comments", 
         CaptionPosition="Over")] 
   public string Comments; 
   public ArticleRating Rating; 
}

This will generate the following form:

<TABLE><TBODY>
<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>
<TR><TD>Ratings:</TD><TD>
<input type="radio" value="0">Bad</input>
<input type="radio" value="0">Good</input>
<input type="radio" value="0">Excelent</input>
</TD>
</TR>
</TBODY></TABLE>

That's the idea....

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

License

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

ediazc

Web Developer

Chile Chile

Member

Eduardo Diaz
personal blog

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalag_vbnetguy PinsussSomeGuyFrom Toronto, Canada17:31 23 Jun '05  
GeneralRe: ag_vbnetguy Pinmemberediazc3:47 24 Jun '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120517.1 | Last Updated 22 Jun 2005
Article Copyright 2005 by ediazc
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid