Microsoft JScript运行时错误:'getCities'未定义

时间:2011-07-25 11:00:13

标签: javascript asp.net telerik runtime-error radcombobox

我在UserControll中使用RadComboBox我希望在RadComboBox中使用CheckBoxes绑定所有城市。因为我已编写代码如下:

FOR ASPX: -

<script type="text/javascript">
function getItemCheckBox(item) {
    debugger;
    //Get the 'div' representing the current RadComboBox Item.
    var itemDiv = item.get_element();

    //Get the collection of all 'input' elements in the 'div' (which are contained in the Item).
    var inputs = itemDiv.getElementsByTagName("input");

    for (var inputIndex = 0; inputIndex < inputs.length; inputIndex++) {
        var input = inputs[inputIndex];

        //Check the type of the current 'input' element.
        if (input.type == "checkbox") {
            return input;
        }
    }

    return null;
}
function check() {
    debugger;
    alert("hello");
}
function getCities() {

    var combo = $find("<%= cmbCity.ClientID %>");
    var hdnAddressType = document.getElementById("<%= hfGeoLocation.ClientID %>");
    var items = combo.get_items();
    var selectedItemsTexts = "";
    var selectedItemsValues = "";
    var itemsCount = items.get_count();
    for (var itemIndex = 0; itemIndex < itemsCount; itemIndex++) {
        var item = items.getItem(itemIndex);

        var checkbox = getItemCheckBox(item);

        //Check whether the Item's CheckBox) is checked.
        if (checkbox.checked) {
            selectedItemsTexts += item.get_text() + ", ";
            selectedItemsValues += item.get_value() + ",";
        }
    }

    hdnAddressType.value = selectedItemsValues;

    selectedItemsTexts = selectedItemsTexts.substring(0, selectedItemsTexts.length - 2);
    selectedItemsValues = selectedItemsValues.substring(0, selectedItemsValues.length - 2);

    //Set the text of the RadComboBox with the texts of the selected Items, separated by ','.
    combo.set_text(selectedItemsTexts);

    //Set the comboValue hidden field value with values of the selected Items, separated by ','.
    combo.set_value(selectedItemsValues);

    //Clear the selection that RadComboBox has made internally.
    if (selectedItemsValues == "") {
        combo.clearSelection();
    }
}
</script>

<div>
      <telerik:RadComboBox ID="cmbCity" runat="server" Height="200px" ExpandDirection="Up"
                    Width="130px">
                    <ItemTemplate>
                        <div id="chk">
                            <asp:CheckBox ID="chkCity" runat="server" onclick="getCities();" Text='<%#Eval("CityName")%>' />
                        </div>
                    </ItemTemplate>
     </telerik:RadComboBox>
     <asp:HiddenField ID="hfGeoLocation" runat="server" />
</div>

在代码背后。以下代码: -

protected void Page_Load(object sender, EventArgs e)
{
    List<usp_SelectCmbCityResult> lstCity = null;
    if (!Page.IsPostBack)
    {
        lstCity = new CityDomain().SelectCmbCity();
        cmbCity.DataSource = lstCity;
        cmbCity.DataValueField = "CityName";
        cmbCity.DataTextField = "CityName";
        cmbCity.DataBind();
        MenUs.Common.Common.BindRadioButtonList(ref rbtnOrientation, typeof(MenUs.Common.Enums.Orientation));
        MenUs.Common.Common.BindRadioButtonList(ref rbtnTargetGender, typeof(MenUs.Common.Enums.TargetGender));
        MenUs.Common.Common.BindRadioButtonList(ref rbtnTargetMarital, typeof(MenUs.Common.Enums.TargetMaritalStatus));
    }
}

当我点击/ CheckBox时,我是Gettig错误

Microsoft JScript运行时错误:'getCities'未定义

请告诉我什么是错的? 在此先感谢.....


解决了问题。实际上MasterPage中存在问题,这就是产生此错误的原因。 谢谢大家的支持......

1 个答案:

答案 0 :(得分:0)

最好在浏览器看到它时看到Javascript代码,而不是使用嵌入式ASP标记。

这些ASP标签应该转换成字符串;我怀疑问题可能是这些字符串中的一个。我不知道cmbCity.ClientIDhfGeoLocation.ClientID的值是什么,但它们可能导致代码中断。如果它们包含引号或换行符,那么您的Javascript代码将无效。

您应该使用浏览器的“查看源”功能来查看代码在浏览器中的显示方式。这很可能会告诉你问题所在。