i working on e-commerce app , problem in showing quantity of items purchased. dropdownlist shows option change quantity, in gridview , has selectedindexchanged event. event code works fine when there two(or more) products , change quantity of 1 row , change other rows's quantity, previous row quantity set default "1" while total price column shows multiplication of price , quanity set dropdownlist.
my dropdownlist code:
<asp:templatefield headertext="qty"> <edititemtemplate> <asp:textbox id="textbox1" runat="server"></asp:textbox> </edititemtemplate> <itemtemplate> <asp:dropdownlist id="ddlqty" runat="server" autopostback="true" onselectedindexchanged="ddlqty_selectedindexchanged"> <asp:listitem>1</asp:listitem> <asp:listitem>2</asp:listitem> <asp:listitem>3</asp:listitem> <asp:listitem>4</asp:listitem> <asp:listitem>5</asp:listitem> </asp:dropdownlist> </itemtemplate> </asp:templatefield>
my c# code selectedindexchanged , gridview rowdataboundevent:
protected void ddlqty_selectedindexchanged(object sender, eventargs e) { dropdownlist ddlqty =(dropdownlist)sender; int rowindex = int.parse(ddlqty.attributes["rowindex"].tostring()); viewstate["index"] = rowindex; viewstate["i"] = ddlqty.selectedvalue; decimal price = convert.todecimal(ds.tables[0].rows[rowindex]["price"].tostring()); decimal totalprice = price * convert.toint32(ddlqty.selectedvalue); ds.tables[0].rows[rowindex]["totalprice"] = totalprice; gridview1.datasource = ds; gridview1.databind(); ds.tables[1].rows[0]["totalprice"] = decimal.parse(ds.tables[1].rows[0]["totalprice"].tostring()) + totalprice - convert.todecimal(ds.tables[0].rows[rowindex]["price"].tostring()); gridview1.footerrow.cells[2].text = "total amount ="; gridview1.footerrow.cells[3].text = ds.tables[1].rows[0]["totalprice"].tostring(); } protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { dropdownlist ddl = (dropdownlist)e.row.findcontrol("ddlqty"); ddl.attributes.add("rowindex", e.row.rowindex.tostring()); if (viewstate["index"] != null) { if (e.row.rowindex.tostring() == viewstate["index"].tostring()) { ddl.selectedvalue = viewstate["i"].tostring(); } } } }
i attaching image make more clear.
i think grid binding on each postback made dropdownlist, changing 1 value overwrite formerly stored value in vs. should try storing these values list, appending changes instead of replacing each time.
imho, anyway, go setting autopostback properties false, , updating row calculations client-side, validating on server side on submit.
Comments
Post a Comment