我正在尝试获取<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/