2014-10-30 64 views
0

更改标签的输出我想要做的是创建一条描述用户在第二个工作区中选择的工作类型的语句下拉列表(标题为workList)。当用户从下拉列表中选择其中一个选项时,我希望该工作的简短文字描述显示在其下面的标签(标题为lblWork)中。我现在只有一个选项(workListChanged)。一旦我弄清楚如何使这个显示,我应该能够完成其余的。但我无法弄清楚如何让标签显示基于选择的内容。目前我收到的错误是“不能隐含性的转换类型‘字符串’到‘布尔’‘if’语句中workListChanged事件。如何根据从下拉列表中选择的内容(ASP.NET中的C#)

<%@ Page Language="C#" Debug="true" %> 

<!DOCTYPE html> 
<script runat="server"> 

protected void workListChanged(object sender, EventArgs e) 
{ 
    if (workList.SelectedItem.Text = "Office Work") 
     lblWork.Text = "You prefer to stay inside and code your life away."; 
} 
</script> 

<html> 
<head id="Head1" runat="server"> 
    <title>Personality Test</title> 
    <style> 
     ul { 
      list-style-type: none; 

     } 
    </style> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Label id="lblName" 
      Text="Name" 
      AssociatedControlID="txtName" 
      runat="server" /> 

     <asp:TextBox 
      id="txtname" 
      AutoPostBack="true" 
      runat="server" /> 

     <br /><br /> 

     <asp:TextBox 
      id="textComments" 
      Text="Tell me a little about yourself" 
      TextMode="MultiLine" 
      Columns="30" 
      rows="10" 
      runat="server" /> 
     <br /><br /> 

     Select a gender: 
     <asp:RadioButton 
      id="rd1Male" 
      Text="Male" 
      GroupName="rgGender" 
      runat="server" /> 

     <asp:RadioButton 
      id="rd1Female" 
      Text="Female" 
      GroupName="rgGender" 
      runat="server" /> 
     <br /><br /> 


     <strong>Favorite Season:</strong> 
     <br /> 
     <asp:DropDownList 
      id="DropDownList1" 
      Runat="server" 
      AutoPostBack="true" 
      > 
     <asp:ListItem Text="Spring" /> 
     <asp:ListItem Text="Summer" /> 
     <asp:ListItem Text="Autumn" /> 
     <asp:ListItem Text="Winter" /> 
     </asp:DropDownList> 
     <br /><br /> 


     <strong>Which of the following colors are your favorite?</strong> 
     <ul> 
      <li> 
       <asp:RadioButton 
       id="rd1Red" 
       Text="Red" 
       GroupName="colors" 
       runat="server" /> 
      </li> 
      <li> 
       <asp:RadioButton 
       id="rd1Blue" 
       Text="Blue" 
       GroupName="colors" 
       runat="server" /> 
      </li> 
      <li> 
       <asp:RadioButton 
       id="rd1Purple" 
       Text="Purple" 
       GroupName="colors" 
       runat="server" /> 
      </li> 
      <li> 
       <asp:RadioButton 
       id="rd1Yellow" 
       Text="Yellow" 
       GroupName="colors" 
       runat="server" /> 
      </li> 
      <li> 
       <asp:RadioButton 
       id="rd1Green" 
       Text="Green" 
       GroupName="colors" 
       runat="server" /> 
      </li> 
      <li> 
       <asp:RadioButton 
       id="rd1Orange" 
       Text="Orange" 
       GroupName="colors" 
       runat="server" /> 
      </li> 
      <li> 
       <asp:RadioButton 
       id="rd1Violet" 
       Text="Violet" 
       GroupName="colors" 
       runat="server" /> 
      </li> 
      <li> 
       <asp:RadioButton 
       id="rd1Pink" 
       Text="Pink" 
       GroupName="colors" 
       runat="server" /> 
      </li> 
      <li> 
       <asp:RadioButton 
       id="rd1Brown" 
       Text="Brown" 
       GroupName="colors" 
       runat="server" /> 
      </li> 
      <li> 
       <asp:RadioButton 
       id="d1Grey" 
       Text="Grey" 
       GroupName="colors" 
       runat="server" /> 
      </li> 
     </ul>   
     <br /><br /> 
     <strong>Which type of work do you prefer?</strong> 
     <br /> 
      <asp:DropDownList 
      id="workList" 
      Runat="server" 
      AutoPostBack="true" 
      OnSelectedIndexChanged="workListChanged"> 
     <asp:ListItem Text="Office Work" /> 
     <asp:ListItem Text="Outdoor Work" /> 
     <asp:ListItem Text="Investigative Work" /> 
     <asp:ListItem Text="Working With People" /> 
     <asp:ListItem Text="Work Requiring Travel" /> 
     <asp:ListItem Text="Helping People" /> 
     </asp:DropDownList> 
     <br /> 

     <asp:Label 
      id="lblWork" 
      runat ="server" /> 






    </div> 
    </form> 
</body> 
</html> 
+1

需要两个等于要对比的标志,而不是之一,因为一个是转让 – 2014-10-30 23:27:01

+0

这是正确的答案,谢谢! – 2014-10-30 23:44:44

回答

1

尝试如下改变你if声明。您使用的是=这意味着你要转让价值,而使用==的字符串比较。你不能这样做if (stringExpression),因为if语句只适用于一个布尔值。

protected void workListChanged(object sender, EventArgs e) 
{ 
    if (workList.SelectedItem.Text == "Office Work") 
     lblWork.Text = "You prefer to stay inside and code your life away."; 
} 
+0

它的工作!谢谢。 – 2014-10-30 23:45:03

相关问题