Solved

How do I manage to show my button when I have choosed what I want in my GridView

Posted on 2009-05-11
11
193 Views
Last Modified: 2012-05-06
I am using the code belowe. When I have added som stuff in my shoppingcart I want a button to show up where it says checkout.
How do I do that? I dont want it to show up until I have added something in my shoppingcart.

Also when I press this button I want the stuff in it to come with me.

code would be great.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="ProjektFilm" %>
 
<html>
<head>
<title>ButtonField ButtonType Example</title>
 
 
<script language="C#" runat="server">
DataTable Cart;
DataView CartView;
 
void Page_Load(Object src, EventArgs e)
{
    if (!IsPostBack)
    {
 
        myGrid.DataSource = (List<Filminfo>)Session["film"];
        myGrid.DataBind();
    }
 
    if (Session["shoppingSession"] == null)
    {
        Cart = new DataTable();
        Cart.Columns.Add(new DataColumn("Item", typeof(string)));
        Cart.Columns.Add(new DataColumn("Price", typeof(string)));
        Session["shoppingSession"] = Cart;
    }
    else Cart = (DataTable)Session["shoppingSession"];
 
    CartView = new DataView(Cart);
    shopCart.DataSource = CartView;
    shopCart.DataBind();
}
 
void updateCart(Object src, GridViewCommandEventArgs e)
{
    DataRow dr = Cart.NewRow();
 
    // get the row index stored in the CommandArgument property
    int index = Convert.ToInt32(e.CommandArgument);
 
    // get the GridViewRow where the command is raised
    GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
 
    // for bound fields, values are stored in the Text property of Cells [ fieldIndex ]
    string item = selectedRow.Cells[1].Text;
    string price = selectedRow.Cells[2].Text;
 
    if (e.CommandName == "AddToCart")
    {
        dr[0] = item; dr[1] = price;
        Cart.Rows.Add(dr);
    }
    else
    {  // remove from Cart
        CartView.RowFilter = "Item='" + item + "'";
        if (CartView.Count > 0) CartView.Delete(0);
        CartView.RowFilter = "";
    }
    shopCart.DataBind();
}
   
</script>
</head>
 
<body>
 
<div class="header"><h3>ButtonField <span class="hilite">ButtonType</span> Example</h3></div>
 
 
<form id="Form1" runat="server">
 
<table cellpadding="5">
<tr valign="top">
   <td><b>Product List</b>
 
      <asp:gridview id="myGrid" runat="server"
         cellpadding=5 font-size="8pt"
         gridlines="horizontal"
         autogeneratecolumns=false         
         onRowCommand="updateCart">
 
         <headerstyle backcolor="darkslategray"
            forecolor="khaki" font-bold />
 
         <rowstyle backcolor="ivory" verticalalign="top" />
 
         <columns>
            
            <asp:boundfield 
            datafield="Titel" 
            readonly="true" 
            headertext="Titel"/>
 
            <asp:boundfield 
            datafield="Genre" 
            readonly="true" 
            headertext="Genre"/>
            
            <asp:boundfield 
            datafield="Artikelnr" 
            readonly="true" 
            headertext="Artikelnr"/>
            
            <asp:boundfield 
            datafield="Regissor" 
            readonly="true" 
            headertext="Regissor"/>
            
                        <asp:boundfield 
            datafield="RentCost" 
            readonly="true" 
            headertext="RentCost"/>
            
            
            
            <asp:buttonfield
               text=" + "
               buttontype="button"
               commandname="AddToCart"  />
 
            <asp:buttonfield
               text=" - "
               buttontype="button"
               commandname="RemoveFromCart" />
 
         </columns>
 
      </asp:gridview>
   </td>
 
   <td><b>Shopping Cart</b>
 
      <asp:gridview id="shopCart" runat="server"
         cellpadding=5 font-size="8pt"
         headerstyle-backcolor="darkslategray"
         headerstyle-forecolor="khaki"
         headerstyle-font-bold
         rowstyle-backcolor="ivory"
         rowstyle-verticalalign="top" />
 
   </td></tr>
</table>
 
</form>
 
<hr size=1 width=90%>
 
</body>
</html>

Open in new window

0
Comment
Question by:Mickeys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 12

Accepted Solution

by:
GuitarRich earned 500 total points
ID: 24352694
have a button on the page and set its visibility to false - then in the Page_Load check the number of rows in the datatable to determine if the button should be visible or not.
You are already storing the shopping cart datatable in the session so that will be available on the next page you go to, just get it like you have in this page: Cart = (DataTable)Session["shoppingSession"];

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="ProjektFilm" %>
 
<html>
<head>
<title>ButtonField ButtonType Example</title>
 
 
<script language="C#" runat="server">
DataTable Cart;
DataView CartView;
 
void Page_Load(Object src, EventArgs e)
{
    if (!IsPostBack)
    {
 
        myGrid.DataSource = (List<Filminfo>)Session["film"];
        myGrid.DataBind();
    }
 
    if (Session["shoppingSession"] == null)
    {
        Cart = new DataTable();
        Cart.Columns.Add(new DataColumn("Item", typeof(string)));
        Cart.Columns.Add(new DataColumn("Price", typeof(string)));
        Session["shoppingSession"] = Cart;
    }
    else Cart = (DataTable)Session["shoppingSession"];
 
    // Check if checkout button is visible here.
    checkOutButton.Visible = (Cart.Rows.Count > 0);
 
    CartView = new DataView(Cart);
    shopCart.DataSource = CartView;
    shopCart.DataBind();
}
 
void updateCart(Object src, GridViewCommandEventArgs e)
{
    DataRow dr = Cart.NewRow();
 
    // get the row index stored in the CommandArgument property
    int index = Convert.ToInt32(e.CommandArgument);
 
    // get the GridViewRow where the command is raised
    GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
 
    // for bound fields, values are stored in the Text property of Cells [ fieldIndex ]
    string item = selectedRow.Cells[1].Text;
    string price = selectedRow.Cells[2].Text;
 
    if (e.CommandName == "AddToCart")
    {
        dr[0] = item; dr[1] = price;
        Cart.Rows.Add(dr);
    }
    else
    {  // remove from Cart
        CartView.RowFilter = "Item='" + item + "'";
        if (CartView.Count > 0) CartView.Delete(0);
        CartView.RowFilter = "";
    }
    shopCart.DataBind();
}
   
</script>
</head>
 
<body>
 
<div class="header"><h3>ButtonField <span class="hilite">ButtonType</span> Example</h3></div>
 
 
<form id="Form1" runat="server">
 
<table cellpadding="5">
<tr valign="top">
   <td><b>Product List</b>
 
      <asp:gridview id="myGrid" runat="server"
         cellpadding=5 font-size="8pt"
         gridlines="horizontal"
         autogeneratecolumns=false         
         onRowCommand="updateCart">
 
         <headerstyle backcolor="darkslategray"
            forecolor="khaki" font-bold />
 
         <rowstyle backcolor="ivory" verticalalign="top" />
 
         <columns>
            
            <asp:boundfield 
            datafield="Titel" 
            readonly="true" 
            headertext="Titel"/>
 
            <asp:boundfield 
            datafield="Genre" 
            readonly="true" 
            headertext="Genre"/>
            
            <asp:boundfield 
            datafield="Artikelnr" 
            readonly="true" 
            headertext="Artikelnr"/>
            
            <asp:boundfield 
            datafield="Regissor" 
            readonly="true" 
            headertext="Regissor"/>
            
                        <asp:boundfield 
            datafield="RentCost" 
            readonly="true" 
            headertext="RentCost"/>
            
            
            
            <asp:buttonfield
               text=" + "
               buttontype="button"
               commandname="AddToCart"  />
 
            <asp:buttonfield
               text=" - "
               buttontype="button"
               commandname="RemoveFromCart" />
 
         </columns>
 
      </asp:gridview>
   </td>
 
   <td><b>Shopping Cart</b>
 
      <asp:gridview id="shopCart" runat="server"
         cellpadding=5 font-size="8pt"
         headerstyle-backcolor="darkslategray"
         headerstyle-forecolor="khaki"
         headerstyle-font-bold
         rowstyle-backcolor="ivory"
         rowstyle-verticalalign="top" />
 
      <asp:Button id="checkOutButton" runat="Server" Text="Checkout" Visible="False"></asp:button>
 
   </td></tr>
</table>
 
</form>
 
<hr size=1 width=90%>
 
</body>
</html>

Open in new window

0
 

Author Comment

by:Mickeys
ID: 24353027
Almost. The button went ok but getting the stuff went bad

I cant bring it out. In debug mode it says there is nothing in it.

DataTable Cart = (DataTable)Session["shoppingSession"];
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="ProjektFilm" %>
 
<html>
<head>
<title>ButtonField ButtonType Example</title>
 
 
<script language="C#" runat="server">
DataTable Cart;
DataView CartView;
 
void Page_Load(Object src, EventArgs e)
{
 
    if (!IsPostBack)
    {       
        myGrid.DataSource = (List<Filminfo>)Session["film"];
        myGrid.DataBind();
        checkOutButton.Visible = false;
    }
 
    if (Session["shoppingSession"] == null)
    {
        Cart = new DataTable();
        Cart.Columns.Add(new DataColumn("Item", typeof(string)));
        Cart.Columns.Add(new DataColumn("Price", typeof(string)));
        Session["shoppingSession"] = Cart;
    }
    else Cart = (DataTable)Session["shoppingSession"];
 
    CartView = new DataView(Cart);
    shopCart.DataSource = CartView;
    shopCart.DataBind();
}
 
public void checkOutButton_Click(Object src, EventArgs e)
{
     Response.Redirect("found.aspx");
}
 
void updateCart(Object src, GridViewCommandEventArgs e)
{
    DataRow dr = Cart.NewRow();
 
    // get the row index stored in the CommandArgument property
    int index = Convert.ToInt32(e.CommandArgument);
 
    // get the GridViewRow where the command is raised
    GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
 
    // for bound fields, values are stored in the Text property of Cells [ fieldIndex ]
    string item = selectedRow.Cells[1].Text;
    string price = selectedRow.Cells[2].Text;
 
    if (e.CommandName == "AddToCart")
    {
        dr[0] = item; dr[1] = price;
        Cart.Rows.Add(dr);
        // Check if checkout button is visible here.
 
    }
    else
    {  // remove from Cart
        CartView.RowFilter = "Item='" + item + "'";
        if (CartView.Count > 0) CartView.Delete(0);
        CartView.RowFilter = "";
 
        
    }
    
    if (CartView.Count > 0)
    checkOutButton.Visible =true;
    
    else
    checkOutButton.Visible = false;
    shopCart.DataBind();
}
 
 
   
</script>
</head>
 
<body>
 
 
<form id="Form1" runat="server">
 
<div class="header"><h3>
    <img alt="logo" src="image/logo.JPG" style="width: 468px; height: 131px" /></h3>
    <h3>
    <asp:Label ID="Label" runat="server" Text="Filmen blev hittad. Vill du köpa den?"></asp:Label>
    </h3></div>
 
 
