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 </asp:Label></th>
<th align="center"><asp:Label ID="lblName" runat="server">TelcoName </asp:Label></th>
<th align="center"><asp:Label ID="lblReloadValue" runat="server">Reload Value(RM) </asp:Label></th>
<th align="center"><asp:Label ID="lblQuantity" runat="server">Quantity </asp:Label></th>
<th align="center"><asp:Label ID="lblTotal" runat="server">Total (RM) </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?