【问题标题】:System.Web.HttpException: 'Cannot have multiple items selected in a DropDownListSystem.Web.HttpException: '不能在 DropDownList 中选择多个项目
【发布时间】:2018-03-26 17:49:45
【问题描述】:

我意识到这个问题已经以各种形式被问过很多次,但我还没有找到解决我的问题的解决方案。

我正在写一个网站来记录死者的详细信息。我的网页有一个标题下拉框和一个性别下拉框。

在数据库中,这些是链接的,我需要根据标题设置性别。

我有一个列表框,其中包含标题 ID/性别 ID 链接以及使用此列表框从标题中查找性别的功能。

<asp:Label CssClass="LegacyLabel">Title</asp:Label>
<asp:ListBox ID="TitleSex" runat="server" CssClass="hidethis" SelectionMode="Single"></asp:ListBox>
<asp:DropDownList ID="LegatorTitle" runat="server" CssClass="LegacyDDL" TabIndex="2" onchange="setSex()"></asp:DropDownList>
<asp:Label CssClass="LegacyLabel">Sex</asp:Label>
<asp:DropDownList ID="LegatorSex" runat="server" CssClass="LegacyDDL" TabIndex="5"></asp:DropDownList>

<script>
function setSex() {
    var t = document.getelementbyid("<%=LegatorTitle.ClientID %>");
    var ts = document.getElementById("<%=TitleSex.ClientID %>");
    var s = document.getElementById("<%=LegatorSex.ClientID %>");
    //get title selectd value
    var tValue = t.options[t.selectedIndex].value;
    var opts = ts.options;
    //set the title sex selected value
    ts.selectedItem.selected = false;
    for (var z0 = 0; z0 < opts.length; z0++) {
        if (opts[z0].value == tValue) {
            ts.selectedIndex = z0;
            break;
        }
    }
    //get the title sex selected text
    var tsText = ts.options[ts.selectedIndex].text;
    opts = s.options;
    //set the sex selected value
    s.selectedItem.selected = false;
    for (z0 = 0; z0 < opts.length; z0++) {
        if (opts[z0].value == tsText) {
            s.selectedIndex = z0;
            break;
        }
    }
}
</script>

下拉列表和列表框是通过使用数据表的 Web 服务设置的

dataTable = fcws.GetTitles(UNAME, PWORD)
LegatorTitle.Items.Clear()
LegatorTitle.DataSource = dataTable
LegatorTitle.DataTextField = "TitleName"
LegatorTitle.DataValueField = "TitleId"
LegatorTitle.DataBind()

TitleSex.Items.Clear()
TitleSex.DataSource = dataTable
TitleSex.DataTextField = "SexId"
TitleSex.DataValueField = "TitleId"
TitleSex.DataBind()
dataTable = fcws.GetTitles(UNAME, PWORD)
LegatorTitle.Items.Clear()
LegatorTitle.DataSource = dataTable
LegatorTitle.DataTextField = "TitleName"
LegatorTitle.DataValueField = "TitleId"
LegatorTitle.DataBind()

'sex
dataTable = fcws.GetSexes(UNAME, PWORD)
LegatorSex.Items.Clear()
LegatorSex.DataSource = dataTable
LegatorSex.DataTextField = "SexName"
LegatorSex.DataValueField = "SexId"
LegatorSex.DataBind()

我还有案例打开和案例关闭日期和状态(带有打开和关闭选项的下拉列表)

当我尝试加载打开的箱子时,一切正常

当我尝试加载已关闭的案例时,我收到以下错误

    Server Error in '/' Application

    Cannot have multiple items selected in a DropDownList

    Source error

    Line 245:            var t = document.getelementbyid("<%=LegatorTitle.ClientID %>");
    Line 246:            var ts = document.getElementById("<%=TitleSex.ClientID %>");
--> Line 247:            var s = document.getElementById("<%=LegatorSex.ClientID %>"); <---
    Line 248:            //get title selected value
    Line 249:            var tValue = t.options[t.selectedIndex].value;

(--> ...

I cannot post images yet so here is a link to the error

我不明白的是:-

  1. 首先为什么会出现错误
  2. 为什么错误只发生在已关闭的案例中
  3. 为什么,当我换行时 'var s = document.getElementById("");'到 'var s = document.getElementById("LegatorSex");',错误移动到行 var ts = document.getElementById(""); (如果我更改了上面代码中的所有行,错误将移动到带有 的上一行)

我检查了有帮助的回复(这就是为什么我在绑定列表之前清除列表以及为什么我在重新设置之前将 selectedItem.selected 设置为 false)完全没有帮助(错误非常具体:你下拉列表中不能有多个选择。找到另一个更适合您需要的控件!)但似乎没有任何效果。

【问题讨论】:

  • 第 10 行的开始 - 第 245 行:document.getelementbyid 应该是 document.getElementById

标签: javascript html asp.net


【解决方案1】:

我找到了答案 (而且我觉得有点愚蠢,我之前没有发现这个......)

作为工作的一部分,我不得不将案例状态默认为打开新案例。我添加的将案例状态设置为打开的代码正在针对所有案例运行,而不仅仅是新案例。

打开的案例工作正常,因为选择已设置为“打开”

非常感谢您的意见,非常抱歉因为我的愚蠢而浪费您的时间

我仍然不知道为什么错误将失败的行与

一起报告

'getelementbyid("");'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-10
    • 2013-07-06
    • 2014-01-26
    • 1970-01-01
    • 2019-09-19
    • 2011-05-21
    • 1970-01-01
    • 2012-08-25
    相关资源
    最近更新 更多