我正在尝试为visual Studio中的下拉列表创建If语句。当在列表中选择一个值时,我希望它在被选择后自己显示出来,尽管列表中的所有值都被输出。下面是我正在使用的代码:
protected void btnCalculate_Click(object sender, EventArgs e)
{
//Declare Variables
String strFrance;
String strPortugal;
String strItaly;
String strSpain;
String strAmsterdam;
String strPoland;
//Assign Values
strFrance = "300";
strPortugal = "350";
strItaly = "400";
strSpain = "400";
strAmsterdam = "250";
strPoland = "350";
if (lstPackages.SelectedItem.Text == "France") ;
{
Response.Write("The Price of France is" + strFrance + "<br />");
}
if (lstPackages.SelectedItem.Text == "Portugal") ;
{
Response.Write("The Price of Portugal is" + strPortugal + "<br />");
}
if (lstPackages.SelectedItem.Text == "Italy") ;
{
Response.Write("The Price of Italy is" + strItaly + "<br />");
}
if (lstPackages.SelectedItem.Text == "Spain") ;
{
Response.Write("The Price of Spain is" + strSpain + "<br />");
}
if (lstPackages.SelectedItem.Text == "Amsterdam") ;
{
Response.Write("The Price of Amsterdam is" + strAmsterdam + "<br />");
}
if (lstPackages.SelectedItem.Text == "Poland") ;
{
Response.Write("The Price of Poland is" + strPoland + "<br />");
}
删除后的
;
如果(LSTPackages.SelectedItem.Text==“...”);
即
if (lstPackages.SelectedItem.Text == "France")
{
Response.Write("The Price of France is" + strFrance + "<br />");
}
而不是
if (lstPackages.SelectedItem.Text == "France") ;
{
Response.Write("The Price of France is" + strFrance + "<br />");
}
如果您悬停在可视化Stuido中的;
上,您将看到一个警告。
顺便提一下:您可以在创建变量时直接赋值。即
string strFrance = "300";