Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

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

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
Mickeys
Asked:
Mickeys
  • 6
  • 5
1 Solution
 
GuitarRichCommented:
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
 
MickeysAuthor Commented:
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
 
GuitarRichCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
MickeysAuthor Commented:
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
 
GuitarRichCommented:
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
 
MickeysAuthor Commented:
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
 
GuitarRichCommented:
what IDE are you using?
0
 
MickeysAuthor Commented:
visual studio 2008
0
 
MickeysAuthor Commented:
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
 
GuitarRichCommented:
so what is the code your using to read the cart in the found.aspx page? can you post that?
0
 
MickeysAuthor Commented:
       protected void Page_Load(object sender, EventArgs e)
        {
            DataTable Cart = (DataTable)Session["shoppingSession"];
           
        }
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now