Hi I am new to ASP.NET and to programming in general. I am trying to pass values from two drop down lists which will be display in a lblResult. My page load looks like this:

protected void Page_Load(object sender, EventArgs e)
    {

        {
            ddlCompany.Items.Add(new ListItem("ABC Company", "0.15"));
            ddlCompany.Items.Add(new ListItem("XYZ Inc", "0.05"));
            ddlCompany.Items.Add(new ListItem("Testing Corp", "0.20"));
            ddlCompany.Items.Add(new ListItem("Retail Sales", "0.40"));

            ddlProduct.Items.Add(new ListItem("Skateboard", "250"));
            ddlProduct.Items.Add(new ListItem("Surfboard", "598"));
            ddlProduct.Items.Add(new ListItem("Wakeboard", "459"));
            ddlProduct.Items.Add(new ListItem("Wake Skate", "125"));
        }

       
    }

I need help getting both drop down lists to calculate a total when selected. The first drop down list has the Company Name, and the product percent markup. The second drop down list has the product, with the product price. An example of what my label results should be : ABC Company, your price for a Skateboard is $287.50 and so on.

Any help I can get on this I would really appreciate it.

Thanks again,

Charlie

You need AutoPostBack = True on the dropdownlist property so the dropdownlist tells the server the value has changed.

Create a function (or use this for both)

lblResult.text = Val(ddlProduct.text) * (1 + Val(ddlCompany.text))

that's the vb.net.    i am not a c# programmer, but the converter says:

lblResult.text == Conversion.Val(ddlProduct.text) * (1 + Conversion.Val(ddlCompany.text))

If this post helped you, please add it to my reputation and mark the thread as solved.

This would be on the "SelectedIndexChange" event for the dropdownlist.

Member Avatar for rajarajan2017

Hi,

Place two dropdownlist boxes and a label in designer. Write the coding as shown below. Just you need to change the item from the ddlcompany and It will automatically calculated and show the output as you need.

<asp:DropDownList runat="server" ID="ddlCompany" AutoPostBack ="true" /><br />
    <asp:DropDownList runat="server" ID="ddlProduct"  AutoPostBack ="true" /><br />
    <asp:Label ID="lbltest" runat="server" />
Protected Sub ddlCompany_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCompany.SelectedIndexChanged
        If ddlCompany.SelectedIndex > 0 Then
            lbltest.Text = ddlCompany.Items.Item(ddlCompany.SelectedIndex).Text & ","
            lbltest.Text &= "Your price for a " & ddlProduct.Items.Item(ddlCompany.SelectedIndex).Text & "is "
            lbltest.Text &= ddlProduct.Items.Item(ddlCompany.SelectedIndex).Value * (1 + ddlCompany.Items.Item(ddlCompany.SelectedIndex).Value)
            ddlProduct.SelectedIndex = ddlCompany.SelectedIndex
        Else
            lbltest.Text = ""
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            ddlCompany.Items.Add(New ListItem("Please Select..."))
            ddlCompany.Items.Add(New ListItem("ABC Company", "0.15"))
            ddlCompany.Items.Add(New ListItem("XYZ Inc", "0.05"))
            ddlCompany.Items.Add(New ListItem("Testing Corp", "0.20"))
            ddlCompany.Items.Add(New ListItem("Retail Sales", "0.40"))

            ddlProduct.Items.Add(New ListItem("Please Select..."))
            ddlProduct.Items.Add(New ListItem("Skateboard", "250"))
            ddlProduct.Items.Add(New ListItem("Surfboard", "598"))
            ddlProduct.Items.Add(New ListItem("Wakeboard", "459"))
            ddlProduct.Items.Add(New ListItem("Wake Skate", "125"))
        End If
    End Sub

If this post helped you, please add it to my reputation and mark the thread as solved.

Thanks everyone I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.