John
John

Reputation: 217

Using the selected value of a dropdown list in an if statement

I'm trying to select the value from this drop down list to use in an if statement so that I can then do calculations based on which choice is chosen. I'm not sure if this is the right way to do it, any help would be much appreciated!

   <asp:DropDownList ID="ddlHours" runat="server">
                <asp:ListItem >Select</asp:ListItem>
                <asp:ListItem >Part-Time</asp:ListItem>
                <asp:ListItem >Full-Time</asp:ListItem>
            </asp:DropDownList>

const int PART_TIME = 15;
                const int FULL_TIME = 25;
                double fee = 0;

                if (ddlHours.SelectedItem.Value == "Part-Time")
                {
                    CalculatePartTime(PART_TIME, fee);

                }

                else if (ddlHours.SelectedItem.Value == "Full-Time")
                {
                    CalculateFullTime(FULL_TIME, fee);
                }

                lblAnswer.Text = String.Format("{0}",fee);

Upvotes: 2

Views: 27232

Answers (3)

Chris Brickhouse
Chris Brickhouse

Reputation: 648

if you open to javascript/jquery, you could do this:

var PART_Time = 15;
var FULL_TIME = 25;
var fee = 0;

$('#ddlHours').change(function() {
    var $this = $(this);
    if ($this.val() == 'Part-Time') {
        $('#labelAnswer').val(CalculatePartTime(PART_TIME, fee);
    }
    else {
        $('#labelAnswer').val(CalculateFullTime(FULL_TIME, fee);
    }
});

i know this doesn't answer your specific question but this would reduce postbacks.

Upvotes: 0

Jonysuise
Jonysuise

Reputation: 1830

I would use :

if (ddlHours.SelectedItem.Text == "Part-Time")

Upvotes: 1

COLD TOLD
COLD TOLD

Reputation: 13579

it looks like you are not looking for value you are looking for text

if(ddlHours.SelectedItem.Text == "Part-Time")

Upvotes: 0

Related Questions