如何根据';s是从下拉列表中选择的(ASP.NET中的C#)

本文关键字:选择 ASP NET 中的 下拉列表 何根 | 更新日期: 2023-09-27 18:28:43

我想要做的是创建一个语句,描述用户在第二个下拉列表(标题为workList)中选择的工作类型。当用户从下拉列表中选择其中一个选项时,我希望该作品的简短文本描述出现在它下面的标签所在的位置(标题为lblWork)。我现在在事件中只有一个选项(workListChanged)。一旦我想好了如何显示,我就可以完成剩下的工作。但我不知道如何让标签根据选择显示一些东西。我当前收到的错误是"无法在workListChanged事件的"if"语句中将类型"string"隐式转换为"bool"。

<%@ 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>

如何根据';s是从下拉列表中选择的(ASP.NET中的C#)

尝试更改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.";
 }