Solved

asp.net listview delete button not working

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now