2014-10-30 45 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

相關問題