Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6922655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:26:03+00:00 2026-05-27T10:26:03+00:00

I have a ListView with an IEnumerable<MyDocument> DataSource pulled from a method. The code

  • 0

I have a ListView with an IEnumerable<MyDocument> DataSource pulled from a method.

The code in myDocsList_ItemCommand() definitely runs, because the document is actually deleted. My problem is that the ListView still shows the (now deleted) document until the next page refresh, even though I have code to do myDocsList.Items.Remove(dataItem).

The simplified .ascx is basically:

<asp:ListView id="myDocsList" runat="server" 
        OnItemDataBound="myDocsList_ItemDataBound" 
        OnItemDeleting="myDocsList_ItemDeleting" 
        OnItemCommand="myDocsList_ItemCommand">
    <LayoutTemplate>
        <table>
            <asp:Placeholder id="itemPlaceholder" runat="server" />
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr><td>
                <asp:LinkButton 
                    ID="delete" runat="server"
                    CommandName="Delete" CommandArgument="X"
                    OnClientClick="javascript:return confirm('...');">
                        Delete
                </asp:LinkButton>
        </td></tr>
    </ItemTemplate>
</asp:ListView>

The simplified .ascx.cs is basically:

protected void Page_Load(object sender, EventArgs e)
{
}

protected void Page_PreRender(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        IEnumerable<MyDocument> docs = getDocuments();
        myDocsList.DataSource = docs;
        myDocsList.DataBind();
    }
}

/* so we have the ID of the document we're deleting later on */
protected void myDocsList_ItemDataBound(object sender, 
                                        ListViewItemEventArgs e)
{
    var deleteButton = 
        (LinkButton) ((Control) e.Item).FindControl("delete");
    deleteButton.CommandArgument =
        ((MyDocument) e.Item.DataItem).id.ToString();
}

/* or we get "raised event ItemDeleting which wasn't handled" */
protected void myDocsList_ItemDeleting(Object sender, 
                                        ListViewDeleteEventArgs e)
{
}

/* do something here? */
//protected void myDocsList_ItemDeleted(Object sender, 
//                                      ListViewDeletedEventArgs e)
//{
//}

protected void myDocsList_ItemCommand(object sender, 
                                        ListViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        int docId = int.Parse(e.CommandArgument.ToString());
        deleteDocument(docId);
        ListViewDataItem dataItem = (ListViewDataItem) e.Item;
        myDocsList.Items.Remove(dataItem);
    }
}

I’ve been reading up on the ASP page lifecycle and a few related questions, but I’m reasonably inexperienced with ASP and a bit lost.

How do I get my ListView items to disappear on the PostBack instead of on the next page refresh?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:26:04+00:00Added an answer on May 27, 2026 at 10:26 am

    You have to rebound data to your ListView in ItemCommand event, you can change your coding style as below:

    //Create a new method for databind
    void BindData()
    {
        IEnumerable<MyDocument> docs = getDocuments();
        myDocsList.DataSource = docs;
        myDocsList.DataBind();
    }
    
    //Call databind method in your prerender event
    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    
    //Again bind data after delete operation
    protected void myDocsList_ItemCommand(object sender,
           ListViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            int docId = int.Parse(e.CommandArgument.ToString());
            deleteDocument(docId);
            BindData();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have listview an i am populating it with songs from the sd card.
I have a WPF ListView and I bind it to a IEnumerable<T> collection. Everything
I have problem when i using ListView and Linq as datasource. The error down:
I have listview with some listadapter after click on item from list I'd like
I have ListView which shows images from an ImageList. Now wanted to get index
I have a listview that loads dynamic controls from xml/xslt <asp:ListView ID=DynamicFields runat=server DataSourceID=CustomFields
I have a listview that is populated by a by objects from an Offer
I have ListView , which displays records (movies) from the SQLite DB. Each record
I have a listview and a button to remove items from that listview. The
I have a listview extends Activity and use custom adapter because I want icon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.