Solved

asp.net listview delete button not working

Posted on 2011-03-04
2
1,438 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 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 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