Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
MasterPage

   <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div align="center" >

                <div class="divStyle">
         <div id="div1"   >
             <div align="center"><asp:Image ID="imgLogo" runat="server" ImageUrl="~/images/main logo.jpg" Height="80"  /></div>

          <div style="float:right" >
              <asp:LinkButton ID="lnkTrackOrder" runat="server" Text="Track Order"  CssClass="links"></asp:LinkButton> &nbsp;
            <asp:LinkButton ID="lnkRegister" runat="server" Text="Register" CssClass="links" OnClick="lnkRegister_Click" ></asp:LinkButton>&nbsp;
            <asp:LinkButton ID="lnkLogin" runat="server" Text="Login" CssClass="links" OnClick="lnkLogin_Click"></asp:LinkButton>&nbsp;<asp:Label ID="lblHi"  runat="server" CssClass="links"></asp:Label>&nbsp;<asp:LinkButton ID="lnkUserName" runat="server" CssClass="links"></asp:LinkButton>&nbsp;

             <asp:ImageButton ID="btnImgCart" runat="server"  ImageUrl="~/images/cart-58-64.gif" Height="35" Width="35" OnClick="btnImgCart_Click" />
            </div>
         <asp:Panel ID="PopupPanel" runat="server" Height="50" Width="70" BackColor="White" CssClass="panel" style="display:none;padding:10px"  HorizontalAlign="Left">
             <asp:LinkButton ID="lnkManage" runat="server" Text="Manage" OnClick="lnkManage_Click" CssClass="panelPopup"></asp:LinkButton><br />
             <asp:LinkButton ID="lnkLogOut" runat="server"  OnClick="lnkLogOut_Click" CssClass="panelPopup"></asp:LinkButton>
         </asp:Panel>
       <ajax:PopupControlExtender ID="PCE1" runat="server" TargetControlID="lnkUserName" Position="Bottom"  PopupControlID="PopupPanel" ></ajax:PopupControlExtender>
        <asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" CssClass="menu" StaticEnableDefaultPopOutImage="False" >
            <Items>
                <asp:MenuItem Text="Home" Value="Home" NavigateUrl="~/home.aspx" ImageUrl="~/images/home-4-m (1).png"></asp:MenuItem>
                <asp:MenuItem Text="Products" Value="Products" ImageUrl="~/images/product-m.png">
                    <asp:MenuItem Text="Footwear" Value="New Item" ImageUrl="~/images/shoe-man-m.png" NavigateUrl="ViewProducts.aspx?ProductId=0&amp;Product=Footwear"></asp:MenuItem>
                    <asp:MenuItem ImageUrl="~/images/notebook-m.png" Text="Laptop" Value="Laptop" NavigateUrl="ViewProducts.aspx?ProductId=1&amp;Product=Laptop"></asp:MenuItem>
                    <asp:MenuItem ImageUrl="~/images/tablet-m.png" Text="Tablet" Value="Tablet" NavigateUrl="ViewProducts.aspx?ProductId=2&amp;Product=Tablet"></asp:MenuItem>
                    <asp:MenuItem ImageUrl="~/images/time-4-m.png" Text="Watch" Value="Watch" NavigateUrl="ViewProducts.aspx?ProductId=3&amp;Product=Watch"></asp:MenuItem>
                </asp:MenuItem>
                <asp:MenuItem Text="Contact Us" Value="Contact Us" ImageUrl="~/images/contacts-2-m.png"></asp:MenuItem>
            </Items>
            <StaticHoverStyle CssClass="selected" />
        </asp:Menu>
                   </div>

            </div>
                  <asp:Panel ID="QuickCartPanel" runat="server"  Width="1000" Height="500" CssClass="modalPopup" HorizontalAlign="Center" ScrollBars="Vertical">
           <div align="center">
               <div style="float:right"><asp:Image ID="imgClose" runat="server" ImageUrl="~/images/close-window-24.jpg"  style="cursor:pointer;padding:5px;" ToolTip="Close Virtual Cart"/></div>
               <br />
                <br />
               <asp:Label ID="lblCartHeader" runat="server" Text="Your Shopping Cart!" Font-Size="15" ForeColor="#0099ff"></asp:Label><br />
                <asp:Label ID="lblStatus" runat="server"></asp:Label>
               <br /><br />

                <asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="Orange" AutoGenerateColumns="false" RowStyle-CssClass="GridViewRowStyle" RowStyle-HorizontalAlign="Center" RowStyle-Height="40"  HeaderStyle-Font-Bold="true"  HeaderStyle-Height="40" CssClass="GridViewStyle" DataKeyNames="ProductId"    OnRowCommand="GridView1_RowCommand" EmptyDataText="Your Cart is Empty!"  EmptyDataRowStyle-BorderStyle="None" ShowHeaderWhenEmpty="false"  EmptyDataRowStyle-BorderColor="White"   EmptyDataRowStyle-Font-Size="Large"  Width="850"   EmptyDataRowStyle-HorizontalAlign="Center" EmptyDataRowStyle-ForeColor="Red" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound">
                         <Columns>

                    <asp:TemplateField HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Medium" HeaderStyle-Width="70" HeaderText="Type" HeaderStyle-ForeColor="White">

                        <ItemTemplate>
                            <asp:Label ID="lblProductType" runat="server" Text='<%# Eval("ProductType") %>'></asp:Label>
                        </ItemTemplate>

                        <HeaderStyle Font-Bold="False" Font-Size="Medium" ForeColor="White" Width="70px" />

                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Medium" HeaderStyle-Width="70" HeaderText="Brand" HeaderStyle-ForeColor="White">

                        <ItemTemplate>
                            <asp:Label ID="lblProductBrand" runat="server" Text='<%# Eval("ProductBrand") %>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle Font-Bold="False" Font-Size="Medium" ForeColor="White" Width="70px" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Medium" HeaderStyle-Width="70" HeaderText="Image" HeaderStyle-ForeColor="White" ItemStyle-Width="70">
                        <ItemTemplate>
                            <asp:Image ID="img" runat="server"  ImageUrl='<%# "~/admin/images/" + Eval("ProductImage") %>' Height="50"></asp:Image>
                        </ItemTemplate>

                        <HeaderStyle Font-Bold="False" Font-Size="Medium" ForeColor="White" Width="70px" />
                        <ItemStyle Width="70px" />

                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Medium" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="260" HeaderText="Product Name" HeaderStyle-ForeColor="White">
                        <ItemTemplate>
                            <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle Font-Bold="False" Font-Size="Medium" ForeColor="White" />
                        <ItemStyle HorizontalAlign="Left" Width="260px" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Medium" ItemStyle-Width="90" HeaderText="Price" HeaderStyle-ForeColor="White">
                        <ItemTemplate>
                            <asp:Label ID="ProductPrice" runat="server" Text='<%# Eval("ProductPrice","{0:C}") %>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle Font-Bold="False" Font-Size="Medium" ForeColor="White" />
                        <ItemStyle Width="90px" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Medium" ItemStyle-Width="90" HeaderText="Qty" HeaderStyle-ForeColor="White"  ItemStyle-VerticalAlign="Middle">
                        <ItemTemplate>
                            <asp:DropDownList ID="chkQty" runat="server" AutoPostBack="true" OnSelectedIndexChanged="chkQty_SelectedIndexChanged">                                <asp:ListItem Text="1" Value="1"></asp:ListItem>
                                <asp:ListItem Text="2" Value="2"></asp:ListItem>
                                <asp:ListItem Text="3" Value="3"></asp:ListItem>
                                <asp:ListItem Text="4" Value="4"></asp:ListItem>
                                <asp:ListItem Text="5" Value="5"></asp:ListItem>
                                <asp:ListItem Text="6" Value="6"></asp:ListItem>
                                <asp:ListItem Text="7" Value="7"></asp:ListItem>
                                <asp:ListItem Text="8" Value="8"></asp:ListItem>
                                <asp:ListItem Text="9" Value="9"></asp:ListItem>
                                <asp:ListItem Text="10" Value="10"></asp:ListItem>
                            </asp:DropDownList>

                        </ItemTemplate>

                        <HeaderStyle Font-Bold="False" Font-Size="Medium" ForeColor="White" />
                        <ItemStyle Width="90px" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Medium" ItemStyle-Width="90" HeaderText="Sub Total" HeaderStyle-ForeColor="White">
                        <ItemTemplate>
                            <asp:Label ID="lblSubTotal" runat="server" Text='<%#Eval("TotalPrice","{0:C}")%>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle Font-Bold="False" Font-Size="Medium" ForeColor="White" />
                        <ItemStyle Width="90px" />

                    </asp:TemplateField>
                    <asp:ButtonField ButtonType="Image" CommandName="DeleteItem" ImageUrl="~/images/delete-24.jpg"   />
                </Columns>
            <EmptyDataRowStyle BorderColor="White" BorderStyle="None" Font-Size="Large" />
            <HeaderStyle BackColor="Orange" Font-Bold="True" Height="40px" />
            <RowStyle CssClass="GridViewRowStyle" Height="40px" HorizontalAlign="Center" />
        </asp:GridView>

           </div>
       <div align="center">
       <br />
           <br />
    <asp:Label ID="lblTot" runat="server"  Font-Size="16"></asp:Label>
          </div>
           <br />
            <asp:Button ID="btnContinue" runat="server" Text="Continue Shopping" CssClass="btn" Height="30" /> &nbsp;
            <asp:Button ID="btnCheckOut" runat="server" Text="Make Payment" OnClick="btnCheckOut_Click" CssClass="btn" Height="30" />
                   </asp:Panel>

            <ajax:ModalPopupExtender ID="ModalPopupPanel1" runat="server" TargetControlID="btnImgCart" PopupControlID="QuickCartPanel" BackgroundCssClass="modalBackground" OkControlID="imgClose" ></ajax:ModalPopupExtender>

           </div>

        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">


        </asp:ContentPlaceHolder>
            </div>

        </ContentTemplate>
      </asp:UpdatePanel>

