Loading Controls Dynamically problem on ASP.Net 2.0

Hi Folks,

If you load your controls dynamically on asp.net 2.0 web site,you will face a problem that all handlers of server side controls doesn’t work and not fire on the server side.

Even you try to put a break point on your server side handler,it will not fire,this is not because of a problem on any of your user controls or your code has something wrong.

the problem is : if you try to write the same line of code on asp.net 1.1 web application this problem will not happen at all,and if you tru to load the same controls on asp.net 2.0 web site the problem will happen.

The solution and Tip for this problem,is to just give an ID for your user control which will be loaded dyamiccly,and the problem will not happen.

This the code which run on asp.net 1.1 perfectly and doesnt run on asp.net website :

C#:
PlaceHolderID.Controls.Clear();
Control c = Page.LoadControl(”UCPath”);
PlaceHolderID.Controls.Add(c);


The Solution :
C#
PlaceHolderID.Controls.Clear();
Control c = Page.LoadControl(”UCPat”);
c.ID = “IDForYourControl“;
PlaceHolderID.Controls.Add(c);


Hope this helps.

Read more on the problem and the solution on Scott Blog :
http://scottonwriting.net/sowblog/posts/3962.aspx







Regards,
Moustafa arafa

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>