Solved

asp.net listview delete button not working

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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