public partial class MasterPage : System.Web.UI.MasterPage
{
    SqlConnection con = null;
    SqlCommand cmd = null;
    SqlDataAdapter da = null;
    DataSet ds = null;

    string strSqlCommand = string.Empty;
    int RowIndex,ProductId,Qty,Price,SubTotal;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
        if (!Page.IsPostBack)
        {
            loadCartDetails();
            if (con.State != ConnectionState.Open)
                con.Open();
            strSqlCommand = "SELECT sum(TotalPrice) from Cart";
            cmd = new SqlCommand(strSqlCommand, con);
            lblTot.Text = "Total Amt Rs." + cmd.ExecuteScalar().ToString();
        }
       if(Page.User.Identity.IsAuthenticated)
       {
            lblHi.Text = "Hi!";
            lnkUserName.Text =  Page.User.Identity.Name;
            lnkLogOut.Text = "LogOut";
            lnkRegister.Text = "";
            lnkLogin.Text = "";
       }
    }

    int ddlQtyLoad()
    {
        strSqlCommand = "Select Qty from Cart";
        if (con.State != ConnectionState.Open)
            con.Open();
        cmd = new SqlCommand(strSqlCommand, con);
        int qty=Convert.ToInt32(cmd.ExecuteScalar().ToString());
        return qty;

    }
    void loadCartDetails()
    {
        strSqlCommand = "Select * from Cart";
        da = new SqlDataAdapter(strSqlCommand, con);
        ds = new DataSet();
        da.Fill(ds, "Cart");
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if(e.CommandName==  "DeleteItem")
        {
            RowIndex = Convert.ToInt32(e.CommandArgument);
            strSqlCommand = "Select * from Cart";
            da = new SqlDataAdapter(strSqlCommand, con);
            ds = new DataSet();
            da.Fill(ds, "Cart");

            ProductId = Convert.ToInt32(ds.Tables["Cart"].Rows[RowIndex][1].ToString());
            strSqlCommand = "Delete from Cart where ProductId=" + ProductId;

            if (con.State != ConnectionState.Open)
                con.Open();
            cmd = new SqlCommand(strSqlCommand, con);
            if(cmd.ExecuteNonQuery()>0)
            {
                loadCartDetails();
                SuccessStatus("Product Successfully deleted from Virtual Cart!");
                ModalPopupPanel1.Show();
            }
        }
    }
    protected void btnImgCart_Click(object sender, ImageClickEventArgs e)
    {
        lblStatus.Text = "";
        loadCartDetails();
        SuccessStatus("hello");
    }

    void SuccessStatus(string str)
    {
        lblStatus.Text = str;
        lblStatus.ForeColor = System.Drawing.Color.Green;
    }
    void FailureStatus(string str)
    {
        lblStatus.Text = str;
        lblStatus.ForeColor = System.Drawing.Color.Red;
    }

    protected void lnkRegister_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registration.aspx?Type=Registration");
    }
    protected void lnkLogin_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registration.aspx?Type=Login");
    }
    protected void lnkLogOut_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("home.aspx");
    }
    protected void chkQty_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddl.NamingContainer;
        int RowIndex = row.RowIndex;
        DropDownList qtyNo = ((DropDownList)row.Cells[5].FindControl("chkQty"));
        int qty = Convert.ToInt32(qtyNo.SelectedItem.ToString());

        strSqlCommand = "Select * from Cart";
        da = new SqlDataAdapter(strSqlCommand, con);
        ds = new DataSet();
        da.Fill(ds, "Cart");
        ProductId = Convert.ToInt32(ds.Tables["Cart"].Rows[RowIndex][0].ToString());
        int price = Convert.ToInt32(ds.Tables["Cart"].Rows[RowIndex][6].ToString());
        int tot = qty * price;

        Label subtot = ((Label)row.Cells[6].FindControl("lblSubTotal"));

        strSqlCommand = "Update Cart set Qty=" + qty + ",TotalPrice=" + tot + " where No=" + ProductId + "";
        if (con.State != ConnectionState.Open)
            con.Open();
        cmd = new SqlCommand(strSqlCommand, con);
        if (cmd.ExecuteNonQuery() > 0)
            Response.Write("updated successfully");
        strSqlCommand = "SELECT sum(TotalPrice) from Cart";
        cmd = new SqlCommand(strSqlCommand, con);
        lblTot.Text = "Total Amt Rs." + cmd.ExecuteScalar().ToString();
        strSqlCommand = "Select * from Cart";
        da = new SqlDataAdapter(strSqlCommand, con);
        ds = new DataSet();
        da.Fill(ds, "Cart");
        GridView1.DataSource = ds;
        GridView1.DataBind();
        ModalPopupPanel1.Show();
        loadCartDetails();
        ((Label)row.Cells[6].FindControl("lblSubTotal")).Text = tot.ToString();
    }

    protected void btnCheckOut_Click(object sender, EventArgs e)
    {
        if(Page.User.Identity.IsAuthenticated)
        {
            Response.Redirect("~/payment.aspx");
        }
        Response.Redirect("Registration.aspx?Type=Login");
        lblHi.Text = "";
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        loadCartDetails();
        ModalPopupPanel1.Show();
    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("chkQty");
            strSqlCommand = "Select Id from Qty";
            da = new SqlDataAdapter(strSqlCommand, con);
            ds = new DataSet();
            da.Fill(ds, "Qty");
            ddl.DataSource = ds.Tables["Qty"];
            ddl.DataTextField = "Id";
            ddl.DataValueField = "Id";
            ddl.DataBind();
        }
    }
}
When  I click on AddToCart button then the quantity will be increased in database and if click on cart it will be updated

protected void lnkAddToCart_Click(object sender, EventArgs e)
{
strSqlCommand = "Select * from Cart where ProductId=" + ProductId;
con.Open();
cmd = new SqlCommand(strSqlCommand, con);
dr=cmd.ExecuteReader();
if (dr.HasRows)
{
Response.Write("Product already exists in Cart");
}
else
{
dr.Close();
strSqlCommand = "Insert into Cart(ProductId,ProductType,ProductBrand,ProductName,ProductImage,ProductPrice) Values(" + ProductId + ",'" + ProductType + "','" + ProductBrand + "','" + ProductName + "','" + Session["ProductImage1"] + "'," + ProductPrice + ")";
if (con.State != ConnectionState.Open)
con.Open();
cmd = new SqlCommand(strSqlCommand, con);
if (cmd.ExecuteNonQuery() > 0)
{
Response.Write("Product successfully added to cart");
UpdatePanel UpdatePanel1 = (UpdatePanel)Master.FindControl("UpdatePanel1");
UpdatePanel1.Update();
}
else
Response.Write("failed");
}
con.Close();
}

please anyone help me how to do this

Thanks & Regards

Abhishek
Posted

1 solution

Check this article it talks about different events in gridview.

GridView Event Handling[^]
 
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