views:

98

answers:

3

I'm working on a CRUD site with a lot of very similar forms for adding new data. In other words:

AddMovie.aspx, AddGame.aspx, AddBeer.aspx, AddAdd.aspx

I keep thinking to myself, "Self, it would be really nice to have a single Add.aspx instead of re-writing so many similar pages - plus all those OOP nerds would think I'm cool since I'm re-using instead of copy/pasting!"

So assuming I'm on the right track, if I were to go with the single Add.aspx page, how could I represent all sets of fields for each object? I thought about a bunch of panels or divs that I could hide/show, but not sure I really like that solution. Is there a better way to do it or should I just give up and go back to the multiple bad ol' Add*Object*.aspx pages?

Also, this is a plain ol' (3.5) web forms app. No ASP.NET MVC goodness for this one, sadly. It seems like there should be such a trivial solution and that I'm just over-thinking things, but I can't come up with one and so I turn to Stack Overflow. :)

+2  A: 

A better way would perhaps be to have a single page called Add.aspx and then based on the querystring you send it (i.e. Add.asps?type=game) you could customize the form and the logic for the particular type of object your are trying to work with.

TheTXI
This is sort of what I was thinking of in my original post, but I'm not sure I like that idea.
Gary the Llama
+4  A: 

Maybe you should look at ASP.NET dynamic data or the subsonic project. Both allow to build CRUD-type website very fast because they support "scaffolding" (the edit pages are generated automatically based on your database model).

M4N
You could also build your own using reflection if you wanted/needed more control.
James Avery
This is exactly what I'm looking for. Thank you! I knew the term scaffolding but completely forgot about it.
Gary the Llama
+1  A: 

Another option is to subclass Page, and then make your pages inherit from it, so you've got one place with all common functionality.

For example:

public abstract class BasePage<T> : System.Web.UI.Page
{
    protected T GetObjectById(int objectId)
    {
        // return new T();
    }
    protected void SaveObject(T obj)
    {
        // Save object to DB here
    }
    protected void DeleteObjectById(int objectId)
    {
        // Delete object
    }

    protected abstract void PopulateUI(T obj);

    protected override void OnLoad(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int objectId = Convert.ToInt32(Request.QueryString.Get("id"));
            T obj = GetObjectById(objectId);
            PopulateUI(obj);
        }
    }
}

Your pages would then inherit from this:

public class AddGame : BasePage<Game>
{
    protected override void PopulateUI(Game game)
    {
        // Populate the UI with game information

        GameNameTextBox.Text = game.Name;
        PublisherNameTextBox.Text = game.Publisher.Name;

        // etc
    }
}

This should make creating the pages much quicker and easier, and gives you a bit more control over how data is retrieved and saved.

Mun
I like this answer too, but will probably go with one of the pre-existing scaffolding options. Thanks!
Gary the Llama