簡體   English   中英

ASP.NET C#將所選值從下拉列表輸出更改為標簽

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

好的,所以我遇到的問題是下拉列表。 下拉列表應該輸出(Drinklabel)選定的值,它選擇了此值,但僅列出了第一個(下拉列表由會話變量生成)。

我想要它,所以我可以使用下拉列表,選擇新值,然后使標簽(Drinklabel)自行更新。

* 波紋管*

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

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

有關頁面代碼的完整信息

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


}

您沒有在將會話對象分配給Local List MyFruit之前對其進行MyFruit

因此在分配Session對象之前添加檢查

替換為:

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

具有以下內容:

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

在您的問題中,您說您始終只能從DropDownList獲得一項。 但是在這里,您只分配Session對象,因此顯然您將獲得一項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM