1

I am having trouble adding a control programmatically in a Panel where the control has an event inside a loop. Here's my code:

pnlAccompanies.Controls.Clear();

foreach (var accompany in this.RetrieveAccompanyList())
{
    var btnRemoveAccompany = new LinkButton();
    btnRemoveAccompany.CommandArgument = accompany.AccompanyID.ToString();
    btnRemoveAccompany.CssClass = "close";
    btnRemoveAccompany.Controls.Add(new Literal() { Text = "×" });
    btnRemoveAccompany.Click += this.btnRemoveAccompany_Click;
    smGTPForm.RegisterAsyncPostBackControl(btnRemoveAccompany);

    pnlAccompanies.Controls.Add(btnRemoveAccompany);
}

And here's my code of the event:

protected void btnRemoveAccompany_Click(object sender, EventArgs e)
{
    var accompanyID = ((LinkButton)sender).CommandArgument.Parser<int>();
    var accompanies = this.RetrieveAccompanyList();
    if (accompanies.Exists(o => o.AccompanyID == accompanyID))
    {
        accompanies.RemoveAll(o => o.AccompanyID == accompanyID);
        HttpContext.Current.Session["gtpd_accompany01"] = accompanies;
        this.PopulateAccompanyList();
    }
}

When I have 2 or more value in the accompanies, the return of the ((LinkButton)sender).CommandArgument is always the last one in the loop, even though I specifically put a different value in each iteration in the ((LinkButton)sender).CommandArgument. Why is it like this?

My code is in C# 4.0, ASP.NET, build in VS2010 Pro.

Please help. Thanks in advance.

4
  • how and where is this defined..? RetrieveAccompanyList can you do a .Sort on the RetrieveAccompanyList? Commented Jan 25, 2013 at 6:24
  • RetrieveAccompanyList is just getting the value of my "gtpd_accompany01" session. I contains a List<Accompany> so I guess it was chronologically sorted. I debug my program and saw that the value inserted in the btnRemoveAccompany.CommandArgument is different in each iteration, but when the event access it, it is always the last value of the list. Commented Jan 25, 2013 at 6:27
  • you need to show all relevant code.. I would need to see how RetrieveAccompanyList is defines also if you are holding values in a List<Accompany> can you sort on that after the list is loaded Commented Jan 25, 2013 at 6:34
  • 1
    Not an answer to your question, but a suggestion: List<T>.RemoveAll() returns the number of items that were removed, so you don't need the call to Exists. Instead, use if (accompanies.RemoveAll(o => o.AccompanyID == accompanyID) != 0) { HttpContext.Current.Session["gtpd_accompany01"] = accompanies; this.PopulateAccompanyList(); } Commented Jan 25, 2013 at 7:06

1 Answer 1

2

You have not assigned the ID value for btnRemoveAccompany,so when you click on this button ,event doesn't know which control raised this event,as all the controls have same attributes.try adding ID value for all the Controls.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.