Include Add to Cart in a Datagrid
-
- Lieutenant, Jr. Grade (LT JG)
- Posts: 44
- Joined: Thu Feb 28, 2008 6:21 am
- Location: Pittsburgh, PA (Go Steelers!)
Include Add to Cart in a Datagrid
I need some help from someone more familiar with .NET in general. I created a new search page in table format, using DataGrid, but now I want to have an Add to Cart link in the furthest right column. Below is my code, in case anyone wants a simple table, and maybe someone will be kind enough to share code on how to add the user control.
From research done to date, it looks like I'll need to create the datagrid programmatically, rather than using this simpler method.
Thanks,
Sheldon
<asp:DataGrid ID="ProductList" runat="server" AutoGenerateColumns="False" BorderStyle="None" CellPadding="4" GridLines="None">
<Columns>
<asp:HyperLinkColumn DataTextField="Name" DataNavigateURLField="NavigateUrl" HeaderText="Name" />
<asp:BoundColumn DataField="Description" HeaderText="Description" />
<asp:BoundColumn DataField="Price" HeaderText="Price" DataFormatString="{0:C}" >
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Right"
Wrap="False" />
</asp:BoundColumn>
</Columns>
<AlternatingItemStyle BackColor="InactiveCaptionText" />
<HeaderStyle BackColor="GradientInactiveCaption" Font-Bold="True" />
</asp:DataGrid>
From research done to date, it looks like I'll need to create the datagrid programmatically, rather than using this simpler method.
Thanks,
Sheldon
<asp:DataGrid ID="ProductList" runat="server" AutoGenerateColumns="False" BorderStyle="None" CellPadding="4" GridLines="None">
<Columns>
<asp:HyperLinkColumn DataTextField="Name" DataNavigateURLField="NavigateUrl" HeaderText="Name" />
<asp:BoundColumn DataField="Description" HeaderText="Description" />
<asp:BoundColumn DataField="Price" HeaderText="Price" DataFormatString="{0:C}" >
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Right"
Wrap="False" />
</asp:BoundColumn>
</Columns>
<AlternatingItemStyle BackColor="InactiveCaptionText" />
<HeaderStyle BackColor="GradientInactiveCaption" Font-Bold="True" />
</asp:DataGrid>
Take at look at the categorygridpage user controls. They leverage both programmatic gridview (or datalist) concept and implement add-to-cart functionality.
There is an add-to-cart user control that makes adding that particular functionality easier - it does have certain design expectations though so it takes a little study time.
Sounds like that's where you are headed with your customization.
There is an add-to-cart user control that makes adding that particular functionality easier - it does have certain design expectations though so it takes a little study time.
Sounds like that's where you are headed with your customization.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
-
- Lieutenant, Jr. Grade (LT JG)
- Posts: 44
- Joined: Thu Feb 28, 2008 6:21 am
- Location: Pittsburgh, PA (Go Steelers!)
Thanks Joe. I'm going to look into using datalist to build these tables instead of datagrid.
Thanks,
Sheldon
Thanks,
Sheldon
Last edited by beveled on Mon Mar 24, 2008 7:02 am, edited 1 time in total.
Hey, I didn't even know what a DataGrid was this time last year
Forget about "tables". Think in terms of repeating data in a consistent fashion.
GridView lets you repeat data in a fashion quite similar to HTML tables, with the ability to act upon each row of the data with tiggers and programming code. Generally GridView controls are used on a "recordset" but they can also be used on different sources of data.
Datalists are more simplistic and tend to work with a single "column". Think in terms of repeating the same single column from a table either across the page or down the page. Like the mailing labels on a sheet of paper, you can repeat the label vertically or repeat them across then down.
Both controls will work directly with a data source like the AC7 tables, or you can "create" a data source programmatically in the code and assign it to the controls.

