SUN SUN - 14 days ago 10
ASP.NET (C#) Question

Parse selected ID of listview from one page to another page & bind listview from that selected ID

I have bind listview with database. I have checkbox in each row so I want user can select particular row with checkbox & that selected ID should get parse to another page & bind listview of that ID on another page. Following code I tried to get selected row from page1 listview but I don't have any idea how do I parse it to another page & bind listview of selected rows.

List<string> ListItems = new List<string>();
foreach (void el_loopVariable in consignements.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);
}
}
}
}


<asp:ListView ID="consignements" runat="server" DataKeyNames="ID">
<ItemTemplate>
<tr>
<td><asp:CheckBox ID="chk" runat="server" ToolTip='<%# Eval("ID") %>' /></td>
<td><asp:HyperLink ID="bookingID" runat="server" Text='<%# Eval("booking_ID") %>'></asp:HyperLink></td>
<td><%# Eval("DateOfBooking", "{0:MMM dd, yyyy}") %></td>
<td><%# Eval("consigner") %></td>
<td><%# Eval("consignee") %></td>
<td><%# Eval("origin") %></td>
<td><asp:Label ID="mode" runat="server" ToolTip='<%# Eval("mode") %>'></asp:Label></td>
<td><%# Eval("destination") %></td>
<td><%# Eval("payee") %></td>
<td><%# Eval("quantity") %></td>
<td><%# Eval("shipper") %></td>
<td><%# Eval("pod") %></td>
<td style="text-align:right"><%# Eval("refInvoiceNo") %></td>
</tr>
</ItemTemplate>
</asp:ListView>

Answer

First, add a Label to the ListView, bind the ID to it as Text and set the Visibility to False. You can use that to pass the ID back to code behind.

<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>' Visible="false"></asp:Label>

And in code behind you can loop all the ListViewDataItems and cast the Controls.

protected void Button1_Click(object sender, EventArgs e)
{
    List<string> ListItems = new List<string>();

    foreach (ListViewDataItem item in consignements.Items)
    {
        CheckBox checkBox = item.FindControl("chk") as CheckBox;
        Label label = item.FindControl("Label1") as Label;

        if (checkBox.Checked == true)
        {
            ListItems.Add(label.Text);
        }
    }
}