简体   繁体   English

ASP.NET C#将所选值从下拉列表输出更改为标签

[英]ASP.NET C# change selected value from drop down list output to label

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). 下拉列表应该输出(Drinklabel)选定的值,它选择了此值,但仅列出了第一个(下拉列表由会话变量生成)。

I would like it so i could use the drop down list, select new value then have the label (Drinklabel) update itself. 我想要它,所以我可以使用下拉列表,选择新值,然后使标签(Drinklabel)自行更新。

* 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();
    }


}

you are not checking the session object before assigning it to the Local List MyFruit . 您没有在将会话对象分配给Local List MyFruit之前对其进行MyFruit

hence add a check before assigning the Session object 因此在分配Session对象之前添加检查

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. 在您的问题中,您说您始终只能从DropDownList获得一项。 but here you are only assigning Session object one time so obviously you will get one item. 但是在这里,您只分配Session对象,因此显然您将获得一项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM