Solved

asp.net listview delete button not working

Posted on 2011-03-04
2
1,372 Views
Last Modified: 2012-08-14
Hello,

I have a delete button inside the item template of a listview.  When I click the button, I am redirected to the desired page without error, but the record is not deleted.  Because there is no error I am having trouble tracking down what is wrong.

here is the listview code:
 
<asp:ListView ID="ListView1" runat="server" DataKeyNames="idTrade" 
        DataSourceID="LinqDataTrade">
        <ItemTemplate>
            <asp:HiddenField ID="idTrade" runat="server" Value='<%# Eval("idTrade") %>' />
            <div class="TradeLable">Date Trade was Entered:</div> 
            <asp:Label ID="dtCreateLabel" runat="server" Text='<%# TradeDate(Eval("dtCreate")) %>' />
            <br /><br />
            <div class="TradeLable">Last Action Taken:</div> 
            <asp:Label ID="intLastActionLabel" runat="server" 
                Text='<%# TradeAction(Eval("intLastAction")) %>' />
            <br />  <br />          
            <div class="TradeLable">Date of Last Action with Trade:</div> 
            <asp:Label ID="dtLastActionLabel" runat="server" 
                Text='<%# TradeDate(Eval("dtLastAction")) %>' />
            <br /><br />
            <div class="TradeLable">Date of the Trade:</div> 
            <asp:Label ID="dtTradeLabel" runat="server" Text='<%# TradeDate(Eval("dtTrade")) %>' />
            <br /><br />
            <div class="TradeLable">Platoon:</div> 
            <asp:Label ID="intPlatoonLabel" runat="server" 
                Text='<%# TradeShift(Eval("intPlatoon")) %>' />
            <br /><br />
            <div class="TradeLable">Hall:</div> 
            <asp:Label ID="intHallLabel" runat="server" Text='<%# Eval("intHall") %>' />
            <br /><br />
            <div class="TradeLable">Status:</div> 
            <asp:Label ID="intStatusLabel" runat="server" Text='<%# TradeStatus(Eval("intStatus")) %>' />
            <br /><br />
            <div class="TradeLable">Shift Owner:</div> 
            <asp:Label ID="chrOwnerLabel" runat="server" Text='<%# Eval("chrOwner") %>' />
            <br /><br />
            <div class="TradeLable">Memeber Who Has Taken the Shift:</div> 
            <asp:Label ID="chrMemeberLabel" runat="server" 
                Text='<%# Eval("chrMemeber") %>' />
            <br /><br />
            <div class="TradeLable">Email of Memeber:</div>
            <asp:Label ID="chrEmailLabel" runat="server" Text='<%# Eval("chrEmail") %>' />
            <br /> <br />           
            <div class="TradeLable">Reason for Shift Trade:</div>
            <asp:Label ID="chrReasonLabel" runat="server" Text='<%# Eval("chrReason") %>' />
            <br /><br />
            <div class="TradeLable"> Shift Details:</div>
            <asp:Label ID="chrDetailsLabel" runat="server" 
                Text='<%# Eval("chrDetails") %>' />
            <br /><br />
            <asp:Button ID="CancelButton" runat="server" Text="Cancel" PostBackUrl="~/Executive/Trade.aspx" />
            <br /><br />
            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                Text="Delete" PostBackUrl="~/Executive/Trade.aspx" />
            <br />
            <br />
            </span>
        </ItemTemplate>
        
        <EmptyDataTemplate>
            <span>There was no trade record retrieved.  If you have reached this page in error, please use the link above to return to the trade managment page.</span>
        </EmptyDataTemplate>
        
        <LayoutTemplate>
            <div ID="itemPlaceholderContainer" runat="server" style="">
                <span ID="itemPlaceholder" runat="server" />
            </div>
            <div style="">
            </div>
        </LayoutTemplate>
        
        
    </asp:ListView>
    
    <asp:LinqDataSource ID="LinqDataTrade" runat="server" 
        ContextTypeName="EFFU_LINQDataContext" EnableDelete="True" TableName="Trades" 
        Where="idTrade == @idTrade">
        <WhereParameters>
            <asp:QueryStringParameter DbType="Guid" DefaultValue="00000000-0000-0000-0000-000000000000" Name="idTrade" 
                QueryStringField="tid" />
        </WhereParameters>
    </asp:LinqDataSource>

Open in new window


0
Comment
Question by:jordanking
2 Comments
 
LVL 3

Accepted Solution

by:
Mahone7 earned 500 total points
ID: 35042013
edit your delete button markup to

<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                Text="Delete" CommandArgument='<%# Eval("id of the item you want to delete") %>' />

Open in new window


then add event call item command to the listview

and add this code to it:
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                Trade item = db.Trades.SingleOrDefault(p => p.id == Convert.ToInt32(e.CommandArgument));
                db.Trades.DeleteOnSubmit(item);
                db.SubmitChanges();
                Response.Redirect("~/Executive/Trade.aspx");
            }
        }

Open in new window

0
 
LVL 1

Author Closing Comment

by:jordanking
ID: 35044082
That worked, thanks!

I use vb so here it is rewritten for VB:

    Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand

        If String.Equals(e.CommandName, "Delete") Then
            Dim db As New EFFU_LINQDataContext
            Dim myGUID = New Guid(e.CommandArgument.ToString)
            Dim item As Trade = db.Trades.SingleOrDefault(Function(p) p.idTrade = myGUID)
            db.Trades.DeleteOnSubmit(item)
            db.SubmitChanges()
            Response.Redirect("~/Executive/Trade.aspx")
        End If

    End Sub
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question