Tom McNish
Tom McNish

Reputation: 95

How can I change the output of a label, based on what's selected from a dropdown list (C# in ASP.NET)

What I want to happen is to create a statement that describes the type of work the user selects in the second dropdown list (titled workList). When the user selects one of the options from the dropdown list, I want a short text description of that work to appear where the label below it is (titled lblWork). I only have one option in the event (workListChanged) right now. Once I figure out how to make THAT display, I should be able to finish the rest. But I can't figure out how to get the label to display something based on the selection. The error I'm currently receiving is "cannot implicity convert type 'string' to 'bool' in the "if" statement in the workListChanged event.

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

Upvotes: 0

Views: 1486

Answers (1)

Dennis R
Dennis R

Reputation: 3237

Try changing your if statement as below. You are using a = which means you are trying to assign value, rather use == for string comparison. You cannot do if (stringExpression), since an if statement only works on a boolean.

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.";
 }  

Upvotes: 1

Related Questions