3

I have the following .ascx page(user control):

    <%@ 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>

And another normal .aspx page, in which I have placed the above user control.

What I need to do is, from a function written in .aspx.cs I want to call the Javascript written in .ascx page.

I tried:

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

but it does not work. Plz help.

2
  • This should work, so you need to give more information. Are there any error messages in the console? Have you tried stepping through the code to see if it is being run. You need to give more info that "it does not work". Commented Dec 8, 2014 at 10:40
  • I have set a JS debugger but it never reaches there. The .aspx page contains an updatepanel, as soon as i remove it the javascript works. Why is it like that? Commented Dec 8, 2014 at 10:55

2 Answers 2

2

I think the problem is you are passing a sting instead of an DOM element you should change you start up script with the below one.

Page.ClientScript.RegisterStartupScript(this.GetType(),"MyFunction","CheckItem(document.getElementById('"+ MultiSelectDDL1.ClientID +"'));",true);
Sign up to request clarification or add additional context in comments.

Comments

0

you need to pass object inseted of client id

 Page.ClientScript.RegisterStartupScript(this.GetType(),"MyFunction","CheckItem(this);",true);

1 Comment

Whatever you are saying is all true, but the real problem is that as long as there is this updatepanel in my page I cannot call any JS from code behind as soon I remove it, everything works fine except certain styling issues. Why is this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.