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 9196233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:48:23+00:00 2026-06-17T21:48:23+00:00

I am having trouble adding a control programmatically in a Panel where the control

  • 0

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.

  • 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-06-17T21:48:24+00:00Added an answer on June 17, 2026 at 9:48 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble dynamically adding controls inside an update panel with partial postbacks. I've
I'm having trouble adding controls with x:name to my custom control. Adding the x:name=startDate
I'm having trouble adding Facebook Connect to my iPhone Project. Here's the directory structure:
I'm having trouble adding arguments to the selector of a button (programmatically created). I've
I'm using jQuery ui tabs with the following code but am having trouble adding
I'm having trouble adding a Label control to a RichTextBox . Clearly, there must
I'm having trouble adding a mailto: link to a grid panel's toolbar. I've tried
I'm having trouble adding in jQuery's toggleClass function into the rest of my code.
I'm having trouble adding an image in xCode 4.2. When I'm inserting an UIimageview
I am having trouble adding ajax functionality to existing asp.net 4 website. I have

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.