locked
pass query string using ajax RRS feed

  • Question

  • User-1998474842 posted

    HI guys

    below code it is sample code i take from net , but i have question when i try to the link of the add product to the cart work as ajax but it doesn't work , can i change this link to the button ?

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="basket.aspx.cs" Inherits="public_basket" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="id" DataSourceID="SqlDataSource2" 
                EmptyDataText="There are no data records to display." AllowPaging="True" 
                PageSize="4">
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" 
                        SortExpression="id" />
                    <asp:TemplateField HeaderText="pic">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("pic") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Image ID="Image1" runat="server" Height="62px" 
                                ImageUrl='<%# Eval("pic") %>' Width="80px" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                    <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
                    <asp:HyperLinkField DataNavigateUrlFields="id" 
                        DataNavigateUrlFormatString="?ID={0}" HeaderText="buy" Text="buy" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                DeleteCommand="DELETE FROM [Items] WHERE [id] = @id" 
                InsertCommand="INSERT INTO [Items] ([id], [name], [pic], [price]) VALUES (@id, @name, @pic, @price)" 
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
                SelectCommand="SELECT [id], [name], [pic], [price] FROM [Items]" 
                UpdateCommand="UPDATE [Items] SET [name] = @name, [pic] = @pic, [price] = @price WHERE [id] = @id">
                <DeleteParameters>
                    <asp:Parameter Name="id" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="id" Type="Int32" />
                    <asp:Parameter Name="name" Type="String" />
                    <asp:Parameter Name="pic" Type="String" />
                    <asp:Parameter Name="price" Type="String" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="name" Type="String" />
                    <asp:Parameter Name="pic" Type="String" />
                    <asp:Parameter Name="price" Type="String" />
                    <asp:Parameter Name="id" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>
        
        </div>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="id" EmptyDataText="There are no data records to display.">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" 
                    SortExpression="id" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                <asp:TemplateField HeaderText="pic">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("pic") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" Height="63px" 
                            ImageUrl='<%# Eval("pic") %>' Width="75px" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
                
                <asp:TemplateField HeaderText="count">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="TextBoxCount" runat="server" Height="19px" 
                            Text='<%# Bind("count") %>' Width="35px"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="total" HeaderText="totalprice" />
                <asp:HyperLinkField DataNavigateUrlFields="id" 
                    DataNavigateUrlFormatString="?DelID={0}" HeaderText="delete" 
                    Text="delete" />
                
            </Columns>
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="change" />
        <asp:Button ID="save" runat="server" Text="Save To DataBase" 
            onclick="save_Click" />
        </form>
    </body>
    </html>
    
    using System;
    using System.Data;
    using System.Web.UI.WebControls;
    
    public partial class public_basket : System.Web.UI.Page
    {
        DataTable Basket_DataTable = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["basket"] != null)
                ///read Basket_DataTable from session if exist
                Basket_DataTable = (DataTable)Session["basket"];
            else
            {
                //create an empty DataTable and Add some columns to it
                Basket_DataTable = new DataTable();
                Basket_DataTable.Columns.Add("id");
                Basket_DataTable.Columns.Add("name");
                Basket_DataTable.Columns.Add("price");
                Basket_DataTable.Columns.Add("pic");
                Basket_DataTable.Columns.Add("count");
                Basket_DataTable.Columns.Add("total");
            }
            /////////////////////////
    
            if (Request["DelID"] != null)
            {
                for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
                    if (Basket_DataTable.Rows[i][0].ToString() == Request["DelID"].ToString())
                        Basket_DataTable.Rows.Remove(Basket_DataTable.Rows[i]);
    
            }
            if (Request["ID"] != null)
            {
                //search item in DataTable
                bool Found = false;
                for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
                    if (Basket_DataTable.Rows[i][0].ToString() == Request["ID"].ToString())
                        Found = true;
    
                //add to basket
                if (Found == false)
                {
                    DataAcess data = new DataAcess();
                    string sql = "SELECT * FROM Items where id=" + Request["ID"];
                    DataTable ret= data.exe_select(sql);
                    if (ret != null && ret.Rows.Count == 1)
                    {
                        Basket_DataTable.Rows.Add(new object[] { Request["ID"],ret.Rows[0]["name"].ToString()
                                                  ,ret.Rows[0]["price"].ToString(),ret.Rows[0]["pic"].ToString()
                                                 ,"1",ret.Rows[0]["price"].ToString()});
                    }
                }
            }
            /////////////////////////
            if (IsPostBack == false)
            {
                GridView2.DataSource = Basket_DataTable;
                GridView2.DataBind();
            }
    
            Session["basket"] = Basket_DataTable;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < GridView2.Rows.Count; i++)
                {
                    TextBox Tb = (TextBox)GridView2.Rows[i].FindControl("TextBoxCount");
                    Basket_DataTable.Rows[i]["count"] = Tb.Text;
                    Basket_DataTable.Rows[i]["total"] = Convert.ToInt32(Tb.Text) * Convert.ToInt32(Basket_DataTable.Rows[i]["price"]);
                }
            }
            catch
            {
            }
            Response.Redirect("basket.aspx");
        }
        protected void save_Click(object sender, EventArgs e)
        {
            DataAcess data = new DataAcess();
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                String ID = GridView2.Rows[i].Cells[0].Text.ToString(); ;
                TextBox Tb = (TextBox)GridView2.Rows[6].FindControl("TextBoxCount");
                string sql = "INSERT INTO Basket (Basket_Amount, count, id)VALUES ({0}, {1})";
                sql = string.Format(sql, Tb.Text, ID);
                data.exe_cmd(sql);
            }
        }
    }
    

    Wednesday, October 31, 2018 7:44 AM

All replies

  • User839733648 posted

    Hi peshangm,

    i have question when i try to the link of the add product to the cart work as ajax but it doesn't work , can i change this link to the button ?

    According to your description and code, I suggest that you could define a session to store the items you've selected.

    You should takes care of creating the shopping cart in session and adding selected items to it:

    And I think that you could create another page as shopping cart and this may be easier to achieve.

    For more about how to create shopping cart, please refer to the below article which tells you how to achieve step by step.

    Best Regards,

    Jenifer

    Thursday, November 1, 2018 10:00 AM