Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello dear,

I have created one user control in my web application.

I have put it on my master page by adding reference as

<User_Control:Movable_Textbox ID="moving_control" runat="server" />

Defaultly when page is loaded one user control is displayed

When i click on add new control one new control is again appears on form by below code
"Movable_Text.ascx" is user control "CPH_Master" is contentplace holder of masterpage
C#
public static int li_Count = 0;
   protected void btn_New_Comment_Click(object sender, EventArgs e)
   {
       try
       {
           System.Web.UI.Control myUserControl  = (System.Web.UI.Control)Page.LoadControl("Movable_Text.ascx");
           myUserControl.ID = Convert.ToString(li_Count);
          
           CPH_Master.Controls.Add(myUserControl);
           li_Count++;
       }
       catch { }
   }

but if second time i <b>click on add new control</b> button only two buttons displayed rather adding third one.

My need is as long as i clicked on add new control new control appears on screen.

For this what steps should i follow to achieve my goal.

Please please suggest me.


Thankful in advance.
Posted
Updated 16-Jun-11 1:28am
v2

1 solution

Try this:

C#
Movable_Text ctrl = (MovableText)LoadControl("filename.ascx");


(assuming Movable_Text is the actual name of your control object).
 
Share this answer
 
Comments
Alizee@CP 16-Jun-11 7:45am    
Hi john,
I am trying like this but same happens. No third control is added

Movable_Text MT= (Movable_Text)LoadControl("Movable_Text.ascx");
this.CPH_Master.Controls.Add(MT);
#realJSOP 17-Jun-11 7:27am    
Try setting it's ID to a guid (string) instead of an integer. Have you tried running the code under the debugger to see if an exception is being thrown?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900