1

私は Web フォームを使用していますが、少し問題があります。ユーザーがアイテムを選択するためのラジオボタンリストがページにあります。彼らの選択に応じて、要求を詳しく説明するためのフィールドが画面に表示されるようにしたいと思います。

ラジオ ボタン リストとパネルの HTML は次のとおりです。

<div class="formInformation">
                    <div class="reason">
                        <h3><b>Why are you requesting a new badge?</b></h3>
                        <asp:RadioButtonList ID="rblReason" runat="server" AutoPostBack="true"
                            RepeatDirection="Vertical" Width="350px" 
                            onselectedindexchanged="rblReason_SelectedIndexChanged">
                            <asp:ListItem Text="Broken">Broken</asp:ListItem>
                            <asp:ListItem Text="Faded">Faded</asp:ListItem>
                            <asp:ListItem Text="Lost">Lost</asp:ListItem>
                            <asp:ListItem Text="NotWork">Doesn&#39;t Work</asp:ListItem>
                            <asp:ListItem Text="Name">Name Change</asp:ListItem>
                            <asp:ListItem Text="Title">Title Change</asp:ListItem>
                            <asp:ListItem Text="Dept">Dept/Location Change</asp:ListItem>
                            <asp:ListItem Text="Other">Other</asp:ListItem>
                        </asp:RadioButtonList>
                    </div>
                    <div class="reason">
                        <h3><b>Further Information:</b></h3>
                        <h4>*Fields will become visible depending on your selection to the left*</h4>
                        <asp:Panel ID="pnlLost" runat="server" Visible="False"> 
                            <div class="pnlText"><h5><b>Are you enrolled in QuickCharge?</b></h5></div>
                            <asp:RadioButtonList ID="rblLost" runat="server"
                                RepeatDirection="Horizontal" Width="350px">
                                <asp:ListItem>Yes</asp:ListItem>
                                <asp:ListItem>No</asp:ListItem>
                            </asp:RadioButtonList>
                        </asp:Panel>
                        <asp:Panel ID="pnlDoesNotWork" runat="server" Visible="False">
                            <div class="pnlText"><h5><b>Explain what doesn't work:</b></h5></div>
                            <asp:TextBox ID="txtNotWorking" runat="server" TextMode="MultiLine" 
                                    Width="225px"></asp:TextBox>
                        </asp:Panel>
                        <asp:Panel ID="pnlNameChange" runat="server" Visible="False">
                            <div class="pnlText"><h5><b>For name change:</b></h5></div>
                            <div class="pnlText">Submit request then contact HR: 770.836.9517</div>
                        </asp:Panel>
                        <asp:Panel ID="pnlTitleChange" runat="server" Visible="False">
                            <div class="pnlText"><h5><b>What is you new title?</b></h5></div>
                            <asp:TextBox ID="txtNewTitle" runat="server" TextMode="MultiLine" 
                                    Width="225px"></asp:TextBox>
                        </asp:Panel>
                        <asp:Panel ID="pnlDeptLocation" runat="server" Visible="False">
                            <div class="pnlText"><h5><b>What is your new Dept/Location?</b></h5></div>
                            <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" 
                                    Width="225px"></asp:TextBox>
                        </asp:Panel>
                        <asp:Panel ID="pnlOther" runat="server" Visible="False">
                            <div class="pnlText"><h5><b>Elaborate:</b></h5></div>
                            <asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" 
                                    Width="225px"></asp:TextBox>
                        </asp:Panel>
                    </div>
                </div>

