创建大量 .aspx 页面以将数据添加到各种表中。 有没有更好的办法?

发布于 07-18 12:30 字数 527 浏览 11 评论 0原文

我正在开发一个 CRUD 网站,其中有许多非常相似的表单用于添加新数据。 换句话说:

AddMovie.aspx、AddGame.aspx、AddBeer.aspx、AddAdd.aspx

我一直在心里想,“自己,如果有一个 Add.aspx 而不是重写这么多类似的页面,那就太好了 - 加上所有那些 OOP 书呆子会认为我很酷,因为我是重复使用而不是复制/粘贴!

所以假设我走在正确的轨道上,如果我要选择单一的Add.aspx 页面,我如何表示每个对象的所有字段集? 我考虑过一堆可以隐藏/显示的面板或 div,但不确定我是否真的喜欢这个解决方案。 有没有更好的方法来做到这一点,或者我应该放弃并返回到多个坏的 ol'AddObject.aspx 页面?

此外,这是一个普通的 ol' (3.5) Web 表单应用程序。 遗憾的是,这方面没有 ASP.NET MVC 的优点。 似乎应该有这样一个微不足道的解决方案,我只是想得太多了,但我想不出一个解决方案,所以我转向 Stack Overflow。 :)

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' AddObject.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. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰魂雪魄2024-07-25 12:30:33

也许您应该查看 ASP.NET 动态数据亚音速项目。 两者都允许非常快速地构建 CRUD 类型的网站,因为它们支持“脚手架”(编辑页面是根据您的数据库模型自动生成的)。

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).

森林散布2024-07-25 12:30:33

更好的方法可能是有一个名为 Add.aspx 的单个页面,然后根据您发送的查询字符串(即 Add.asps?type=game),您可以自定义表单和特定类型对象的逻辑。尝试与.

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.

深海少女心2024-07-25 12:30:33

另一种选择是子类化 Page,然后让您的页面继承它,这样您就拥有了一个具有所有常用功能的位置。

例如:

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);
        }
    }
}

您的页面将从以下继承:

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
    }
}

这应该使创建页面变得更快更容易,并且让您对数据的检索和保存方式有更多的控制。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文