我有一个.ascx ComboBox用户控件,其源代码定义如下。
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ComboBoxControl.ascx.cs" Inherits="UserControl_ComboBoxControl" %>
<table border="0" cellpadding="0" cellspacing="0" style="border: 0px solid red">
<tr>
<td class="DropDownLook" style="vertical-align: top" colspan="2">
<div style="vertical-align: top;width:150px;">
<div id="divCheckBoxList" ClientIDMode = "AutoID" runat="server" class="DivCheckBoxList">
<div id = "dvComboAll" >
<asp:CheckBox ID="chkAll" runat="server" ClientIDMode = "AutoID" CssClass="CheckBoxList leftPaddingforcombo" Text="ALL" ToolTip="ALL" />
</div>
<div >
<asp:CheckBoxList ID="chkMultipleValues" ClientIDMode = "AutoID" runat="server" CssClass="CheckBoxList"
Width="176px">
</asp:CheckBoxList>
</div>
</div>
</div>
</td>
</tr>
</table>
在UserControl的.cs代码后面文件中,我有以下代码行
if (!IsPostBack)
{ chkMultipleValues.Attributes.Add("onclick", "FindSelectedItems(this," + chkAll.ClientID + "');");
}
在我正在使用我的ComboBox控件的.aspx中,我有以下javascript代码的安静。
if(document.getElementById('hdnSelectedValues').value.trim() == '')
{
document.getElementById('dvErrorMsg').style.display = "block";
if(navigator.appName!="Netscape")
document.getElementById('ctl00_ContentBody_lblErrorMsg').innerHTML = DisplayMessage(3);
else
document.getElementById('ctl00_ContentBody_lblErrorMsg').innerHTML = 'No parameters selected.';
return false;
}
在IE 11中,如果我在多选ComboBoxControl中选择所有选项,document.getElementById('hdnSelectedValues').value.trim()
包含控件中的所有选项。
我遇到的问题是,如果我从ComboBoxControl中选择任意数量的选项而不是ALL,document.getElementById('hdnSelectedValues').value.trim()
将返回为空。
下面是我的“FindSelectedItems”javascript函数的代码,我附加到page_load事件后面的ComboBoxControl代码。
请注意,它在IE 5文档模式下正常工作。这个问题出现在IE 11中。
function FindSelectedItems(sender, obj) {
if (obj) {
var ChkAll = document.getElementById(obj.id);
}
document.getElementById('ctl00_ContentBody_lblErrorMsg').innerHTML = '';
var valuetodisplay = document.getElementById('ctl00_hdnSelectedValuesForDisplay').value;
document.getElementById('dvErrorMsg').style.display = "none";
var cblstTable = document.getElementById(sender.id);
var checkBoxPrefix = sender.id + "_";
var noOfOptions = cblstTable.rows.length;
var selectedText = "";
var countAll = 0;
for (i = 0; i < noOfOptions; ++i) {
if (document.getElementById(checkBoxPrefix + i).checked) {
countAll += 1;
if (selectedText == "") {
if (navigator.appName == "Netscape") {
selectedText = document.getElementById(checkBoxPrefix + i).parentNode.textContent;
valuetodisplay = document.getElementById(checkBoxPrefix + i).parentNode.textContent;
}
else {
selectedText = document.getElementById(checkBoxPrefix + i).parentNode.innerText;
valuetodisplay = document.getElementById(checkBoxPrefix + i).parentNode.innerText;
}
}
else {
if (navigator.appName == "Netscape") {
selectedText = selectedText + "," + document.getElementById(checkBoxPrefix + i).parentNode.textContent;
valuetodisplay = valuetodisplay + ", " + document.getElementById(checkBoxPrefix + i).parentNode.textContent;
}
else {
selectedText = selectedText + "," + document.getElementById(checkBoxPrefix + i).parentNode.innerText;
valuetodisplay = valuetodisplay + ", " + document.getElementById(checkBoxPrefix + i).parentNode.innerText;
}
}
}
}
document.getElementById('hdnSelectedValues').value = selectedText;
document.getElementById('ctl00_hdnSelectedValuesForDisplay').value = valuetodisplay;
if (noOfOptions == countAll) {
ChkAll.checked = true;
document.getElementById('ctl00_hdnSelectedValuesForDisplay').value = "ALL";
}
else {
if (selectedText == "") {
ChkAll.checked = false;
document.getElementById('ctl00_hdnSelectedValuesForDisplay').value = "ALL";
}
else {
ChkAll.checked = false;
}
}
}