C#切换语句和单选按钮列表

时间:2013-06-04 15:23:46

标签: c# html

我正在处理网络表单,我遇到了一些麻烦。我在页面上有一个单选按钮列表供用户从中选择项目。根据他们的选择,我希望在屏幕上显示一个字段,以便他们详细说明他们的请求。

以下是我的单选按钮列表和面板的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;
        }
    }

目前,“破碎”,“褪色”,“迷失”和“其他”案件完美无缺。任何想法为什么其他四个不?我试着寻找拼写错误,但我没有找到任何错误。然后又可能是我无法看到自己的错误。

感谢任何帮助!

3 个答案:

答案 0 :(得分: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;
 }

答案 1 :(得分:0)

Text属性更改为Value,如下所示:

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

然后在切换案例中,检查rblReason.SelectedValue

switch (rblReason.SelectedValue)
{
.
.
.
}

答案 2 :(得分:0)

另一种需要考虑的方法是类似下面的代码,并且不需要switch语句。

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

pnlLost.Visible = string.Equals(text, "Lost", culture);
pnlDoesNotWork.Visible = string.Equals(text, "???", culture);
...