5,427,303 members and growing! (15,767 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

WebForms Automatic Generation Using Reflection (2)

By ediazc

This article continues the series on Web Form automatic generation.
C#, Windows, Win Mobile, .NET 1.1, .NET, ASP.NET, Visual Studio, VS.NET2003, Dev

Posted: 24 Jun 2005
Updated: 24 Jun 2005
Views: 13,985
Bookmarked: 16 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 1.58 Rating: 1.87 out of 5
4 votes, 57.1%
1
1 vote, 14.3%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
2 votes, 28.6%
5

Introduction

In the previous article, I gave you a general idea about this project where we build a form automatically from class generation.

This time we will explore the basics of reflection used in our project.

Reflection

You can use reflection to get the inside information of a class.

I am not going to teach you all about reflection, there is a lot to discuss about this topic. For more information on this, please go through my blog.

I will show you how to read the public properties of a class and generate a simple form. This is our first experiment on generation and not the final component that we want to build.

Using FormGen

First, we will create a class called FormGen. Here is the declaration of this class:

class FormGen {
    public static void Generate(Type classType, TextWriter output);
}

You can use this class in ASP.NET in the following way:

class MyForm

{

   public string Email;

   public string Password;

   public string Comments;

}
....
private void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
     FormGen.Generate(typeof(MyForm), Response.Output);
  }

Implementing GenForm

You can download the implementation of GenForm. This is a basic implementation in our first attempt to explore this technology. Here is the core code to implement a simple reflector form generator:

using System;
using System.IO;
using System.Reflection;
...

public static void Generate(Type classType, TextWriter output)
{
   WriteTableHeader(output);
   FieldInfo[] fields = classType.GetFields ();
   for (int i = 0; i < fields.Length; i++) 
   {
         WriteRow(output, fields[i].Name, 
                      InputText(fields[i].Name));
   }
   WriteTableFooter(output);
}

This is very limited... In this implementation we have focused only on the text input box.

What's next

In the next article, we will use attributes to better handle the format of the form.

Stay tuned .... :)

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


Eduardo Diaz
personal blog
Occupation: Web Developer
Location: Chile Chile

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralThird articlememberjel010:37 30 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jun 2005
Editor: Rinish Biju
Copyright 2005 by ediazc
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project