Solved

adding multiple instance of a user control dynamically

Posted on 2013-06-05
2
199 Views
Last Modified: 2013-06-06
Hi all,

I have a button which when clicked I want to add a user control to a div;

    int count = 0;

    protected void AddSectionButton_Click(object sender, EventArgs e)
    {
        WorkoutSection newSection = (WorkoutSection)LoadControl("~/usercontrols/WorkoutSection.ascx");
        //add the section to the workout and pass to the usercontrol;

        count += 1;

        newSection.sectionName = "NEW SECTION TEST " + count;

        section_container.Controls.Add(newSection);
        
    }

Open in new window


The code work fine fo rthe first click bu tthen doesn't add subsequent instances after I have clicked? any ideas what I am doing wrong here?
0
Comment
Question by:flynny
2 Comments
 
LVL 14

Accepted Solution

by:
Daniel Junges earned 500 total points
ID: 39222750
you code have to remember all controls added and readd all on every callback
0
 
LVL 12

Expert Comment

by:Jitendra Patil
ID: 39225109
try to add below code in your button click event

 newSection.ClientIDMode = ClientIDMode.AutoID;

so i suppose your code should be as below
int count = 0;

    protected void AddSectionButton_Click(object sender, EventArgs e)
    {
        WorkoutSection newSection = (WorkoutSection)LoadControl("~/usercontrols/WorkoutSection.ascx");
        //add the section to the workout and pass to the usercontrol;

       // newSection.sectionName = "NEW SECTION TEST " + count;
         newSection.ClientIDMode = ClientIDMode.AutoID;
        section_container.Controls.Add(newSection);
       
    }


hope this helps.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now