<table cellpadding="5">
<tr valign="top">
   <td><b>Product List</b>
 
      <asp:gridview id="myGrid" runat="server"
         cellpadding=5 font-size="8pt"
         gridlines="horizontal"
         autogeneratecolumns=false         
         onRowCommand="updateCart">
 
         <headerstyle backcolor="darkslategray"
            forecolor="khaki" font-bold />
 
         <rowstyle backcolor="ivory" verticalalign="top" />
 
         <columns>
            
            <asp:boundfield 
            datafield="Titel" 
            readonly="true" 
            headertext="Titel"/>
 
            <asp:boundfield 
            datafield="Genre" 
            readonly="true" 
            headertext="Genre"/>
            
            <asp:boundfield 
            datafield="Artikelnr" 
            readonly="true" 
            headertext="Artikelnr"/>
            
            <asp:boundfield 
            datafield="Regissor" 
            readonly="true" 
            headertext="Regissor"/>
            
                        <asp:boundfield 
            datafield="RentCost" 
            readonly="true" 
            headertext="RentCost"/>
            
            
            
            <asp:buttonfield
               text=" + "
               buttontype="button"
               commandname="AddToCart"  />
 
            <asp:buttonfield
               text=" - "
               buttontype="button"
               commandname="RemoveFromCart" />
 
         </columns>
 
      </asp:gridview>
   </td>
 
   <td><b>Shopping Cart</b>
 
      <asp:gridview id="shopCart" runat="server"
         cellpadding=5 font-size="8pt"
         headerstyle-backcolor="darkslategray"
         headerstyle-forecolor="khaki"
         headerstyle-font-bold
         rowstyle-backcolor="ivory"
         rowstyle-verticalalign="top" />
 
       <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <asp:Button ID="checkOutButton" runat="server" Text="Checkout" 
           onclick="checkOutButton_Click" />
 
   </td></tr>
<tr valign="top">
   <td>&nbsp;</td>
 
   <td>&nbsp;</td></tr>
</table>
 
<hr size=1 width=90%>
 
    <br />
    <br />
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Tillbaka</asp:HyperLink>
 
</form>
 
</body>
</html>

Open in new window

0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24353210
ok - in the updateCart method - you need to update the session object stick this line in at the end
Session["shoppingSession"] = Cart;
That should make sure the session has the most upto date Cart object in.
When you say it has nothing in it, is the object null or does the datatable not contain any rows?
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:Mickeys
ID: 24353284
well I am not sure. It aint null.......ExtendedProperties = 0
and I cant find anything to get something out of it.

It didnt help putting that line you said either.
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24353305
In the checkOut button click event put a break point on the Response.Redirect("found.aspx"); line and check whether the Cart.Rows has any rows in it. If it does then either the session object is not being retreived properly or its being re-set before it gets to the found.aspx page.
Can you post the code that gets the session object in the found.aspx page please?
0
 

Author Comment

by:Mickeys
ID: 24353391
well the problem is that I cant put any debug in aspx thats why I try to break out the code in my other thread. :-/
aspx
-------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ProjektFilm.WebForm1" %>
 
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="ProjektFilm" %>
 
<html>
<head>
<script language="C#" runat="server">
        DataTable Cart;
        DataView CartView;
 
        void Page_Load(Object src, EventArgs e)
        {
            if (!IsPostBack)
            {
                myGrid.DataSource = (List<Filminfo>)Session["film"];
                myGrid.DataBind();
                checkOutButton.Visible = false;
            }
 
            if (Session["shoppingSession"] == null)
            {
                Cart = new DataTable();
                Cart.Columns.Add(new DataColumn("Title", typeof(string)));
                Cart.Columns.Add(new DataColumn("Artikelnr", typeof(string)));
                Cart.Columns.Add(new DataColumn("Genre", typeof(string)));
                Cart.Columns.Add(new DataColumn("Regissor", typeof(string)));
                Cart.Columns.Add(new DataColumn("Rentcost", typeof(string)));
                Session["shoppingSession"] = Cart;
            }
            else Cart = (DataTable)Session["shoppingSession"];
 
            CartView = new DataView(Cart);
            shopCart.DataSource = CartView;
            shopCart.DataBind();
        
 
 
        }
 
 
 
        void updateCart(Object src, GridViewCommandEventArgs e)
        {
            DataRow dr = Cart.NewRow();
 
            // get the row index stored in the CommandArgument property
            int index = Convert.ToInt32(e.CommandArgument);
 
            // get the GridViewRow where the command is raised
            GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
 
            // for bound fields, values are stored in the Text property of Cells [ fieldIndex ]
            string genre = selectedRow.Cells[1].Text;
            string artikelid = selectedRow.Cells[2].Text;
            string title = selectedRow.Cells[0].Text;
            string rentcost = selectedRow.Cells[4].Text;
            string regissor = selectedRow.Cells[3].Text;
 
            if (e.CommandName == "AddToCart")
            {
                dr[0] = title; dr[1] = artikelid;
                dr[2] = genre; dr[3] = regissor;
                dr[4] = rentcost;
                Cart.Rows.Add(dr);
 
            }
            else
            {  // remove from Cart
                CartView.RowFilter = "Title='" + title + "'";
                if (CartView.Count > 0) CartView.Delete(0);
                CartView.RowFilter = "";
 
 
            }
 
            if (CartView.Count > 0)
                checkOutButton.Visible = true;
 
            else
                checkOutButton.Visible = false;
            shopCart.DataBind();
 
            Session["shoppingSession"] = Cart;
        }
 
   
