2
votes

I got a datatable in list view.

ID TelcoName Reload Value(RM) Quantity Total(RM) 1 Maxis 5 1 5 Delete 2 Digi 5 1 5 Delete

This is basically what my table looks like. My html code is this:

<asp:ListView ID="ListView1" runat="server" OnSorting="ListView1Sorting" OnItemCommand="ListView1_ItemCommand">
                    <LayoutTemplate>
                            <table border="0" cellpadding="1">
                                <tr style="background-color:#FFFFFF">
                                    <th align="center"><asp:Label ID="lblId" runat="server">Id &nbsp;</asp:Label></th>
                                    <th align="center"><asp:Label ID="lblName" runat="server">TelcoName&nbsp;</asp:Label></th>
                                    <th align="center"><asp:Label ID="lblReloadValue" runat="server">Reload Value(RM)&nbsp;</asp:Label></th>
                                    <th align="center"><asp:Label ID="lblQuantity" runat="server">Quantity&nbsp;</asp:Label></th>
                                    <th align="center"><asp:Label ID="lblTotal" runat="server">Total (RM)&nbsp;</asp:Label></th>                                    
                                    <th></th>
                                </tr>
                               <tr id="itemPlaceholder" runat="server"></tr>
                            </table>
                        </LayoutTemplate>
                        <ItemTemplate>
                        <tr>
                        <td align="center"><asp:Label runat="server" ID="lblId"><%#Eval("ID") %></asp:Label></td>
                        <td align="center"><asp:Label runat="server" ID="lblTelcoName"><%#Eval("TelcoName") %></asp:Label></td>
                        <td align="center"><asp:Label runat="server" ID="lblReloadValue"><%#Eval("ReloadValue")%></asp:Label></td>
                        <td align="center"><asp:Label runat="server" ID="lblQuantity"><%#Eval("Quantity")%></asp:Label></td>
                        <td align="center"><asp:Label runat="server" ID="lblTotal"><%#Eval("Total")%></asp:Label></td>
                        <td align="center"><asp:LinkButton ID="lnkDelete" runat="server" CommandName="Sort" CommandArgument="Delete">Delete</asp:LinkButton></td>
                        </tr>
                        </ItemTemplate>
                    </asp:ListView>

This is my behind code for deleting:

Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) If (e.CommandName) = "Sort" Then Dim txteno As Label = DirectCast(e.Item.FindControl("ID"), Label) Dim deletecommand As String = "delete from dt where ID=" & Convert.ToInt32(txteno.text) Session("dt").DeleteCommand = deletecommand End If End Sub

My problem here is i coud'nt get the ID of the row which the user choose. The "ID" is the name of the first column but when i run the website, this statement return nothing.

Dim txteno As Label = DirectCast(e.Item.FindControl("ID"), Label)

My question is how do i retrieve the value of the column("Id") to use for the deleting data? i ask around and someone suggest to me to use e.item.DataItemIndex but i coudnt find this command anywhere. Any idea how to solve this?

This has just been added

Dim dt As New DataTable Dim i = 0 Dim ID As New DataColumn("ID") dt.Columns.Add(ID) Dim TelcoName As New DataColumn("TelcoName") dt.Columns.Add(TelcoName) Dim ReloadValue As New DataColumn("ReloadValue") dt.Columns.Add(ReloadValue) Dim Quantity As New DataColumn("Quantity") dt.Columns.Add(Quantity) Dim Total As New DataColumn("Total") dt.Columns.Add(Total) ListView1.DataSource = dt ListView1.DataBind() Session("dt") = dt

After i binded to the listview, do i need to delcare a name for the datatable so that i can use it as a reference for sql statement or i can just leave it as dt and in sql statement, when i refer to dt, it refer to the table in the listview?

1

1 Answers

1
votes

EDIT: Addressing two questions separately - one is that a control handle could not be grabbed, the other question is around deleting from a DataTable

Control Reference

If you're trying to get a handle on the control using this:

Dim txteno As Label = DirectCast(e.Item.FindControl("ID"), Label)

Then I think the control ID is incorrect as in your markup you've defined it as "lblId". So perhaps try:

 Dim txteno As Label = DirectCast(e.Item.FindControl("lblId"), Label)

Also, I'm not sure what impact it would have but I would give the controls in the LayoutTemplate (so I am guessing the header for your ListView) different ID's to the controls in your ItemTemplate, if only for maintenance/cleanliness if nothing else.

The label should also be bound like:

<asp:Label id="lblId" runat="server" Text='<%# Eval("ID") %>'></asp:Label>

So that in code-behind the text can be retrieved.

Deleting from DataTable

Based on the poster's comments, the DataTable's data is not coming from a database.

Since you aren't connecting back to a database, you don't have to write SQL to remove items from the DataTable.

It looks like you're storing the DataTable in session. And also that you are binding totally in code. As such, what you could do in your ItemCommand method is:

  1. Iterate over the data table to find the item to delete
  2. Delete the item (you could consult, for example, this forum post)
  3. Once the item has been removed from the DataTable, re-bind it to the list view. As an example of someone who seems to have been in a similar situation please see this

HTH, Nathan