ASP.Net DataGrid
Posted Wednesday, February 22nd, 2006 8:33:00 am Now playing: Korn - Twisted Transistor (2:58)
A while ago, I had resigned myself to the fact that I would have to custom-build my own shopping cart for a site I'm working on. Well, I'm sure I could have bought something to do relatively the same thing, but what fun is that for a coder like me?
Anyway, I got to the part where I actually display the items in the cart. I was happily coding along in my usual ASP mindset, adding forms, hidden input tags, and... whoops. That's not going to work, this is ASP.Net. Doh.
So I switched out to a DataGrid. The first thing I figured out is that I have absolutely no idea how to use a DataGrid. The second thing I figured out, and this one took a while, is that the CssClass property of the styles are applied to the table row, and not each table cell. Very annoying, as I ended up creating my own custom class to fix that. Easy enough:
Next came figuring out how to add a title to the grid. If you notice on all of the sites I host, there's a title bar for each table in the content section. Well, there wasn't an easy way to do this. I couldn't figure out how to access the table control that the DataGrid populates, so I ended up just stacking a one-row table on top of it.
Now I had to figure how to put the total of the order in the footer, but there are enough good articles on how to do that, so that didn't take long. Adding a remove button to remove a product someone doesn't want from the cart was fairly easy as well.
The fun came in having to allow them to change the quantity on the fly. That turned out to be more of a project than I was expecting. There were two approaches I was considering, and of course I took the difficult one: using the onblur method of an input tag to populate two hidden fields with the ItemID I want to update the quantity of and the new quantity, as well as activate a hidden button on the form.
Phew! I wasn't expecting so much work out of what would be maybe an hour of work in classic ASP.
A while ago, I had resigned myself to the fact that I would have to custom-build my own shopping cart for a site I'm working on. Well, I'm sure I could have bought something to do relatively the same thing, but what fun is that for a coder like me?
Anyway, I got to the part where I actually display the items in the cart. I was happily coding along in my usual ASP mindset, adding forms, hidden input tags, and... whoops. That's not going to work, this is ASP.Net. Doh.
So I switched out to a DataGrid. The first thing I figured out is that I have absolutely no idea how to use a DataGrid. The second thing I figured out, and this one took a while, is that the CssClass property of the styles are applied to the table row, and not each table cell. Very annoying, as I ended up creating my own custom class to fix that. Easy enough:
Public Class DataGrid Inherits WebControls.DataGrid Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs) MyBase.OnPreRender(e) For Each dgiItem As DataGridItem In Me.Items For Each tc As TableCell In dgiItem.Cells If dgiItem.ItemType = ListItemType.Item Then tc.CssClass = Me.ItemStyle.CssClass ElseIf dgiItem.ItemType = ListItemType.AlternatingItem Then tc.CssClass = Me.AlternatingItemStyle.CssClass End If Next Next End Sub End Class
Next came figuring out how to add a title to the grid. If you notice on all of the sites I host, there's a title bar for each table in the content section. Well, there wasn't an easy way to do this. I couldn't figure out how to access the table control that the DataGrid populates, so I ended up just stacking a one-row table on top of it.
Now I had to figure how to put the total of the order in the footer, but there are enough good articles on how to do that, so that didn't take long. Adding a remove button to remove a product someone doesn't want from the cart was fairly easy as well.
The fun came in having to allow them to change the quantity on the fly. That turned out to be more of a project than I was expecting. There were two approaches I was considering, and of course I took the difficult one: using the onblur method of an input tag to populate two hidden fields with the ItemID I want to update the quantity of and the new quantity, as well as activate a hidden button on the form.
Phew! I wasn't expecting so much work out of what would be maybe an hour of work in classic ASP.
Category
DataGrid
DataGrid
Comments