Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

asp.net listview delete button not working

Posted on 2011-03-04
2
Medium Priority
?
1,461 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
Mahone7 earned 2000 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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

704 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