开发者

ASP.NET 2.0 Correct Control For Adding to a DB

I am looking for some advice. I have been developing for about 11 years on the Oracle EnterpriseOne platform and am now making the leap to c#. So I am no stranger to programming but there are a couple of concepts I am struggling to tie up in my head when creating c# ASP.NET webpages.

I have the following scenario:

So I have an order header + an order detail table and I want to create a new order, there could potentially be multiple order lines for each order header.

In EnterpriseOne I would have a landing screen where could search, select and add new orders. When you click the add button i would load a header screen (no grid) where you would enter all the order header info, common to all order detail records. When clicking OK (to the header screen) I would display a detail screen (grid) where you could add the multiple order detail records.

So how do I do this in c# ASP.NET 2.0? I would like to create an N-tier application.

  1. I have created new classes (OrdersHeader + Order Detail) which match to my order header and order detail tables.
  2. I have created a new class (OrdersDB) which interacts with the order header/detail classes and the database (using ADO.NET and stored procedures).
  3. On the landing screen I have created an ObjectDataSource pointing to OrdersDB and the SelectMothod... so this will return a collection of OrderDetails and allow the user to seach and select on existing orderdetails.
  4. I create a header screen (when they click add) which will contain a list of text boxes +labels. When clicking OK I will create a instance of a new OrderHeader class from the values on screen and pass to the insert method of OrdersDB. is this correct?
  5. On the details screen which control should I use to let the user add order details? I was assuming I could use the GridVie开发者_JAVA百科w which was set up to allow Inserting but when inquiring on an empty DB this is bringing back a blank grid (and now allowing me to add new rows).

I hope thats clear enough. I am sure it is obvious what i need to do, but i think years of working in another platform has confused the issue for me.


It really depends on the UI you are catering for.

If you want a single screen where you can basically directly edit rows in the database (edit values, delete records, add records) your probably looking for a GridView

It also depends on your comfort level/experience with ASP.NET Web Forms. Web Forms is great in the "drag and drop" sense (which IMO is also it's downfall). Meaning you can chuck a control on a page and be ready to go.

But sometimes it's overkill. I prefer to use the web controls only when absolutely necessary.

I would be keeping with a simple form, basic form fields, then constructing the entity on the server and passed onto the business tier for validation and persistence. Have full control over the process.

By the way, i don't see in your question how any of your solutions so far is N-Tier? If you want N-Tier, you should be communicating with your data tier (which would be on a seperate computer to your web tier) via some sort of web service (ie WCF), no directly calling OrdersDB


I think you got the right idea. The problem is how do you do inserts with a grid view, and unfortunately it does not come with an insert item template. The easiest way to do this is just put your insert controls in a panel and show the controls when they click the add item button.

You can use the grid view for inserting, by adding the insert controls to the footer item templates, but you will have to handle the hiding and showing of the footer row yourself. Also the footer row will not appear if you are binding to an empty dataset, so you have to populate the gridview with a dummy record or something to get the footer to show up when there are no items so there are a few hoops to jump though, but if you google it you should find some examples. Here is one that looks valid: http://www.aspdotnetfaq.com/Faq/How-to-insert-row-in-GridView-with-SqlDataSource.aspx

There are also other controls you can use like detailsview and formview, but honestly I never use them because by the time you add in complexity like transactions and custom validations and all that it can be a pain and I think it is just easier to handle all the code manually for the edit and insert unless it is a really simple situation. They are nice for their ability to auto generate fields and labels from the database schema, so they can be useful.

So for me at least, I use grid view for viewing, and sometimes editing, but rarely for inserting, and I rarely use the built in updating with the two way data binding. Usually if I edit in grid view I will just use the edit button to capture the edit item index, and grab the key with the command argument or the griddatakeys and handle the update code myself. Not saying this is the best way, it is just how I most often do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