簡體   English   中英

如何根據從下拉列表中選擇的內容(ASP.NET中的C#)更改標簽的輸出

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

我想做的是創建一個描述用戶在第二個下拉列表(標題為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>

嘗試如下更改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.";
 }  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM