Hi, I have a ListView with Checkbox in ItemTemplate. My requirement is to get all data of selected Checkbox of a row. For this I am using a Button Click which will check the selected checkbox of ListView and then it will get all the data of selected of that checkbox. Here is my code sample and please help me out where I am heading issues.
<asp:ListView ID="lstCookies" runat="server" DataKeyNames="ID" OnItemCommand="lstCookies_ItemCommand">
<LayoutTemplate>
<table summary="Shopping cart" runat="server">
<tr>
<th class="goods-page-image">Service Image</th>
<th class="goods-page-quantity">Service Name</th>
<th class="goods-page-description">Service Description</th>
<th class="goods-page-price">Selling Price</th>
<th class="goods-page-total" colspan="2">Total</th>
<th class="del-goods-col"></th>
</tr>
<tr runat="server" id="itemPlaceholder"></tr>
</table>
<asp:DataPager ID="dataPagerNumeric" PagedControlID="lstCookies" OnPreRender="dataPagerNumeric_PreRender"
runat="server" PageSize="5">
<Fields>
<asp:NumericPagerField ButtonCount="5"
NumericButtonCssClass="numeric_button"
/>
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<table summary="Shopping cart">
<tr>
<td style="display:none">
<asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>' Visible="false"></asp:Label>
</td>
<td class="goods-page-image">
<a href="javascript:;">
<img src='<%#Eval("ServiceImage") %>' alt="ServiceImage" runat="server">
<%-- <asp:Image ID="IMG1" runat="server" ImageUrl='<%#Eval("ServiceImage") %>' alt='<%#Eval("ServiceName") %>' />--%>
</a>
</td>
<td class="goods-page-quantity">
<div class="product-quantity">
<p id="product-quantity" class="form-control input-sm">
<%#Eval("ServiceName") %>
</p>
</div>
</td>
<td class="goods-page-description">
<h3><a href="javascript:;"><%#Eval("ServiceName") %></a></h3>
<%--<p><strong>Item 1</strong> - Type: Bed; Size: single</p>--%>
<em>More info is here</em>
</td>
<td class="goods-page-price">
<strong><span>₹</span><%#Eval("SellingPrice") %></strong></td>
<td class="goods-page-total">
<strong><span>₹</span><%#Eval("SellingPrice") %></strong></td>
<td class="del-goods-col">
<%--<a class="del-goods" href="javascript:;"> </a>--%>
<asp:CheckBox ID="ChkSelectItem" runat="server" Text='<%#Eval("ID") %>' />
<%--<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="SelectedItem" CommandArgument='<%#Eval("ID") %>' runat="server"></asp:LinkButton>--%>
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:ListView>
<asp:Button ID="btnCheckOut" runat="server" Text="Checkout" OnClick="btnCheckOut_Click" class="btn btn-primary" />
Aspx.cs Code:--
protected void Page_Load()
{
if(!Page.IsPostBack)
{
s1 = string.Join(",", sd.ToArray());
s2 = string.Join(",", cd.ToArray());
ds = objBAL.Get_Cart_Data1(s1, s2);
lstCookies.DataSource = ds;
lstCookies.DataBind();
}
}
protected void lstCookies_ItemCommand(object sender, ListViewCommandEventArgs e)
{
}
protected void btnCheckOut_Click(object sender, EventArgs e)
{
foreach (ListViewDataItem item in lstCookies.Items)
{
CheckBox cb = (CheckBox)item.FindControl("ChkSelectItem");
if (cb.Checked)
{
string id = lstCookies.DataKeys[item.DataItemIndex].Values[0].ToString();
var serviceImage = lstCookies.DataKeys[item.DataItemIndex].Values[1].ToString();
string serviceName = lstCookies.DataKeys[item.DataItemIndex].Values[2].ToString();
float sellingPrice = Convert.ToInt64(lstCookies.DataKeys[item.DataItemIndex].Values[3].ToString());
}
}
}
When I write above code in btnCheckOut_Click Event, then I face head with following errors:
Index was out of range. Must be non-negative and less than the size of the collection.
Kindly help me out How to get all data of selected checkbox in ListView.