好的,我遇到的问题是下拉列表。 下拉列表应该输出(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();
}
}
答案 0 :(得分:1)
在将会话对象分配到本地列表MyFruit
之前,您没有检查它。
因此在分配Session
对象
替换为:
MyFruit = Session["Fruitname"] as List<string>;
以下内容:
if(Session["Fruitname"]!=null)
MyFruit = Session["Fruitname"] as List<string>;
在您的问题中,您说您始终只能从DropDownList
获得一个项目。
但是在这里你只分配Session
个对象一次,所以显然你会得到一个项目。