c# - Selecting an item in a GridView and adding to the List -
i have written piece of code below productid
gridview when user clicks on select link.
convert.toint32(gridview1.selecteddatakey.values["productid"])))
however, if user clicks on more 1 click newer value replaces previous. there way keep adding cart list when user clicks on new item?
hope makes sense
thanks
edit:
here's code shopping page:
public partial class _default : system.web.ui.page { list<basketclass> cart = new list<basketclass>(); protected void gridview1_selectedindexchanged1(object sender, eventargs e) { cart.add(new basketclass(convert.toint32(gridview1.selecteddatakey.values["bookid"]))); session.add("cartsess", cart); response.redirect("basket.aspx"); } }
i dont know if location of creating list important? wasn't sure if placed in click event if keep creating new instance?
then basket page have:
protected void page_init(object sender, eventargs e) { list<basketclass> cart = (list<basketclass>)session["cartsess"]; foreach (basketclass bookid in cart) { gridview1.datasource = cart; gridview1.databind(); accessdatasource1.selectcommand = "select [bookid], [book_title] [tblbook] "; } }
you should not create basketcart list object in top of page, if there no significant difference, think cna this:
protected void page_init(object sender, eventargs e) { if(session["cartsess"]!=null) { foreach (basketclass bookid in (list<basketclass>)session["cartsess"]) { gridview1.datasource = cart; gridview1.databind(); accessdatasource1.selectcommand = "select [bookid], [book_title] [tblbook] "; } } }
and gridview should be:
protected void gridview1_selectedindexchanged1(object sender, eventargs e) { list<basketclass> cart; if(session["cartsess"]!=null) { cart = (list<basketclass>)session["cartsess"] } else cart = new list<basketclass>(); cart.add(new basketclass(convert.toint32(gridview1.selecteddatakey.values["bookid"]))); session.add("cartsess", cart); response.redirect("basket.aspx"); }
Comments
Post a Comment