加入购物车:从一个页面(Datalist)获取详细信息到另一个页面(gridview) [英] Add to cart :Getting Details from from one page (Datalist) to another page(gridview)

查看:98
本文介绍了加入购物车:从一个页面(Datalist)获取详细信息到另一个页面(gridview)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我解决这个问题。我一直在购物车项目中工作并在datalist中显示产品详情

产品名称

价格数量

产品描述

和下一页是添加购物车页面如果我点击添加到购物车按钮,productdetails中的值应该在网格视图中绑定在下一页我尝试了一些编码,但它不起作用它没有绑定任何值。请帮助我来解决这个问题。

编码如下

查看productDetails.aspx.cs页面

Please Please Help me with this . I have been working in the shopping cart project and displayed product details in datalist like
Product Name
Price Quantity
product Description
and the next Page is Add cart page if i click the add to cart button the values in productdetails should be binded in the gridview in the next page i tried some coding but it doesnt work it doesnt binded any values .please help me to solve this problem.
coding as follows
View productDetails.aspx.cs page

  protected void use(object sender, DataListItemEventArgs e)
    {
       Label prd = (Label)e.Item.FindControl("productNameLabel");
     ViewState["productName"] = prd.Text;
      Label des = (Label)e.Item.FindControl("Label1");
      ViewState["description"] = des.Text;
      Image img = (Image)e.Item.FindControl("Image1");
      ViewState["image"] = img.ImageUrl.ToString();
      Label prc = (Label)e.Item.FindControl("priceLabel");
      ViewState["price"] = prc.Text;
}




protected void Button1_Click(object sender, EventArgs e)
    {
        int i = int.Parse(TextBox1.Text);


        int p = int.Parse((string)ViewState["price"]);
        string img = ViewState["image"].ToString();
        string s2 = System.Web.HttpContext.Current.User.Identity.Name;
        string s1 = ViewState["productName"].ToString();
        con.Open();
        string cartCmd = "insert into completeCart(pName,quantity,uName,img,price) values('" + s1 + "','" + i + "','" + s2 + "','" + img + "','" + p + "') ";
        SqlCommand cmd = new SqlCommand(cartCmd, con);
        cmd.ExecuteNonQuery();
        con.Close();

        Response.Redirect("Add_cart.aspx?user=" + s2);

    }



加入购物车aspx.cs


Add to cart aspx.cs

public partial class Add_cart : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Integrated Security=true;Initial Catalog=rsaProducts");
    string url;
    decimal grandTotal = 0;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGridData();
        string s2 = System.Web.HttpContext.Current.User.Identity.Name;
        Response.Cookies["uname"].Value = s2;
    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        BindGridData();
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "price")) * Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "quantity"));
                grandTotal = grandTotal + rowTotal;


            }
            Response.Cookies["price"].Value = grandTotal.ToString();
            TextBox2.Text = grandTotal.ToString();
            HyperLink1.ImageUrl = "~/img/str/buttons/checkout.jpg";
    }
    private void BindGridData()
    {
        try
        {
            con.Open();
            string productid = Request.QueryString["id"].ToString();
            {
                string sql = "select * from completeCart where id=" + productid;
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }

        catch (Exception ex)
        {
        }
        finally
        {
            con.Close();
        }
    }
}



加入cart.aspx页面



< asp:GridView ID =GridView1CssClass =GridView1runat =serverAutoGenerateColumns =False

DataKeyNames =id

EmptyDataText = 购物车中没有商品

onrowdatabound =GridView1_RowDataBound

onrowdeleted =GridView1_RowDeleted

BackColor =#DEBA84BorderColor = #DEBA84BorderStyle =无BorderWidth =1px

CellPadding =3CellSpacing =2>

< rowstyle forecolor =#8C4510 backcolor =#FFF7E7>

< columns> < asp:CommandField ShowDeleteButton =True/>

< asp:BoundField DataField =pNameHeaderText =PRODUCTSortExpression =pName/>

< asp:TemplateField HeaderText =imgSortExpression =img>

< edititemtemplate>

< asp:TextBox ID =TextBox1runat =serverText ='<%#Bind(img)%>'>



< itemtemplate>

< asp:图片ID =Image1runat =serverWidth =250pxHeight =300pxImageUrl ='<%#Bind(img)%>'/>





