eswar rao

eswar rao

  • 1.6k
  • 98
  • 19.1k

how to add datalist item to temporary cart table in asp.net

Aug 27 2016 2:28 AM
hi every one,
 
i would like to add my datalist item to temporary cart table (data table) , when i am click on image buttton of datalist item ..plz tel me, already i am tried some , but it was showing one item detail repeated , and also if i am click on another item previous item was lost from data table please help me....
 
 
my c#  code
------------------ 
 
protected void dl_beers_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "item_plus")
{
// Label lblprice = (Label)(e.Item.FindControl("lbl_beer_price"));
Label lblcart = (Label)(e.Item.FindControl("lbl_items_order"));
Label lblitem = (Label)(e.Item.FindControl("lbl_beer_name"));
Label lblpid = (Label)(e.Item.FindControl("lbl_pid"));
int order = Convert.ToInt16(lblcart.Text);
order = order + 1;
int index = e.Item.ItemIndex;
Label lblprice = (Label)dl_beers.Items[index].FindControl("lbl_beer_price");
//for price
float price = float.Parse(lblprice.Text);
price = price * order;
Convert.ToSingle(price);
//add to cart
DataTable dt = new DataTable();
dt.Columns.Add("pid",typeof(string));
dt.Columns.Add("pname",typeof(string));
dt.Columns.Add("pprice", typeof(string));
for (int i = dt.Rows.Count; i <= Convert.ToInt32(lblcart.Text); i++) //for (int i = 0; i <= Convert.ToInt32(lblcart.Text); i++)
{
dt.Rows.Add(lblpid.Text, lblitem.Text, lblprice.Text);
addtocart();
ViewState["dt"] = dt;
this.BindGrid();
}
lblcart.Text = Convert.ToString(order);
}
if (e.CommandName == "item_minus")
{
// int ordercount = Convert.ToInt16(spn_beersorder.InnerText);
Label lblcart = (Label)(e.Item.FindControl("lbl_items_order"));
int order = Convert.ToInt16(lblcart.Text);
if (order > 0)
{
order = order - 1;
// beers_order = order;
lblcart.Text = Convert.ToString(order);
// ordercount = ordercount - 1;
// spn_beersorder.InnerText = Convert.ToString(ordercount);
}
else
{
order = 0;
lblcart.Text = Convert.ToString(order);
// spn_beersorder.InnerText = Convert.ToString(order+"x");
}
}
}
 
 
 
my aspx code
----------------------
 
 
<asp:DataList ID="dl_beers" OnItemCommand="dl_beers_ItemCommand" Width="100%" CellSpacing="10" DataKeyField="pid" ItemStyle-HorizontalAlign="Center" RepeatDirection="Vertical" RepeatColumns="1" RepeatLayout="Table" runat="server">
<HeaderTemplate>
<thead>
<tr style=" border-bottom-style: solid;border-width:thin;border-color: #EAEAEA; text-align:center;">
<th>
Item
</th>
<th>
Name
</th>
<th>
Volume
</th>
<th>
Price
</th>
<th style=" text-align:left;">
Order
</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr class="c_dl_products_alter" >
<td>
<asp:Image ID="img_item_beer" CssClass="img-responsive img img-thumbnail" Height="70px" Width="50px" ImageUrl='<%#"../admin/"+Eval("pimgpath") %>' runat="server" />
</td>
<td>
<asp:Label ID="lbl_beer_name" Font-Bold="true" runat="server" Text='<%#Eval("pname") %>'></asp:Label>
<p runat="server" id="p_beer_desc">
<%#Eval("pdisc") %>
</p> <asp:Label ID="lbl_o" runat="server" Text="Label"></asp:Label>
</td>
<td>
<asp:Label ID="lbl_beer_volume" Font-Bold="true" runat="server" Text='<%#Eval("pvolume") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lbl_beer_price" Font-Bold="true" runat="server" Text='<%#Eval("pprice") %>'></asp:Label>
</td>
<td style="text-align:center; width:110px;">
<div class="c_div_btn_cart">
<asp:ImageButton ID="imgbtn_minus" ImageUrl="img/minus.png" CssClass="img-circle img-responsive img c_imgbtns" CommandName="item_minus" CommandArgument='<%#Eval("pid") %>' runat="server" />
</div>
<div class="c_div_itemquntity">
<asp:Label ID="lbl_items_order" Font-Bold="true" CssClass="lbl_p_quantiy" runat="server" Text="0"></asp:Label>
</div>
<div class="c_div_btn_cart">
<asp:ImageButton ID="imgbtn_plus" ImageUrl="img/plus.png" CssClass="img-circle img-responsive img c_imgbtns" CommandArgument='<%#Eval("pid") %>' CommandName="item_plus" runat="server" />
</div>
<asp:Label ID="lbl_pid" runat="server" Visible="false" Text='<%#Eval("pid") %>'></asp:Label>
</td>
</tr>
</tbody>
</ItemTemplate>
</asp:DataList>
 
 // cart view
 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="pid" HeaderText="ID" ItemStyle-Width="150" >
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="pname" HeaderText="Name" ItemStyle-Width="150" >
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="pprice" HeaderText="price" ItemStyle-Width="150" >
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" Text="Update" runat="server" />
<asp:LinkButton ID="LinkButton3" Text="Cancel" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
 
 
 
 
 
 

Answers (5)