ASP.NET is cool + DataGrid Designer Tips
Forget my previous rants about ASP.NET. I was plain wrong. This stuff is cool!
I gave myself a little project : create a food ordering site for my company's intranet. Nothing fancy. but it should put me through all the basics:
1. Creating a data tier
2. creating a business tier
3. using the ASP.NET UI to do CRUD work.
4. manage users using Forms Authentication
5. Create a Web.UI.Page framework : page class inherited by all the site's pages
6. create custom Web User Controls to serve as headers,footers, menu's etc..
and so on...
So far i've managed to get through 1,2,3,5 abd 6. created 2 pages so far using the data grid and the Repeater control to update and delete user's and restaurants. I must say that ASP.NET is pretty darn cool - once you get into it's Zen. Once you do that - everything starts falling into place.
There are a lot of quirks in this story. Being a Windows dev guy practically my whole career - I shouldnt really be that excited about putting a grid on a page, filling it out and using its events to update and retrieve data. but i am. because i totally see how ASP programmers had to battle this thing is the "old" days (a few months ago...?) plus now i can see me writing my own website (soon... grasshopper!)
I've also enlisted the help of a very helpful book :
ASP.NET WebSite Programming - Problem-Design-Solution
By Marco Bellinaso and Kevin Hoffman - the C# edition.
This book is NOT for the ASP.NET beginner. When i picked it up at first, i was totally blown away by the amount of knowledge that was assumed. but the steep learning cure paid off! Once i got through the initial hurdles - the book totally makes sense. It describes a community website from bottom to top - 3- tiered architecture - with all the explantions i wanted. This was good for me having done 3-tiered winform and VB6 apps in the past, so i totally got the Data Layer and the Business layer design. th UI was a pure bonus.
The thing that bothered me the most before reading this book was not the small stuff - how to do datagrids and so on - that's peanuts. What i was worried about was not having a "pattern" in my head for creating tiered applications in the .NET framework. I was not really sure how i should handle the ADO.NET change from ADO in VB6. Should i bubble up datasets? How should i implement Classes representing tables? Does the framework provide a better way (through the newly found OO features) to expose a data layer to the business layer?
At first i was running on full gas - trying out typed datasets, designer produced Data adapters and CodeBehind and all that stuff. i soon realised I must get away from all that crap. It turns out that the basic design patterns I have used and learned in VB6 still apply in 98% of the design in .NET. There were a few small imporvements, but the real pattern logic stayed the same for me - * Build Data layer with Classes to represent Tables and Stored procedure calls on them, * Build business objects representing logical objects that use the Table objects in the data layer and finally - * build a UI that uses the Business objects framework.
Some design changes from VB;
* In VB I Used the "Facade" design pattern using a global object that represents the database, with properies that are objects representing the tables in it. Now i use just classes that can be used both as logical objects and , using static methods, can also do some helper functions that i would use a different object for in VB 6
* I now return Datasets from the business logic - which i wouldn't do in VB 6. in VB6 i would only contact the UI using objects, never recordsets. but the diconnected nature of ADO.NET lets me do this. hey, thats exactly what the dataset class was made for , right?
There are probably more, smaller , design changes i'm not mentioning. Turning back to ASP,NET - i'll just say this about the data grid: At first i was a bit intimidated by the amount of code required to use template columns and template items, but then i discovered something wonderful (yes i know this will sound geeky!) : You can totally design the Column and item templaes visually using drag and drop!. really. to do this:
1. create some template columns (empty columns). either write them down in the HTML, or use the "property builder" in the context menu of the DataGrid designer.
2. once you have those columns, turn back to the datagrid in the designer( not in html mode), and then right click on it. you'll see and "Edit Template" sub menu on the Context menu, which allows you to select which template column you'd like to visually design. just select one of the columns, and voila! you can just drag and drop into that template whatever you like - textboxes, lables etc.. it's just a like another web page design surface. hack, you can drag whole tables inside there. imagine writing that manually.
The nice this is that in the visual designer of the Template column, you can design both the ItemTemplate and the EditItemTemplate columns, allowing you to work really really fast!
3. Setting the Bound properties of the visual objects in the designer:
- select the textbox or label or whatever and open the propeties window
- select the top property - "DataBindings" and click the button
- Selected the property from the list you wich to bind, and on the right select "custom binding expression"
- write the expression as you would in the HTML :
"DataBinder.Eval(Container.DataItem,"MyProperty")"
-voila - you have a bound object in the datagrid without writing asingle line of HTML.
-you can open the HTML designer and see that the generated code is exactly as you would write it (except the newline formatting..)
p.s
I know i probably have lots of typos in this post, but i was in rush and just had to spit it out.