Sanju
Sanju

Reputation: 903

How to get the Id of Checkbox which is inside a Repeater

I have a repeater inside which i put a Checkbox and above rapeater there is a HTML checkbox which is used to Check/Uncheck a Checkbox which is inside repeater using client side javascript. Here is my Code: JavaScript for Check/Uncheck:

<script type="text/javascript">
    function selectAll() {

        for (i = 0; i < document.all.length; i++) {
            alert("Working");
            if (document.all[i].type == 'checkbox') {
                if (document.getElementById(cbSelectAll).Checked = true) {
                    //document.all[i].Checked = false;
                } else {
                    document.all[i].Checked = true;
                }
            }
        }
    }
</script>

HTML Code for Repeater:

<div id="hdPropertyList" runat="server">
    <table border="0" cellpadding="0" cellspacing="0" class="navigation" width="100%">
        <tr>
            <td>
                <input type="checkbox" id="cbSelectAll" onchange="selectAll()" />
                <asp:Button runat="server" ID="btnContactAll" Text="Contact All" />
            </td>
            <td id="tdOrderBy" runat="server">
            </td>
            <td>
                <asp:Label ID="lblPage" runat="server" CssClass="pageList"></asp:Label>
            </td>
        </tr>
    </table>
</div>
<div class="boxleft SearchFeaturedlist" style="display: none">
    <h2>
        Featured Properties</h2>
</div>
<asp:Repeater ID="rptPropertyList" runat="server" EnableViewState="false" OnItemDataBound="rptPropertyList_ItemDataBound"
    OnLoad="rptPropertyList_Load">
    <ItemTemplate>
        <table id="propertyTable" runat="server" enableviewstate="false">
            <tr id="tbrLabel" runat="server" enableviewstate="false">
                <td id="tbcLabel" colspan="3" runat="server" enableviewstate="false">
                </td>
            </tr>
            <tr id="tbrTitle" runat="server" enableviewstate="false">
                <td id="tbcTitle" runat="server" enableviewstate="false">
                    <asp:CheckBox ID="ChkSelect" runat="server" /><span id="spnSelect" runat="server"></span>
                </td>
            </tr>
        </table>
        <div id="divAds" runat="server" visible="false" enableviewstate="false" style="width: 100%;
            overflow: hidden">
        </div>
    </ItemTemplate>
</asp:Repeater>

Please help me in this regards. Thanks in Advance.

Upvotes: 0

Views: 5446

Answers (2)

Sanju
Sanju

Reputation: 903

I got the answer using Jquery. I used only the HTML checkbox to Check Uncheck all the checkbox on my Asp.net page.

<script type="text/javascript">
$(document).ready(function() {
    $('td.title_listing :checkbox').change(function() {
        $('#cbSelectAll').attr('checked', false);
    });
});
function CotactSelected() {
    var n = $("td.title_listing input:checked");
    alert(n.length);
    var s = "";
    n.each(function() {
        s += $(this).val() + ",";
    });
    window.location = "/D_ContactSeller.aspx?property=" + s;
    alert(s);
}

Thanks to "Paul Alan Tylor" for your guidance.

Upvotes: 1

Paul Alan Taylor
Paul Alan Taylor

Reputation: 10680

The ID of the repeater will be available through it's ClientID property.

Really, you want to be asking whether you need this at all. Why not place the repeater inside a named div, and then simply find all input elements that have a type of checkbox that reside within it ( getElementsByTagName would help here ).

With a decent js addon library, like mootools or jQuery, you'll be able to use CSS selectors, which will make your task even easier.

Here's mootools example :-

function selectAllOrNone() 
{
    var myNewValue = $('selectall').innerText == "All" ? "None" : "All";
    var myCheckers = $$('input[type=checkbox]');

    $('selectall').innerText = myNewValue;

    myCheckers.each(
        function(e) {
            e.checked = (myNewValue == "None");
        }
    );
}

Upvotes: 2

Related Questions