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

  • Home
  • SEARCH
  • 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 187157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:46:26+00:00 2026-05-11T15:46:26+00:00

Possible Duplicate: .Net Changes the element IDs I have the following problem (I’ll explain

  • 0

Possible Duplicate:
.Net Changes the element IDs

I have the following problem (I’ll explain it simplified because otherwise it would get too complicated/long).

I need to create an ASP.net server control that is inherited from Panel. Simple enough. When the custom control renders, it should dynamically create a Button, associate an event handler to it (which will also be defined inside the server control) and add this button to the rendered control. The click event handler defined inside the server control does some job which for the moment isn’t interesting.

I already coded an example and that works fine. In the constructor of the server control I create a new button control, give it an ID, associate an event handler, on the OnInit of the server control I add the button to the Panel controls (remember, my control inherits from Panel) and then everything gets rendered. This looks something like the following:

public class MyCustomControl: Panel     {         private Button myButton;          public MyCustomControl()         {             myButton = new Button();             myButton.ID = 'btnTest';             myButton.Click += new EventHandler(btnTest_Click);         }          protected void btnTest_Click(object sender, EventArgs e)         {             //do something...         }          //...          protected override void OnInit(EventArgs e)         {             base.OnInit(e);              this.Controls.AddAt(0, myButton);         }          protected override void OnLoad(EventArgs e)         {             base.OnLoad(e);             //...         }          protected override void OnPreRender(EventArgs e)         {             base.OnPreRender(e);         }           protected override void Render(HtmlTextWriter output)         {             base.RenderContents(output);         }     } 

This works quite fine. The button gets rendered correctly and when I click on it, the appropriate handler inside this customer server control is invoked and executed. Now the problem however starts when I would like to add multiple instances of this server control to my page because all of my generated buttons will have the same id ‘btnTest’ which results in a conflict. So what I tried is to associate a random number to the id of the button, s.t. it end up being ‘btnTest1235512’ or something similar. This solves my problem with the multiple IDs, but results in the problem that my event handler when clicking on the button is no more called correctly. I guess this is due to the problem that my button always gets another id when entering in the constructor and so the appropriate callback (btnTest_Click event handler) isn’t found.

Can someone give me a suggestion how I could handle the problem. Is there some way for remembering the ID of the button and re-associating it. As far as I know however this has to happen in the OnInit, where the ViewState isn’t yet available. So storing the id in the ViewState wouldn’t work.

Any suggestions??

  • 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. 2026-05-11T15:46:27+00:00Added an answer on May 11, 2026 at 3:46 pm

    Implementing INamingContainer should fix the problem. You can then keep naming all your buttons btnTest.

    This is just a marker interface so you don’t have to implement any methods or properties.

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

Sidebar

Ask A Question

Stats

  • Questions 194k
  • Answers 194k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer System.Windows.Forms is not allowed inside CLR functions. http://blogs.msdn.com/tims/archive/2004/05/27/142798.aspx http://msdn.microsoft.com/en-us/library/ms403279.aspx May 12, 2026 at 6:40 pm
  • Editorial Team
    Editorial Team added an answer Try the following: public partial class PreTextBox : TextBox {… May 12, 2026 at 6:40 pm
  • Editorial Team
    Editorial Team added an answer When you give an interceptor set to an EndpointMapping, then… May 12, 2026 at 6:40 pm

Related Questions

Possible Duplicate: .Net Changes the element IDs I have the following problem (I'll explain
Possible Duplicate: How can I properly handle 404 in ASP.NET MVC? I've made the
The issue is there is a database with around 20k customer records and I
Possible Duplicate: Change CSS Dynamically I need to change the height of a div
Possible Duplicate: C# 'var' keyword versus explicitly defined variables EDIT: For those who are

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.