< asp:BoundField DataField =quantityHeaderText =QUANTITY

SortExpression =quantity/> ;

< asp:BoundField DataField =priceHeaderText =COSTSortExpression =price/>

< footersty le backcolor =#F7DFB5forecolor =#8C4510>

< pagerstyle forecolor =#8C4510horizo​​ntalalign =Center>

< SelectedRowStyle BackColor =#738A9CFont-Bold =TrueForeColor =White/>

< HeaderStyle BackColor =#A55129Font-Bold =TrueForeColor =White />

< sortedascendingcellstyle backcolor =#FFF1D4>

< sortedascendingheaderstyle backcolor =#B95C30>

< ; sorteddescendingcellstyle backcolor =#F1E5CE>

< sorteddescendingheaderstyle backcolor =#93451F>







<! - PayPal徽标 - >





< img src =https://www.paypal.com/en_US/i/bnr/horizo​​ntal_solution_PPeCheck.gifborder =0

alt =解决方案图形>




<! - PayPal徽标 - >






add to cart.aspx page

<asp:GridView ID="GridView1" CssClass="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id"
EmptyDataText="No Item in the Cart"
onrowdatabound="GridView1_RowDataBound"
onrowdeleted="GridView1_RowDeleted"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2">
<rowstyle forecolor="#8C4510" backcolor="#FFF7E7">
<columns> <asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="pName" HeaderText="PRODUCT" SortExpression="pName" />
<asp:TemplateField HeaderText="img" SortExpression="img">
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("img") %>'>

<itemtemplate>
<asp:Image ID="Image1" runat="server" Width="250px" Height="300px" ImageUrl='<%# Bind("img") %>' />


<asp:BoundField DataField="quantity" HeaderText="QUANTITY"
SortExpression="quantity" />
<asp:BoundField DataField="price" HeaderText="COST" SortExpression="price" />
<footerstyle backcolor="#F7DFB5" forecolor="#8C4510">
<pagerstyle forecolor="#8C4510" horizontalalign="Center">
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<sortedascendingcellstyle backcolor="#FFF1D4">
<sortedascendingheaderstyle backcolor="#B95C30">
<sorteddescendingcellstyle backcolor="#F1E5CE">
<sorteddescendingheaderstyle backcolor="#93451F">



<!-- PayPal Logo -->


<img src="https://www.paypal.com/en_US/i/bnr/horizontal_solution_PPeCheck.gif" border="0"
alt="Solution Graphics">


<!-- PayPal Logo -->


总费用< asp:TextBox ID =TextBox2runat =server

BorderColor =# CC3300Enabled =False

Font-Bold =TrueForeColor =Red>

    


     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;     <% - < asp:ImageButton ID =ImageButton3runat =serverAlternateText =check out

onclick =ImageButton3_Click1ToolTip =点击查看ImageUrl =〜/ img / str / buttons / checkout.jpg/> - %>






< asp:HyperLink ID =HyperLink1runat =server> HyperLink

Total Cost<asp:TextBox ID="TextBox2" runat="server"
BorderColor="#CC3300" Enabled="False"
Font-Bold="True" ForeColor="Red">
    

                  <%--<asp:ImageButton ID="ImageButton3" runat="server" AlternateText="check out"
onclick="ImageButton3_Click1" ToolTip="click for check out" ImageUrl="~/img/str/buttons/checkout.jpg" />--%>



<asp:HyperLink ID="HyperLink1" runat="server" >HyperLink





请帮我解决这个问题



please help me to solve this problem

推荐答案

请参考下面的文章,它展示了构建购物车应用程序的所有事情。



http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/shopping-cart [ ^ ]



在构建应用程序时,请使用一些编码标准和一些设计模式。请参考上面的文章,并将其作为构建应用程序的参考。



如果可能的话,总是使用存储过程而不是内联查询。
please refer below article, it's demonstrating every thing for building a Shopping cart application.

http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/shopping-cart[^]

And please use some coding standard's and some design patters too, while building your application. just refer the above article, and take it as reference for building your application.

And also if possible, always use store procedures instead of inline queries.


这篇关于加入购物车:从一个页面(Datalist)获取详细信息到另一个页面(gridview)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