我有以下.ascx页面(用户控制):

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MultiSelectDDL.ascx.cs" Inherits="MultiSelectDDL" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
 <link href="Styles/style.css" rel="stylesheet" type="text/css" />
<script type = "text/javascript">

    function CheckItem(checkBoxList) {
        debugger;
        var options = checkBoxList.getElementsByTagName('input');
        var arrayOfCheckBoxLabels = checkBoxList.getElementsByTagName("label");
        var s = "";

        for (i = 0; i < options.length; i++) {
            var opt = options[i];
            if (opt.checked) {
                s = s + ", " + arrayOfCheckBoxLabels[i].innerHTML;
            }
        }
        if (s.length > 0) {
            s = s.substring(2, s.length);
        }
        var TxtBox = document.getElementById("<%=txtCombo.ClientID%>");
    TxtBox.value = s;
    document.getElementById('<%=hidVal.ClientID %>').value = s;
}
</script>


<asp:TextBox ID="txtCombo" runat="server" ReadOnly="true" Width="138px" Font-Size="X-Small" CssClass="txtbox"></asp:TextBox>
<cc1:PopupControlExtender ID="PopupControlExtender111" runat="server"
    TargetControlID="txtCombo" PopupControlID="Panel111" Position="Bottom" >
</cc1:PopupControlExtender>

<input type="hidden" name="hidVal" id="hidVal" runat="server" />

<asp:Panel ID="Panel111" runat="server" ScrollBars="Vertical" Width="142px" Height="75" BackColor="White" BorderColor="Gray" BorderWidth="1">

    <asp:CheckBoxList ID="chkList"
        runat="server"
        Height="75" onclick="CheckItem(this)">
    </asp:CheckBoxList>

</asp:Panel>


还有另一个普通的.aspx页面,在其中放置了上述用户控件。

我需要做的是,从.aspx.cs编写的函数中,我要调用.ascx页编写的Javascript。

我试过了:

 Page.ClientScript.RegisterStartupScript(this.GetType(),"MyFunction","CheckItem('"+ MultiSelectDDL1.ClientID +"');",true);


但它不起作用。请帮助。

最佳答案

我认为问题是您传递的是字符串而不是DOM元素,您应该使用以下内容更改启动脚本。

Page.ClientScript.RegisterStartupScript(this.GetType(),"MyFunction","CheckItem(document.getElementById('"+ MultiSelectDDL1.ClientID +"'));",true);

07-24 09:19