</script>
</head>
 
<body>
 
 
<form id="Form1" runat="server">
 
<div class="header"><h3>
    <img alt="logo" src="image/logo.JPG" style="width: 468px; height: 131px" /></h3>
    <h3>
    <asp:Label ID="Label" runat="server" Text="Filmen blev hittad. Vill du köpa den?"></asp:Label>
    </h3></div>
 
 
<table cellpadding="5">
<tr valign="top">
   <td><b>Product List</b>
 
      <asp:gridview id="myGrid" runat="server"
         cellpadding=5 font-size="8pt"
         gridlines="horizontal"
         autogeneratecolumns=false         
         onRowCommand="updateCart">
 
         <headerstyle backcolor="darkslategray"
            forecolor="khaki" font-bold />
 
         <rowstyle backcolor="ivory" verticalalign="top" />
 
         <columns>
            
            <asp:boundfield 
            datafield="Titel" 
            readonly="true" 
            headertext="Titel"/>
 
            <asp:boundfield 
            datafield="Genre" 
            readonly="true" 
            headertext="Genre"/>
            
            <asp:boundfield 
            datafield="Artikelnr" 
            readonly="true" 
            headertext="Artikelnr"/>
            
            <asp:boundfield 
            datafield="Regissor" 
            readonly="true" 
            headertext="Regissor"/>
            
                        <asp:boundfield 
            datafield="RentCost" 
            readonly="true" 
            headertext="RentCost"/>
            
            
            
            <asp:buttonfield
               text=" + "
               buttontype="button"
               commandname="AddToCart"  />
 
            <asp:buttonfield
               text=" - "
               buttontype="button"
               commandname="RemoveFromCart" />
 
         </columns>
 
      </asp:gridview>
   </td>
 
   <td><b>Shopping Cart</b>
 
      <asp:gridview id="shopCart" runat="server"
         cellpadding=5 font-size="8pt"
         headerstyle-backcolor="darkslategray"
         headerstyle-forecolor="khaki"
         headerstyle-font-bold
         rowstyle-backcolor="ivory"
         rowstyle-verticalalign="top" />
 
       <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <asp:Button ID="checkOutButton" runat="server" Text="Checkout" 
           onclick="checkOutButton_Click" />
 
   </td></tr>
<tr valign="top">
   <td>&nbsp;</td>
 
   <td>&nbsp;</td></tr>
</table>
 
<hr size=1 width=90%>
 
    <br />
    <br />
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Tillbaka</asp:HyperLink>
 
</form>
 
</body>
</html>
 
 
 
----------------------
aspx.cs
-----------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
 
 
 
namespace ProjektFilm
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        
        public void checkOutButton_Click(Object src, EventArgs e)
        {
            Response.Redirect("found.aspx");
 
        }
 
 
 
        
    }
}

Open in new window

0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24353659
what IDE are you using?
0
 

Author Comment

by:Mickeys
ID: 24353768
visual studio 2008
0
 

Author Comment

by:Mickeys
ID: 24353819
Here we go.  There wasnt put an title on the page so I couldnt debug. ???? strange but anyway no it works........I got 1 in my Cart.Rows....so that should be ok
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24353860
so what is the code your using to read the cart in the found.aspx page? can you post that?
0
 

Author Comment

by:Mickeys
ID: 24355784
       protected void Page_Load(object sender, EventArgs e)
        {
            DataTable Cart = (DataTable)Session["shoppingSession"];
           
        }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question