AMS
AMS

Reputation: 550

Multiple instances of usercontrol in asp.net

I have a user control with a textbox. onclick of a textbox opens a popupcontrolExtender which has few checkboxes on it. When a checkbox is checked it writes the checkbox values to textbox.

Here is my java script which does that:

<script type = "text/javascript">

    function CheckItem(checkBoxList) {
        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].innerText;
            }
        }
        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>

In the above js this code var TxtBox = document.getElementById("<%=txtCombo.ClientID%>"); gets the textbox and writes to it.

When I use the UserControl in my aspx page its works fine, if there is only one instance of my user control. But when I add the second instance of the usercontrol, the text is written to only the second instance of the text box. If I check/uncheck checkboxes the first in the first instance of user control, still the text is added to the altest check box.

Can some one help me resolving this issuse? How do I get the each insatnce of the check box so that i can modify my java script?

Edit

addding the complete code

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MultiSelectDropDown.ascx.cs" Inherits="MenuTest.MultiSelectDropDown" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<script type = "text/javascript">

    function CheckItem(checkBoxList) {
        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].innerText;
            }
        }
        if (s.length > 0) {
            s = s.substring(2, s.length); //sacar la primer 'coma'
        }
        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="150" Font-Size="X-Small"></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="150" Height="110" BackColor="AliceBlue" BorderColor="Gray" BorderWidth="1">

    <asp:CheckBoxList ID="chkList" 
        runat="server" 
        Height="80" onclick="CheckItem(this)">      
        <asp:ListItem Text="Contains" Value="Contains"/>
        <asp:ListItem Text="Related" Value="Related"/>
        <asp:ListItem Text="Exact" Value="Exact"/>
    </asp:CheckBoxList>
</asp:Panel>

Upvotes: 5

Views: 3533

Answers (4)

rdans
rdans

Reputation: 2157

Another approach to this is to make the javascript function name unique to each instance of the user control. You can do this by appending the user controls client id to the function name.

function CheckItem<%=ClientID %>(checkBoxList) {

Be sure to also include the client id when calling the function.

Height="80" onclick="CheckItem<%=ClientID %>(this)">

Upvotes: 2

Hanlet Esca&#241;o
Hanlet Esca&#241;o

Reputation: 17380

Everytime you have an instance of your UserControl you are also re-adding the javascript function. You should add the javascript function once, so remove it from the UserControl and add it in your main .aspx page, and change it to something like this:

function CheckItem(chk, txt, hid) {
    var tbl = chk.parentNode.parentNode.parentNode; //table with checkboxes

    var options = tbl.getElementsByTagName('input'); //checkboxes
    var arrItems = new Array(); //array for selected items

    for (i = 0; i < options.length; i++) {
        var opt = options[i];
        if (opt.checked) {
            arrItems.push(opt.value); //if checked, push it in array
        }
    }

    txt.value = arrItems.join(", "); //use join to comma separate them
    hid.value = arrItems.join(); //comma separated without extra space
}

Now let's call the function from every Checkbox, instead from the CheckBoxList. To do that, we add an attribute to each time user clicks on a checkbox item, instead of the entire control, we place it in the code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        foreach (ListItem li in this.chkList.Items)
        {
            li.Attributes.Add("onclick", "CheckItem(this, document.getElementById('" + txtCombo.ClientID + "'), document.getElementById('" + hidVal.ClientID + "'))");
        }
    }
}

Finally, remove any javascript events from your CheckBoxList, since we did this in the code behind:

<asp:CheckBoxList ID="chkList" runat="server" Height="80">
    <asp:ListItem Text="Contains" Value="Contains" />
    <asp:ListItem Text="Related" Value="Related" />
    <asp:ListItem Text="Exact" Value="Exact" />
</asp:CheckBoxList>

Upvotes: 2

Greg
Greg

Reputation: 746

Try adding a jQuery like selector: $( "input[id*='man']" ) will find 'bigman' and 'littleman'

Upvotes: 1

Stan Smith
Stan Smith

Reputation: 906

Including your ASP and full JavaScript would be helpful for me to get a clearer picture of your challenge.. or at least understanding where the CheckItem() call is originating.

With that being said.. I'm making a assumptions with this proposal:

Can you modify the caller of CheckItem() so that it includes the txtCombo.ClientID and hidVal.ClientID? similar to this:

<script type = "text/javascript">

    function CheckItem(checkBoxList, txtClientId, hidClientId) {
        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].innerText;
            }
        }
        if (s.length > 0) {
            s = s.substring(2, s.length); //sacar la primer 'coma'
        }
        var TxtBox = document.getElementById(txtClientId);
    TxtBox.value = s;
    document.getElementById(hidClientId).value = s;
    }

</script>

And your call would be something like:

CheckItem(checkBoxList, '<%=txtCombo.ClientID%>', '<%=hidVal.ClientID %>');

Upvotes: 1

Related Questions