SUN
SUN

Reputation: 973

Getting selected rows from listview not working in asp.net

I am binding some data from remoter server database to my listview. I have made one column to select the row with checkbox & on button click I want to update status of those selected rows. Problem is the code which I am using is not able to find checkbox control inside listview & which resulting unable to select rows.

I have set checkbox tooltip to row ID to get selected ID of row. Listview gets bind perfectly with checkbox control inside. When put a breakpoint I noticed that 'IF' condition (if (item is CheckBox)) is not getting true hence it unable to run further code.

List<string> ListItems = new List<string>();
foreach (void el_loopVariable in shipments.Items) {
    el = el_loopVariable;
    foreach (void item_loopVariable in el.Controls) {
        item = item_loopVariable;
        if (item is CheckBox) {
            if (((CheckBox)item).Checked == true) {
                ListItems.Add(((CheckBox)item).ToolTip);
                Session["selectedConsignments"] = ListItems.ToArray();
            }
        }
    }
}

Listview (For simplicity I am just putting one column of that checkbox)

<asp:ListView ID="shipments" runat="server" DataKeyNames="ID">
    <ItemTemplate>
        <tr>
            <td id="cell13" runat="server">
                <asp:CheckBox ID="chk" runat="server" ToolTip='<%# Eval("ID") %>' />
            </td>
        </tr>
    </ItemTemplate>
</asp:ListView>

Upvotes: 0

Views: 919

Answers (2)

Hope Mystery
Hope Mystery

Reputation: 338

// here:
<td id="cell13" runat="server">

There is an upper parent control, So try:

item.Controls[0] is CheckBox 

But as I have read with compiled MSIL code if you have used:

CheckBox chk = item.Controls[0] as CheckBox;
if(chk != null)
// is executed faster 

Upvotes: 0

Chetan
Chetan

Reputation: 6901

I am not sure what exact problem you might have with the VB code of yours. But you are doing overwork to identify the checkbox in listview items. You can use FindControl which takes controlId as parameter and find any control matching with that Id in the parent control.

Consider writing your code as following.

List<string> ListItems = new List<string>();
foreach (var el_loopVariable in shipments.Items)
{
    //Passing id "chk" to FindControl method of current ListViewItem and trying to cast it as Checkbox
    var checkBox = el_loopVariable.FindControl("chk") as CheckBox;
    if (checkBox != null && checkBox.Checked)
    {
        ListItems.Add(checkBox.ToolTip);
    }
}

This should help you resolve your issue.

You also do not need to declared "runat=server" for td if you are not going to access it at your server side code.

Here is VB.NET version of the same code.

Dim ListItems As New List(Of String)()
For Each el_loopVariable As ListViewItem In shipments.Items
    Dim checkBox As CheckBox = TryCast(el_loopVariable.FindControl("chk"), CheckBox)

    If checkBox IsNot Nothing AndAlso checkBox.Checked Then
        ListItems.Add(checkBox.ToolTip)
    End If
Next

Upvotes: 2

Related Questions