Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have a Datalist in my project and it have a two column("Price" and "Quantity")

Price is coming from database. I am using DropdownList for Quantity.

I wanna multiply these 2 column and get result dynamicly. The important point in here, if i change the quantity, result have to change without page refresh.

How can I do that? What is the event of datalist for do this?

Thanks.


XML
<asp:DataList ID="DataList1" DataKeyField="SIRANO" runat="server"
                        onitemcommand="DataList1_ItemCommand"  >

Price:<asp:Label ID="Label5" runat="server" Text='<%#Eval("PRICE") %>'></asp:Label>
Quantity:<asp:DropDownList ID="DropDownList1" runat="server" Height="20px" Width="48px">
   <asp:ListItem>3</asp:ListItem>
  </asp:DropDownList>
<b> Total:<asp:Label ID="Label7" runat="server"></asp:Label></b>
Posted

C#
<asp:datalist id="DataList1" datakeyfield="id" runat="server" xmlns:asp="#unknown">
                <itemtemplate>
                    Price:<asp:label id="Label5" runat="server" text="<%#Eval("PRICE") %>"></asp:label>
                    Quantity:<asp:dropdownlist id="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged" autopostback="true">
                        <asp:listitem text="0" value="0" selected="True"></asp:listitem>
                        <asp:listitem text="1" value="1"></asp:listitem>
                        <asp:listitem text="2" value="2"></asp:listitem>
                        <asp:listitem text="3" value="3"></asp:listitem>
                        <asp:listitem text="4" value="4"></asp:listitem>
                        <asp:listitem text="5" value="5"></asp:listitem>
                    </asp:dropdownlist>
                    Total:<asp:label id="Label7" runat="server"></asp:label>
                </itemtemplate>
            </asp:datalist>



protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ddlList = (DropDownList)sender;
        var row = (DataListItem)ddlList.NamingContainer;
        int index = row.ItemIndex;
        var p = Convert.ToInt32(((Label)row.FindControl("Label5")).Text);
        var q = Convert.ToInt32(ddlList.SelectedItem.Value);
        var res = Convert.ToInt32(p * q);
        Label lblRes = ((Label)row.FindControl("Label7"));
        lblRes.Text = res.ToString();
    }
 
Share this answer
 
I am getting "Format Excaption was unhandled by user code" on "var p=convert.toInt32" line. So it is not working
 
Share this answer
 
Comments
PJ003 11-Mar-14 8:22am    
change var to int.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900