0

Ok so the problem i am having is with a drop down list. the drop down list is supposed to output (Drinklabel) the selected value, it dose this but only the first in the list (the drop down list is generated by session variable).

I would like it so i could use the drop down list, select new value then have the label (Drinklabel) update itself.

*code bellow *

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Drinklabel.Text = "Your Chosen Beverage is A " + DropDownList1.SelectedValue.ToString() + " Drink.";
    }

////////////////////////////////////////////////////////////

Full about page code

public partial class About : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        MyFruit = Session["Fruitname"] as List<string>;
        //Create new, if null
        if (MyFruit == null)
            MyFruit = new List<string>();
        DropDownList1.DataSource = MyFruit;
        DropDownList1.DataBind();

    }

    public List<string> MyFruit { get; set; }

    protected void ButtonCalculate_Click(object sender, EventArgs e)
    {
        decimal total = calculatePrice(DropDownList1.SelectedItem.Text,
                                       TextBoxQuantity.Text.Trim());

        LabelResult.Text = "You would like " + TextBoxQuantity.Text.Trim() +
            DropDownList1.SelectedItem.Text + "(s) for a total of $" +
            total.ToString();
    }

    private decimal calculatePrice(string p1, string p2)
    {
        throw new NotImplementedException();
    }

    private decimal calculatePrice(string fruitName, int quantity)
    {
        // Ask the database for the price of this particular piece of fruit by name
        decimal costEach = GoToDatabaseAndGetPriceOfFruitByName(fruitName);

        return costEach * quantity;
    }

    private decimal GoToDatabaseAndGetPriceOfFruitByName(string fruitName)
    {
        throw new NotImplementedException();
    }


}
3
  • could you please show us the code how you are binding the session data into DropDownList Commented Nov 10, 2013 at 12:41
  • ok posted above, thanks Commented Nov 10, 2013 at 13:00
  • could you please check wether Session contains valid data or not? Commented Nov 10, 2013 at 13:43

1 Answer 1

1

you are not checking the session object before assigning it to the Local List MyFruit.

hence add a check before assigning the Session object

Replace this:

MyFruit = Session["Fruitname"] as List<string>;

With following:

if(Session["Fruitname"]!=null)
MyFruit = Session["Fruitname"] as List<string>;

and in your Question you said that you are able to get only one item from DropDownList always. but here you are only assigning Session object one time so obviously you will get one item.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.