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
179 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
  • 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
 

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
3 Use Cases for Connected Systems

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

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now