c# - 我的面板无法显示用户是否单击特定的下拉列表项

标签 c# asp.net

我正在尝试获取<div>当特定 ListItem 时出现被选中。

在我的代码后面我有:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{                       
    if (reportedBefore.SelectedItem.Text=="yes") 
    {
        reportedBeforePanel.Visible = true;
    }
    else
    {
        reportedBeforePanel.Visible = false;
    }                   
}

我提到了this article here最初,它指出我需要一些东西:

You need to Enable the AutoPostBack of the dropdownlist for raising the OnSelectedIndexChanged event on server side.

AutoPostBack="true"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged

诚然,我没有AutoPostBack前。添加后,恐怕由于某种原因要求div还是没有显示。

<asp:DropDownList ID="reportedBefore" CssClass="larger-drop-2" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Text="Select" Value="Select"></asp:ListItem>
            <asp:ListItem Text="No" Value="No"></asp:ListItem>
            <asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
            <asp:ListItem Text="Unsure" Value="Unsure"></asp:ListItem>
 </asp:DropDownList>
   <asp:Panel ID="reportedBeforePanel" runat="server" Visible="false">
            <div id="showDiv">
              <label for="yesDetails">
                 Please provide details
              </label>
      <asp:TextBox ID="yesDetails" CssClass="third-w-form" runat="server"/>
             </div>
  </asp:Panel>

有人愿意帮助我吗?

最佳答案

问题出在以下 if 条件中:

reportedBefore.SelectedItem.Text=="yes"

这样,您将进行区分大小写的字符串比较(这是 .NET 中的默认设置),但下拉列表中的值以不同的方式写入("is" 与"is")。 "is")。

为了解决此问题,请执行不区分大小写的字符串比较

string.Compare(reportedBefore.SelectedItem.Text, "yes", true) == 0

或更改 if 语句中的大小写。

关于c# - 我的面板无法显示用户是否单击特定的下拉列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45163483/

相关文章:

c# - session 在 Controller 方法中变为空

c# - 调用 WCF 服务端点

c# - 如何在 asp.net 中动态创建 div

c# - 如何测试类的实例是否为特定泛型类型?

c# - 未处理 Windows 服务异常

c# - 如何在 ASP.Net 的 MVC Controller 中使用文件类?

c# - ASP.NET 页面中的内联编码

c# - 在javascript中更改图像控件的大小

SQL XML 或 JSON 通过 ASP.NET 返回到 Objective-C

asp.net - 如何在 ASP.NET Core 自定义标签助手中使用 View ?