Click here to Skip to main content
15,888,022 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
My query is that i am doing a shopping cart site.I have a repeater control loaded with products and when user clicks on a specific product ,the selected product should get loaded to the gridview which is at the rightcorner .It should happen without postback and when user selects another product the gridview should get updated with another product.How to attain the result. Expecting ur suggestions.
Thank you
Posted
Comments
AshishChaudha 30-Oct-12 8:14am    
Whats the issue??
fahad.1 30-Oct-12 8:37am    
I want to add an item to the cart when I click on selected item ...
Should I need to paste my code????
[no name] 30-Oct-12 8:54am    
yes offcourse you need to provide some code sippnets. We can see in your computer to help you.

if u have any problem, post ur code and explain problem... otherwise the general solution is,

1.use update panel and
2.repeater's item command,

C#
<![CDATA[<%@ Page Language="C#" AutoEventWireup="True" %>
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="C#"  runat="server">
       protected void Page_Load(Object Sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ArrayList values = new ArrayList();
                values.Add(new PositionData("Microsoft", "Msft"));
                values.Add(new PositionData("Intel", "Intc"));
                values.Add(new PositionData("Dell", "Dell"));
                Repeater1.DataSource = values;
                Repeater1.DataBind();
            }
        }

        protected void R1_ItemCommand(Object Sender, RepeaterCommandEventArgs e) 
        {
            ArrayList data = new ArrayList();
            data.Add(new Data(((Button)e.CommandSource).Text));
            GridView1.DataSource = data;
            GridView1.DataBind();
       }

        public class Data
        {
            string name;
            public Data(string name)
            {
                this.name = name;
            }
            public string Name
            {
                get{return name;}
            }
        }
        public class PositionData
        {
            private string name;
            private string Id;
            public PositionData(string name, string id)
            {
                this.name = name;
                this.Id = id;
            }
            public string Name
            {
                get{return name;}
            }
            public string id
            {
                get{return Id;}
            }
        }
    </script>
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>Repeater Example</title>
 </head>
  <body>
    <h3>Repeater Example</h3>
     <form id="form1" runat="server">
     <asp:ScriptManager ID="ScriptManager1" runat="server">
     </asp:ScriptManager>
     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
     <ContentTemplate>
       <b>Repeater1:</b>
       <br />
       <asp:Repeater id="Repeater1" OnItemCommand="R1_ItemCommand" runat="server">
          <HeaderTemplate>
             <table border="1">
                <tr>
                   <td><b>Company</b></td>
                   <td><b>Id</b></td>
                </tr>
          </HeaderTemplate>
          <ItemTemplate>
             <tr>
                <td> <%# DataBinder.Eval(Container.DataItem, "id") %> </td>
                <td> <ASP:Button ID="Button1" Text=<%# DataBinder.Eval(Container.DataItem, "Name") %> runat="server" /></td>
             </tr>
          </ItemTemplate>
          <FooterTemplate>
             </table>
          </FooterTemplate>
       </asp:Repeater>
       <asp:GridView ID="GridView1" runat="server">
       </asp:GridView>
       </ContentTemplate>
       </asp:UpdatePanel>
       </form>
       </body>
</html>


Ref:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcommand.aspx[^]

For any clarifications, reply to this...
Mark it if u find this useful..
 
Share this answer
 
v5
XML
this is my shoppingcart.aspx page code.....
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="ShoppingCart.aspx.cs" Inherits="SessionShoppingCart.ShoppingCart" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:Repeater runat="server" ID="rptCategories" DataSourceID="CategoriesData" OnItemCommand="rptCategories_ItemCommand">

            <asp:Button CssClass="fah" ID="Button1" runat="server" Text='<%# Eval("CategoryName") %>'
                CommandArgument='<%# Eval("CategoryID") %>' />


    <asp:SqlDataSource ID="CategoriesData" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]">
    <asp:Repeater runat="server" ID="Repeater1" DataSourceID="ProductsData" OnItemCommand="Repeater1_ItemCommand">

            <asp:Button CssClass="waj" ID="Button1" runat="server" Text='<%# Eval("ProductName") %>'
                CommandArgument='<%# Eval("ProductID") %>' />



    <asp:Button ID="Checkout" runat="server" Text="Check Out" OnClick="Checkout_Click" />
    <asp:SqlDataSource ID="ProductsData" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT Products.ProductID, Products.ProductName, Categories.CategoryName,
        Products.UnitPrice FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID
        WHERE Products.CategoryID = @CategoryID">
        <SelectParameters>
            <asp:QueryStringParameter Name="CategoryID" QueryStringField="CategoryID" />
        </SelectParameters>




Here I want to put an item into cart when I click on it...