ラジオボタンリストでの選択に基づいてパネルを表示する私のC#は次のとおりです

  protected void rblReason_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (rblReason.SelectedItem.Text)
        {
            case "Broken":
                pnlLost.Visible = false;
                pnlDoesNotWork.Visible = false;
                pnlNameChange.Visible = false;
                pnlTitleChange.Visible = false;
                pnlDeptLocation.Visible = false;
                pnlOther.Visible = false;
                break;
            case "Faded":
                pnlLost.Visible = false;
                pnlDoesNotWork.Visible = false;
                pnlNameChange.Visible = false;
                pnlTitleChange.Visible = false;
                pnlDeptLocation.Visible = false;
                pnlOther.Visible = false;
                break;
            case "Lost":
                pnlLost.Visible = true;
                pnlDoesNotWork.Visible = false;
                pnlNameChange.Visible = false;
                pnlTitleChange.Visible = false;
                pnlDeptLocation.Visible = false;
                pnlOther.Visible = false;
                break;
            case "NotWork":
                pnlDoesNotWork.Visible = true;
                pnlLost.Visible = false;
                pnlNameChange.Visible = false;
                pnlTitleChange.Visible = false;
                pnlDeptLocation.Visible = false;
                pnlOther.Visible = false;
                break;
            case "Name":
                pnlNameChange.Visible = true;
                pnlLost.Visible = false;
                pnlDoesNotWork.Visible = false;
                pnlTitleChange.Visible = false;
                pnlDeptLocation.Visible = false;
                pnlOther.Visible = false;
                break;
            case "Title":
                pnlTitleChange.Visible = true;
                pnlLost.Visible = false;
                pnlDoesNotWork.Visible = false;
                pnlNameChange.Visible = false;
                pnlDeptLocation.Visible = false;
                pnlOther.Visible = false;
                break;
            case "Dept":
                pnlDeptLocation.Visible = true;
                pnlLost.Visible = false;
                pnlDoesNotWork.Visible = false;
                pnlNameChange.Visible = false;
                pnlTitleChange.Visible = false;
                pnlOther.Visible = false;
                break;
            case "Other":
                pnlOther.Visible = true;
                pnlLost.Visible = false;
                pnlDoesNotWork.Visible = false;
                pnlNameChange.Visible = false;
                pnlTitleChange.Visible = false;
                pnlDeptLocation.Visible = false;
                break;
        }
    }

現在、「破損」、「色褪せ」、「紛失」、および「その他」のケースは完全に機能します。他の4つがそうしない理由はありますか? スペルミスを探してみましたが、見つかりませんでした。また、自分の間違いが見えないだけかもしれません。

どんな支援も大歓迎です!

4

3 に答える 3

1

ListItem現在、テキスト属性は属性のテキストではなくタグ間のテキストを提供しているため、おそらくテキストを使用する必要がありTextます。

変化する

case "Name":

case "Name Change"

また

変化する

<asp:ListItem Text="Name">Name Change</asp:ListItem>

<asp:ListItem Text="Name Change" Value="Name"></asp:ListItem>

回避できる冗長なコードがあります

 pnlLost.Visible = false;
 pnlDoesNotWork.Visible = false;
 pnlNameChange.Visible = false;
 pnlTitleChange.Visible = false;
 pnlDeptLocation.Visible = false;
 pnlOther.Visible = false;

 switch (rblReason.SelectedItem.Text)
 {
        case "NotWork":
            pnlDoesNotWork.Visible = true;
            break;
        case "Name":
            pnlNameChange.Visible = true;            
            break;
        case "Title":
            pnlTitleChange.Visible = true;
            break;
        case "Dept":
            pnlDeptLocation.Visible = true;
            break;
        case "Other":
            pnlOther.Visible = true;
            break;
 }
于 2013-06-04T15:33:59.120 に答える
0

Text属性をValue次のように変更します。

 <asp:ListItem Value="NotWork">Doesn&#39;t Work</asp:ListItem>

次に、スイッチの場合にチェックを行いますrblReason.SelectedValue

switch (rblReason.SelectedValue)
{
.
.
.
}
于 2013-06-04T15:37:49.567 に答える
0

考慮すべき別のアプローチは、以下のコードのようなもので、switch ステートメントは不要です。

var text = rblReason.SelectedItem.Text;
var culture = CultureInfo.InvariantCulture;

pnlLost.Visible = string.Equals(text, "Lost", culture);
pnlDoesNotWork.Visible = string.Equals(text, "???", culture);
...
于 2013-06-04T15:42:03.527 に答える