locked
Deleting an item of type object in Repeater control RRS feed

  • Question

  • User609686767 posted

    Hi guys,

    I have a list of Person objects, then uses that to bind to my repeater control. I have added a DELETE button to remove the item that the user selected. It seems everytime the delete button is clicked, whole process of Page_load and variable initialization restarts again, thereby clearing my List collection. How can I do persist my list collection variable, or what other way should I do this? Below is my code. Thanks in advance!

     

    .CS code

     

    namespace WebApplication1
    {
        public partial class _Default : System.Web.UI.Page
        {
            public List<Person> People = new List<Person>(); 
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!(Page.IsPostBack))
                {
                People.Add(new Person("John"));
                People.Add(new Person("Michael"));
                People.Add(new Person("Jerico"));
    
                Repeater1.DataSource = People;
                Repeater1.DataBind();
                }
            }
    
            protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
            {
                if (e.CommandName == "delete")
                {
                    LinkButton btn = e.CommandSource as LinkButton;
    
                    if (btn != null)
                    {
                        var itemID = (string)e.CommandArgument;
                        lblTest.Text = People[e.Item.ItemIndex].Name.ToString(); //test to see index
    
                        People.RemoveAt(e.Item.ItemIndex);
                        //Do Stuff 
                    }
                } 
    
            }
    
            protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.AlternatingItem ||
                    e.Item.ItemType == ListItemType.Item)
                {
                    Literal lblN =  (Literal)(e.Item.FindControl("lblName"));
    
                    lblN.Text = (String)DataBinder.Eval(e.Item.DataItem,"Name"); //test to see value
    
                }
            }
    
        }
    }
     
    Marked up code: 
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater1" runat="server" 
                onitemcommand="Repeater1_ItemCommand" onitemdatabound="Repeater1_ItemDataBound">
                <HeaderTemplate>
                </HeaderTemplate>
                <ItemTemplate>
                    <table border="1">
                        <tr>
                            <td>
                                <asp:Literal ID="lblName" runat="server"></asp:Literal>
                            </td>
                            <td>
                                <asp:LinkButton ID="lbDelete"  Text="DELETE" CommandName="delete" runat="server"></asp:LinkButton>
                            </td>
                            
                        </tr> 
                    </table>
                </ItemTemplate>
                <FooterTemplate>
                </FooterTemplate>
            </asp:Repeater>
        </div>
        <asp:Literal ID="lblTest" runat="server"></asp:Literal>
        </form>
    </body>
    </html>
    
    

     
    Saturday, July 21, 2012 8:01 AM

Answers

  • User960609757 posted

    Hi,

    This data is not stored anywhere natively from page load to page load. You will need to perform one of the following 3 tasks:

    1. Store the data in ViewState (not necessarily recommended if the data is large)
    2. Store the data in a Session object (same story, large data equals bad memory usage)
    3. Make a return trip to rebind the data each time the page loads (breaks down if there is too much activity on the database or if the query is slow)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 6, 2012 5:54 AM

All replies

  • User960609757 posted

    Hi,

    This data is not stored anywhere natively from page load to page load. You will need to perform one of the following 3 tasks:

    1. Store the data in ViewState (not necessarily recommended if the data is large)
    2. Store the data in a Session object (same story, large data equals bad memory usage)
    3. Make a return trip to rebind the data each time the page loads (breaks down if there is too much activity on the database or if the query is slow)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 6, 2012 5:54 AM
  • User609686767 posted

    Thanks mate, I have resolved this using step 2 & 3.

    Appreciate your response.

    Saturday, September 8, 2012 1:56 AM