Now this is code behide page.....



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SessionShoppingCart
{
    public partial class ShoppingCart : System.Web.UI.Page
    {
        Decimal cost;
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Basket_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Literal total = (Literal)e.Row.FindControl("TotalPrice");
                total.Text = cost.ToString("c");
            }
        }
     //   protected void AddToCart_Click(object sender, EventArgs e)
     //   {
     //       var selectedProducts = Products.Rows.Cast<GridViewRow>()
     //.Where(row => ((CheckBox)row.FindControl("SelectedProducts")).Checked)
     //.Select(row => Products.DataKeys[row.RowIndex].Value.ToString()).ToList();
     //       if (Session["Cart"] == null)
     //       {
     //           Session["Cart"] = selectedProducts;
     //       }
     //       else
     //       {
     //           var cart = (List<string>)Session["Cart"];
     //           foreach (var product in selectedProducts)
     //               cart.Add(product);
     //           Session["Cart"] = cart;
     //       }
     //       foreach (GridViewRow row in Products.Rows)
     //       {
     //           CheckBox cb = (CheckBox)row.FindControl("SelectedProducts");
     //           if (cb.Checked)
     //               cb.Checked = false;
     //       }
     //   }
        protected void Checkout_Click(object sender, EventArgs e)
        {
            if (Session["Cart"] != null)
                Response.Redirect("Checkout.aspx");
        }
        protected void Products_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        protected void Categories_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        protected void rptCategories_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Response.Redirect("~/ShoppingCart.aspx?CategoryID=" + e.CommandArgument);
        }
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            
            
        }
        
    }
}
 
Share this answer
 
and this my cart page....
XML
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="Checkout.aspx.cs" Inherits="SessionShoppingCart.Checkout" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <asp:GridView ID="Basket" runat="server" AutoGenerateColumns="False" GridLines="None"
        EnableViewState="False" ShowFooter="True" DataKeyNames="ProductID" OnRowCreated="Basket_RowCreated">
        <Columns>
            <asp:TemplateField HeaderText="Remove">
                <ItemTemplate>
                    <asp:CheckBox ID="RemovedProducts" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Product" SortExpression="ProductName">
                <ItemTemplate>
                    <asp:Label ID="ProductName" runat="server" Text='<%# Eval("ProductName") %>' />
                </ItemTemplate>
                <FooterTemplate>
                    <strong>Total Price: </strong>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Price" SortExpression="UnitPrice">
                <ItemTemplate>
                    <asp:Label ID="UnitPrice" runat="server" Text='<%# Eval("UnitPrice", "{0:c}") %>' />
                </ItemTemplate>
                <FooterTemplate>
                    <strong>
                        <asp:Literal ID="TotalPrice" runat="server" />
                    </strong>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:Button ID="RemoveProduct" runat="server" Text="Remove From Basket" OnClick="RemoveProduct_Click" />
    &nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="ConfirmPurchase" runat="server" Text="Confirm Purchase" />
    <asp:SqlDataSource ID="BasketData" runat="server"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>">
    </asp:SqlDataSource>
</asp:Content>


and this is my .cs page


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace SessionShoppingCart
{
    public partial class Checkout : System.Web.UI.Page
    {
        Decimal cost;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Cart"] == null)
                Response.Redirect("ShoppingCart.aspx");
            BindBasket();
        }
        protected void RemoveProduct_Click(object sender, EventArgs e)
        {
            var cart = (List<string>)Session["Cart"];
            var removedProducts = Basket.Rows.Cast<GridViewRow>()
              .Where(row => ((CheckBox)row.FindControl("RemovedProducts")).Checked)
              .Select(row => Basket.DataKeys[row.RowIndex].Value.ToString()).ToList();
            cart.RemoveAll(removedProducts.Contains);
            BindBasket();
        }
        protected void BindBasket()
        {
            var sql = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE ProductID IN ({0})";
            var values = (List<string>)Session["Cart"];
            if (values.Count > 0)
            {
                var parms = values.Select((s, i) => "@p" + i.ToString()).ToArray();
                var inclause = string.Join(",", parms);
                BasketData.SelectCommand = string.Format(sql, inclause);
                BasketData.SelectParameters.Clear();
                for (var i = 0; i < parms.Length; i++)
                {
                    BasketData.SelectParameters.Add(parms[i].Replace("@", ""), values[i]);
                }
                DataView view = (DataView)BasketData.Select(DataSourceSelectArguments.Empty);
                var costQuery = view.Cast<DataRowView>().Select(drv => drv.Row.Field<decimal>("UnitPrice"));
                cost = costQuery.Sum();
                Basket.DataSource = view;
                Basket.DataBind();
            }
        }
        protected void Basket_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Literal total = (Literal)e.Row.FindControl("TotalPrice");
                total.Text = cost.ToString("c");
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900