如何在ASP.NET中将datalist项添加到临时购物车表 [英] How to add datalist item to temporary cart table in ASP.NET

查看:108
本文介绍了如何在ASP.NET中将datalist项添加到临时购物车表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨每一个,



i想要将我的datalist项添加到临时购物车表(数据表),当我点击datalist项目的图像按钮时。 .plz告诉我,我已经尝试了一些,但它显示了一个项目细节重复,如果我点击另一个项目以前的项目从数据表丢失请帮助我....



我尝试了什么:



hi every one,

i would like to add my datalist item to temporary cart table (data table) , when i am click on image buttton of datalist item ..plz tel me, already i am tried some , but it was showing one item detail repeated , and also if i am click on another item previous item was lost from data table please help me....

What I have tried:

protected void dl_beers_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "item_plus")
{
// Label lblprice = (Label)(e.Item.FindControl("lbl_beer_price"));
Label lblcart = (Label)(e.Item.FindControl("lbl_items_order"));
Label lblitem = (Label)(e.Item.FindControl("lbl_beer_name"));
Label lblpid = (Label)(e.Item.FindControl("lbl_pid"));
int order = Convert.ToInt16(lblcart.Text);
order = order + 1;
int index = e.Item.ItemIndex;
Label lblprice = (Label)dl_beers.Items[index].FindControl("lbl_beer_price");
//for price
float price = float.Parse(lblprice.Text);
price = price * order;
Convert.ToSingle(price);
//add to cart
DataTable dt = new DataTable();
dt.Columns.Add("pid",typeof(string));
dt.Columns.Add("pname",typeof(string));
dt.Columns.Add("pprice", typeof(string));
for (int i = dt.Rows.Count; i <= Convert.ToInt32(lblcart.Text); i++) //for (int i = 0; i <= Convert.ToInt32(lblcart.Text); i++)
{
dt.Rows.Add(lblpid.Text, lblitem.Text, lblprice.Text);
addtocart();
ViewState["dt"] = dt;
this.BindGrid();
}
lblcart.Text = Convert.ToString(order);
}
if (e.CommandName == "item_minus")
{
// int ordercount = Convert.ToInt16(spn_beersorder.InnerText);
Label lblcart = (Label)(e.Item.FindControl("lbl_items_order"));
int order = Convert.ToInt16(lblcart.Text);
if (order > 0)
{
order = order - 1;
// beers_order = order;
lblcart.Text = Convert.ToString(order);
// ordercount = ordercount - 1;
// spn_beersorder.InnerText = Convert.ToString(ordercount);
}
else
{
order = 0;
lblcart.Text = Convert.ToString(order);
// spn_beersorder.InnerText = Convert.ToString(order+"x");
}
}
}







我的aspx代码

----------------------






my aspx code
----------------------

<asp:DataList ID="dl_beers" OnItemCommand="dl_beers_ItemCommand" Width="100%" CellSpacing="10" DataKeyField="pid" ItemStyle-HorizontalAlign="Center" RepeatDirection="Vertical" RepeatColumns="1" RepeatLayout="Table" runat="server">
<HeaderTemplate>
<table><thead>
<table><tbody><tr style=" border-bottom-style: solid;border-width:thin;border-color: #EAEAEA; text-align:center;">
<th>
Item
</th>
<th>
Name
</th>
<th>
Volume
</th>
<th>
Price
</th>
<th style=" text-align:left;">
Order
</th>
</tr></tbody></table>
</thead></table>
</HeaderTemplate>
<itemtemplate>
<table><tbody>
<tr class="c_dl_products_alter">
<td>
<asp:Image ID="img_item_beer" CssClass="img-responsive img img-thumbnail" Height="70px" Width="50px" ImageUrl='<%#"../admin/"+Eval("pimgpath") %>' runat="server" />
</td>
<td>
<asp:Label ID="lbl_beer_name" Font-Bold="true" runat="server" Text='<%#Eval("pname") %>'>
<p  runat="server" id="p_beer_desc">
<%#Eval("pdisc") %>
 <asp:Label ID="lbl_o" runat="server" Text="Label">
</td>
<td>
<asp:Label ID="lbl_beer_volume" Font-Bold="true" runat="server" Text='<%#Eval("pvolume") %>'>
</td>
<td>
<asp:Label ID="lbl_beer_price" Font-Bold="true" runat="server" Text='<%#Eval("pprice") %>'>
</td>
<td style="text-align:center; width:110px;">
<div class="c_div_btn_cart">
<asp:ImageButton ID="imgbtn_minus" ImageUrl="img/minus.png" CssClass="img-circle img-responsive img c_imgbtns" CommandName="item_minus" CommandArgument='<%#Eval("pid") %>' runat="server" />
</div>
<div class="c_div_itemquntity">
<asp:Label ID="lbl_items_order" Font-Bold="true" CssClass="lbl_p_quantiy" runat="server" Text="0">
</div>
<div class="c_div_btn_cart">
<asp:ImageButton ID="imgbtn_plus" ImageUrl="img/plus.png" CssClass="img-circle img-responsive img c_imgbtns" CommandArgument='<%#Eval("pid") %>' CommandName="item_plus" runat="server" />
</div>
<asp:Label ID="lbl_pid" runat="server" Visible="false" Text='<%#Eval("pid") %>'>
</td>
</tr>
</tbody></table>
</itemtemplate>





//购物车视图



// cart view

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" >
<alternatingrowstyle backcolor="White" forecolor="#284775" />
<columns>
<asp:BoundField DataField="pid" HeaderText="ID" ItemStyle-Width="150" >
<itemstyle width="150px" />

<asp:BoundField DataField="pname" HeaderText="Name" ItemStyle-Width="150" >
<itemstyle width="150px" />

<asp:BoundField DataField="pprice" HeaderText="price" ItemStyle-Width="150" >
<itemstyle width="150px" />

<asp:TemplateField>
<itemtemplate>
<asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandName="Edit" />
</itemtemplate>
<edititemtemplate>
<asp:LinkButton ID="LinkButton2" Text="Update" runat="server" />
<asp:LinkButton ID="LinkButton3" Text="Cancel" runat="server" />
</edititemtemplate>

</columns>
<editrowstyle backcolor="#999999" />
<footerstyle backcolor="#5D7B9D" font-bold="True" forecolor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<pagerstyle backcolor="#284775" forecolor="White" horizontalalign="Center" />
<rowstyle backcolor="#F7F6F3" forecolor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<sortedascendingcellstyle backcolor="#E9E7E2" />
<sortedascendingheaderstyle backcolor="#506C8C" />
<sorteddescendingcellstyle backcolor="#FFFDF8" />
<sorteddescendingheaderstyle backcolor="#6F8DAE" />

推荐答案

Q uote:

i would like to add my datalist item to temporary cart table (data table) , when i am click on image buttton of datalist item

i would like to add my datalist item to temporary cart table (data table) , when i am click on image buttton of datalist item





First, replace or your Image with ImageButton control. You would need to handle the adding of items at Click event of your ImageButton instead. For example:



First, replace or your Image with ImageButton control. You would need to handle the adding of items at Click event of your ImageButton instead. For example:

<asp:imagebutton id="btnItemAdd" onclick="btnItemAdd_Click" cssclass="img-responsive img img-thumbnail" height="70px" width="50px" imageurl="<%#"../admin/"+Eval("pimgpath") %>" runat="server" />





Then you can do something like this at Click event:



Then you can do something like this at Click event:

protected void btnItemAdd_Click(object sender, ImageClickEventArgs e){
        ImageButton btn = (ImageButton)sender;
 	DataListItem item = (DataListItem)btn.NamingContainer;
        if (item != null)
        {
            //your add logic here
 
            //gets the ItemIndex
            int index = item.ItemIndex;

            //access the controls here
            
            Label lblcart = (Label)(item.FindControl("lbl_items_order"));


            if (ViewState["CurrentTable"] != null) {

                //add items to existing DataTable here

                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;

                //add new items here

                if (dtCurrentTable.Rows.Count > 0) {
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
                    dr["Column1"] = lblCart.Text;

                    //add new row to DataTable 
                    dtCurrentTable.Rows.Add(drCurrentRow);

                    //Store the current data to ViewState for future reference 
                    ViewState["CurrentTable"] = dtCurrentTable;
                }
 
            }
	    else{

            	//create the datatable for the first time and add the items

            	DataTable dt = new DataTable();
            	DataRow dr = null;

            	dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
            	dt.Columns.Add(new DataColumn("Column1", typeof(string))); 
            	//add more columns here based on your requirements


            	//add the data to the DataTable
            	dr = dt.NewRow();
            	dr["RowNumber"] = 1;
            	dr["Column1"] = lblCart.Text;
            	dt.Rows.Add(dr);

            	//Store the DataTable in ViewState for future reference 
            	ViewState["CurrentTable"] = dt;

            }

        }

}







Quote:

but it was showing one item detail repeated , and also if i am click on another item previous item was lost from data table

but it was showing one item detail repeated , and also if i am click on another item previous item was lost from data table





It gets repeated because you are looping through all the items in DataList, thus overriding each of your data. The code I’ve provided should fix that, and it should add one item at a time to your DataTable.



It gets repeated because you are looping through all the items in DataList, thus overriding each of your data. The code I've provided should fix that, and it should add one item at a time to your DataTable.


这篇关于如何在ASP.NET中将datalist项添加到临时购物车表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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