Forget about "tables". Think in terms of repeating data in a consistent fashion.
GridView lets you repeat data in a fashion quite similar to HTML tables, with the ability to act upon each row of the data with tiggers and programming code. Generally GridView controls are used on a "recordset" but they can also be used on different sources of data.
Datalists are more simplistic and tend to work with a single "column". Think in terms of repeating the same single column from a table either across the page or down the page. Like the mailing labels on a sheet of paper, you can repeat the label vertically or repeat them across then down.
Both controls will work directly with a data source like the AC7 tables, or you can "create" a data source programmatically in the code and assign it to the controls.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
-
- Lieutenant, Jr. Grade (LT JG)
- Posts: 44
- Joined: Thu Feb 28, 2008 6:21 am
- Location: Pittsburgh, PA (Go Steelers!)
I tried adding <uc:AddToCartLink ID="Add2Cart" runat="server" ProductId='<%#Eval("ProductId")%>' /> to my datagrid as a bound column, but got an error message saying that wasn't in the table. I'd have to try it again to get the exact error message.Generally GridView controls are used on a "recordset" but they can also be used on different sources of data.
Thanks,
Sheldon
That's not enough information for me to be much help. If you post the entire code you're trying to do, I might be able to help more.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
-
- Lieutenant, Jr. Grade (LT JG)
- Posts: 44
- Joined: Thu Feb 28, 2008 6:21 am
- Location: Pittsburgh, PA (Go Steelers!)
Here's what I ended up with:
<asp:DataList ID="ProductList" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="100%"
OnItemDataBound="ProductList_ItemDataBound" DataKeyField="ProductId" CssClass="catalog" EnableViewState="false" HorizontalAlign="left">
<ItemStyle HorizontalAlign="left" VerticalAlign="top" CssClass="tableNode" />
<ItemTemplate>
<tr>
<td><asp:PlaceHolder ID="phItemTemplate1" runat="server"></asp:PlaceHolder></td>
<td><asp:Label ID="ProductDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label></td>
<td><uc:ProductPrice ID="Price" runat="server" Product='<%#Container.DataItem%>' /></td>
<td><asp:PlaceHolder ID="phItemTemplate2" runat="server"></asp:PlaceHolder>/<td>
<td><div style="margin-top:10px;margin-bottom:20px"><uc:AddToCartLink ID="Add2Cart" runat="server" ProductId='<%#Eval("ProductId")%>' /></div></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor="#C4DAFC">
<td><asp:PlaceHolder ID="phItemTemplate1" runat="server"></asp:PlaceHolder></td>
<td><asp:Label ID="ProductDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label></td>
<td><uc:ProductPrice ID="Price" runat="server" Product='<%#Container.DataItem%>' /></td>
<td><asp:PlaceHolder ID="phItemTemplate2" runat="server"></asp:PlaceHolder>/<td>
<td><div style="margin-top:10px;margin-bottom:20px"><uc:AddToCartLink ID="Add2Cart" runat="server" ProductId='<%#Eval("ProductId")%>' bgcolor="#CCCCCC"/></div></td>
</tr>
</AlternatingItemTemplate>
</asp:DataList>
Next I'd like to figure out how to change the bgcolor of the user control for the alternating rows, but that's a detail.
<asp:DataList ID="ProductList" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="100%"
OnItemDataBound="ProductList_ItemDataBound" DataKeyField="ProductId" CssClass="catalog" EnableViewState="false" HorizontalAlign="left">
<ItemStyle HorizontalAlign="left" VerticalAlign="top" CssClass="tableNode" />
<ItemTemplate>
<tr>
<td><asp:PlaceHolder ID="phItemTemplate1" runat="server"></asp:PlaceHolder></td>
<td><asp:Label ID="ProductDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label></td>
<td><uc:ProductPrice ID="Price" runat="server" Product='<%#Container.DataItem%>' /></td>
<td><asp:PlaceHolder ID="phItemTemplate2" runat="server"></asp:PlaceHolder>/<td>
<td><div style="margin-top:10px;margin-bottom:20px"><uc:AddToCartLink ID="Add2Cart" runat="server" ProductId='<%#Eval("ProductId")%>' /></div></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor="#C4DAFC">
<td><asp:PlaceHolder ID="phItemTemplate1" runat="server"></asp:PlaceHolder></td>
<td><asp:Label ID="ProductDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label></td>
<td><uc:ProductPrice ID="Price" runat="server" Product='<%#Container.DataItem%>' /></td>
<td><asp:PlaceHolder ID="phItemTemplate2" runat="server"></asp:PlaceHolder>/<td>
<td><div style="margin-top:10px;margin-bottom:20px"><uc:AddToCartLink ID="Add2Cart" runat="server" ProductId='<%#Eval("ProductId")%>' bgcolor="#CCCCCC"/></div></td>
</tr>
</AlternatingItemTemplate>
</asp:DataList>
Next I'd like to figure out how to change the bgcolor of the user control for the alternating rows, but that's a detail.
- m_plugables
- Commander (CMDR)
- Posts: 149
- Joined: Tue Mar 11, 2008 12:44 am
- Contact:
- m_plugables
- Commander (CMDR)
- Posts: 149
- Joined: Tue Mar 11, 2008 12:44 am
- Contact:
place this info inside the UC tag in ascx file which contains the html. For example
Similarly if you want to use some styles from css(casecadding style sheet) then you can specify the css style in the
ItemStyle-CssClass, AlternatingItemStyle-CssClass. For example you want to apply two differant styles on alternating rows like
color, font-family etc. Then first you should provide those styles in css file of current theme for example we have two classes
even and odd for alternating rows then the code using this will be like
Code: Select all
<asp:DataList ID="ProductList" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="100%"
OnItemDataBound="ProductList_ItemDataBound" DataKeyField="ProductId" CssClass="catalog" EnableViewState="false" HorizontalAlign="left"
AlternatingItemStyle-BackColor="Chocolate" ItemStyle-BackColor="Yellow">
ItemStyle-CssClass, AlternatingItemStyle-CssClass. For example you want to apply two differant styles on alternating rows like
color, font-family etc. Then first you should provide those styles in css file of current theme for example we have two classes
even and odd for alternating rows then the code using this will be like
Code: Select all
<asp:DataList ID="ProductList" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="100%"
OnItemDataBound="ProductList_ItemDataBound" DataKeyField="ProductId" CssClass="catalog" EnableViewState="false" HorizontalAlign="left"
AlternatingItemStyle-CssClass="odd" ItemStyle-CssClass"even">
You will have to play a bit with CSS to achieve the desired results. Once you are able to specify a css-class for a particular display item then you can control almost anything about that item.
I would suggest that you try a few things with CSS. For example create two classes 'MyOddClass' and 'MyEvenClass'. Set these these classes to the DataList control. The go on experimenting with the CSS for MyOddClass and MyEvenClass. You will not be disappointed.
setting background color in css
I would suggest that you try a few things with CSS. For example create two classes 'MyOddClass' and 'MyEvenClass'. Set these these classes to the DataList control. The go on experimenting with the CSS for MyOddClass and MyEvenClass. You will not be disappointed.
setting background color in css
Code: Select all
background-color: #000080;
They need to appear inside your CSS file.
If you are using something like this in ASPX file
then you need to have 'MyOddClass' and 'MyEvenClass' in your style.css file of your currently active theme.
For example something like this in sytle.css should exist
If you are using something like this in ASPX file
Code: Select all
<asp:DataList ID="ProductList" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="100%"
OnItemDataBound="ProductList_ItemDataBound" DataKeyField="ProductId" CssClass="catalog" EnableViewState="false" HorizontalAlign="left"
AlternatingItemStyle-CssClass="MyOddClass" ItemStyle-CssClass"MyEvenClass">
For example something like this in sytle.css should exist
Code: Select all
.MyOddClass
{
background-color: #DBDADA;
padding: 6px 2px 6px 6px;
}
.MyEvenClass
{
background-color: #F5F5F5;
padding: 6px 2px 6px 6px;
}
-
- Lieutenant, Jr. Grade (LT JG)
- Posts: 44
- Joined: Thu Feb 28, 2008 6:21 am
- Location: Pittsburgh, PA (Go Steelers!)
I see thanks. Looks like that's what Mazhar was suggesting as a possible step #2 if step #1 didn't work. The issue that I have with this is that it did change the color, but in the wrong place.
I don't think this will change only the background color of my user control, for alternating items, when you consider the HTML tags in my code (used for creating a table-like style).
Thanks,
Sheldon
I don't think this will change only the background color of my user control, for alternating items, when you consider the HTML tags in my code (used for creating a table-like style).
Thanks,
Sheldon
-
- Lieutenant, Jr. Grade (LT JG)
- Posts: 44
- Joined: Thu Feb 28, 2008 6:21 am
- Location: Pittsburgh, PA (Go Steelers!)
Re: Include Add to Cart in a Datagrid
With this code, I've attempted to add the AlternatingItemStyle-CssClass, but it is not taking effect. Is it possibly being overridden by the template?
Code: Select all
<asp:DataList ID="ProductList" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="100%"
OnItemDataBound="ProductList_ItemDataBound" DataKeyField="ProductId" CssClass="catalog" EnableViewState="false" HorizontalAlign="left"
AlternatingItemStyle-CssClass="altitemclass">
<ItemStyle HorizontalAlign="left" VerticalAlign="top" CssClass="tableNode" />
<ItemTemplate>
<tr>
<td><asp:PlaceHolder ID="phItemTemplate1" runat="server"></asp:PlaceHolder></td>
<td><asp:Label ID="ProductDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label></td>
<td><uc:ProductPrice ID="Price" runat="server" Product='<%#Container.DataItem%>' /></td>
<td><asp:PlaceHolder ID="phItemTemplate2" runat="server"></asp:PlaceHolder>/<td>
<td><div style="margin-top:10px;margin-bottom:20px"><uc:AddToCartLink ID="Add2Cart" runat="server" ProductId='<%#Eval("ProductId")%>' /></div></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor="#C4DAFC">
<td><asp:PlaceHolder ID="phItemTemplate1" runat="server"></asp:PlaceHolder></td>
<td><asp:Label ID="ProductDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label></td>
<td><uc:ProductPrice ID="Price" runat="server" Product='<%#Container.DataItem%>' /></td>
<td><asp:PlaceHolder ID="phItemTemplate2" runat="server"></asp:PlaceHolder>/<td>
<td><div style="margin-top:10px;margin-bottom:20px"><uc:AddToCartLink ID="Add2Cart" runat="server" ProductId='<%#Eval("ProductId")%>' backcolor="#C4DAFC"/></div></td>
</tr>
</AlternatingItemTemplate>
</asp:DataList>
Re: Include Add to Cart in a Datagrid
You are using AlternatingItemTemplate as well as AlternatingItemStyle-CssClass. I think your AlternatingItemStyle-CssClass settings are getting overridden by what you set in AlternatingItemTemplate.
-
- Lieutenant, Jr. Grade (LT JG)
- Posts: 44
- Joined: Thu Feb 28, 2008 6:21 am
- Location: Pittsburgh, PA (Go Steelers!)
Re: Include Add to Cart in a Datagrid
Could I trouble you to explain how to get that css code into the template?
Thanks,
Sheldon
Thanks,
Sheldon
Re: Include Add to Cart in a Datagrid
Can you remove AlternatingItemStyle-CssClass and try
instead of
Code: Select all
<AlternatingItemTemplate>
<tr class="altitemclass">
Code: Select all
<AlternatingItemTemplate>
<tr bgcolor="#C4DAFC">
-
- Lieutenant, Jr. Grade (LT JG)
- Posts: 44
- Joined: Thu Feb 28, 2008 6:21 am
- Location: Pittsburgh, PA (Go Steelers!)
Re: Include Add to Cart in a Datagrid
Unfortunately, it had no effect. Any other ideas? Are there other css styles for the user control?
Code: Select all
<asp:DataList ID="ProductList" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="100%"
OnItemDataBound="ProductList_ItemDataBound" DataKeyField="ProductId" CssClass="catalog" EnableViewState="false" HorizontalAlign="left" >
<ItemStyle HorizontalAlign="left" VerticalAlign="top" CssClass="tableNode" />
<ItemTemplate>
<tr>
<td><asp:PlaceHolder ID="phItemTemplate1" runat="server"></asp:PlaceHolder></td>
<td><asp:Label ID="ProductDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label></td>
<td><uc:ProductPrice ID="Price" runat="server" Product='<%#Container.DataItem%>' /></td>
<td><asp:PlaceHolder ID="phItemTemplate2" runat="server"></asp:PlaceHolder>/<td>
<td><div style="margin-top:10px;margin-bottom:20px"><uc:AddToCartLink ID="Add2Cart" runat="server" ProductId='<%#Eval("ProductId")%>' /></div></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="altitemclass">
<td><asp:PlaceHolder ID="phItemTemplate1" runat="server"></asp:PlaceHolder></td>
<td><asp:Label ID="ProductDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label></td>
<td><uc:ProductPrice ID="Price" runat="server" Product='<%#Container.DataItem%>' /></td>
<td><asp:PlaceHolder ID="phItemTemplate2" runat="server"></asp:PlaceHolder>/<td>
<td><div style="margin-top:10px;margin-bottom:20px"><uc:AddToCartLink ID="Add2Cart" runat="server" ProductId='<%#Eval("ProductId")%>' backcolor="#C4DAFC"/></div></td>
</tr>
</AlternatingItemTemplate>
</asp:DataList>
-
- Lieutenant, Jr. Grade (LT JG)
- Posts: 44
- Joined: Thu Feb 28, 2008 6:21 am
- Location: Pittsburgh, PA (Go Steelers!)
Re: Include Add to Cart in a Datagrid
Ah, I found the issue. It was actually a graphic issue. The text was embedded in with the little shopping cart graphic, instead of using CSS to control it's properties.
If anyone wants a transparent "add to cart" graphic, I've attached it.
Beveled
If anyone wants a transparent "add to cart" graphic, I've attached it.
Beveled