0

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>
2
  • Does your code even compile? What does void do in foreach loop. Commented Jul 21, 2017 at 6:01
  • @ChetanRanpariya sorry but actually I am vb.net developer. I just converted my vb code to c# from telrik. Because I noticed VB posts gets unanswered mostly that's why. Commented Jul 21, 2017 at 6:05

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. your solution worked for me. And yes "runat=server" I have given because I need to use cell to change it's css from code behnd. Anyways Tysm.
0
// 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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.