Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i HAVE listview control on page in which i have given checkbox(in <ItemTemplate>) to select any row.....


as listview is loaded checkbox for each row gets generated based on data available in LISTVIEW.....

ID's of Checkbox in listview appears like
for First row id of checkbox is ListView1_chkbx1_0
for Second row id of checkbox is ListView1_chkbx1_1
for Third row id of checkbox is ListView1_chkbx1_2

I want your HELP to FIND out HOW MANY CHECKBOXES ARE PRESENT IN PAGE AFTER PAGE GETS LOADED

OR
U can tell me how can I find no. of rows present in listview because no. of rows and no. of checkboxes will be the same :)


Thnx in advance :)
Posted
Comments
Azee 12-Oct-13 7:52am    
Hey there, can you post the structure of your listview?
prashantttt 12-Oct-13 7:59am    
<asp:ListView ID="ListView1" runat="server">
<layouttemplate>
<table>
<tr>
<td>
<asp:Localize ID="Localize1" runat="server" Text="Select">
</td>
<td>
<asp:Localize ID="Localize2" runat="server" Text="ID">
</td>
<td>
<asp:Localize ID="Localize3" runat="server" Text="City">
</td>
<td>
<asp:Localize ID="Localize4" runat="server" Text="Country">
</td>
<td>
<asp:Localize ID="Localize5" runat="server" Text="Postal Code">
</td>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>

<itemtemplate>
<tr>
<td>
<asp:CheckBox runat="server" ID="chkbx1" />
</td>
<td>
<asp:TextBox ID="txtID" runat="server" Text='<%#Eval("CustomerID")%>'>
</td>
<td>
<asp:TextBox ID="txtCity" runat="server" Text='<%#Eval("City")%>'>

</td>
<td>
<asp:TextBox ID="txtCountry" runat="server" Text='<%#Eval("Country")%>'>
</td>
<td>
<asp:TextBox ID="txtPostalCode" runat="server" Text='<%#Eval("PostalCode")%>'>
</td>
</tr>


In client side you can achieve this as below

$('#yourList li').size() // li - is the list item which is same as your number of checkbox


Hope this helps
 
Share this answer
 
This is a regex based fastest approach using jquery

var totalElements = $('input').filter(function() {
return this.id.match(/ListView._chkbx/);
}).length;

alert(totalElements);

see it working live at http://jsfiddle.net/9HA6j/130/[^]

Thanks,
Amit
 
Share this answer
 
v3
JavaScript
--jquery--

var tr = $("#tbllist tr");

var count= tr.length;
alert(count);



and Covered my listview with table as shown below:
ASP.NET
<table id="tbllist">
<listview>
<tr></tr>  //in this tr i specified column names using <asp:localize>
</listview>

</table>


and included this line in HEAD
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/jscript"> </script>
Now I'm Getting my output
 
Share this answer
 
v4
Hey there,

First add <code>id="items"</code> property of your table inside ListView
and use this JavaScript to get number of checkBoxes.
<pre lang="Javascript">var noOfCheckBoxz = $('#items tr input[type="checkbox"]').length</pre>

Let me know if it helps.

Azee...
 
Share this answer
 
Hi,

Use jQuery to achieve this in a simple way:
JavaScript
<script type="text/javascript">
    var count = 0;
    $(document).ready(function () {
        $(".listview input[type=checkbox]").each(function () {
            count = parseInt(count + 1);
        });
        alert(count);
    });
</script></script>

Here .listview is the CssClass of listview, add this class name to your listview
 
Share this answer
 
Comments
prashantttt 12-Oct-13 7:45am    
Hi Tanweer Honestly I dont know anything about jquery....

do i need to replace listview in above line
$(".listview input[type=checkbox]").each(function () {

with listview ID...?
tanweer 12-Oct-13 7:51am    
First of all in your script section add this to refrence jQuery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
Then you need to set a CssClass of the Listview as .listview
just so simple :)
prashantttt 12-Oct-13 8:05am    
When I'm setting CssClass for listview its throwing Parser Error...
i.e. CssClass property is not availabel in Listview(i mean CssClass is not shown in intelligence box)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900