 |
|
 |
Hi,
I am trying to load a usercontrol on a .aspx page dynamically on click of a button. the code for that is
Control userControl = Page.LoadControl("a.ascx") Page.Controls.Add(userControl);
Now, I have some initialization methods on the usercontrol, which should happen only when the page is not postbacked.
But since the page is already loaded and I am loading the usercontrol on click of button, .IsPostback property on the usercontrol is always true.
How do I avoid this.
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
 |
If you don't mind using jQuery, I recently blogged about a decent solution for the challenges of dynamically loading user controls:
http://samuelmueller.com/post/2008/12/20/Dynamically-Loading-ASPNET-User-Controls-with-jQuery.aspx
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
This is helpful but how do you access the methods and property of the control from the page. I need to load a different control depending on a selection made by the user. I have buttons on the page whose click events call methods on the controls that process the data selected by the user. I thought I could register one control on the page then call usercontrol.LoadControl(path) to redirect the registered control to a different ascx file. However, instead the initially registered control persists in loading. I then tried to instantiate the control in code, but then the page will not compile because the methods being called by the buttons on the page do not yet exist. Does anyone have any ideas?
|
| Sign In·View Thread·PermaLink | 1.67/5 (3 votes) |
|
|
|
 |
|
 |
I know EXACTLY what you mean. The following code I use to display the correct Details block on a drill down information page. This is what I did to get around it:
Dim objDetails As Object
Select Case ViewState("SubMode") Case SubMode.Brand objDetails = Page.LoadControl("~/Controls/BrandDetails.ascx") Case SubMode.Store objDetails = Page.LoadControl("~/Controls/StoreDetails.ascx") Case SubMode.Model objDetails = Page.LoadControl("~/Controls/ModelDetails.ascx") End Select
objDetails.DBID = ViewState("CurrentID")
phDetails.Controls.Add(objDetails)
I came to this conclusion because I know that Objects (being the base class) never bitch about which methods or properties you try to use and therefor compile just fine. The proper way to do this I guess would be to type objDetails as one of my user controls but i wasnt sure on how to do this.
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
 |
AH just figured it out...
On the .aspx page at the top I placed:
<![CDATA[<%@ reference Control="~/Controls/BrandDetails.ascx" %>]]>
and on the .vb page it allowed me to do this:
CType(objDetails, Controls_BrandDetails).DBID = ViewState("CurrentID")
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
When working with dynamically loaded controls, you may have noticed that all dynamically loaded controls are unloaded from the page when a postback occurs. This is quite annoying, since you manually have to reload them to the page after the postback, and must reload them at the right time and with the correct ID. If done wrong, the reloaded controls will not work properly. This can also become quite a problem for large, complex web applications.
A very simple and handy solution to this issue is described in this blog:
http://daniel-saidi.blogspot.com/2008/07/aspnet-dynamically-loaded-usercontrol.html
It features a slimmed, downloadable class which, hopefully, will take care of this issue for you.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
They do not fire! If you have a button inside the dinamically loaded web user control, this controls won't be fired!
|
| Sign In·View Thread·PermaLink | 1.20/5 (5 votes) |
|
|
|
 |
|
|
 |
|
 |
I have a project that has been developed using ASP.Net V1.1. In my code file I have load the control using "PlaceHolder.Control.Add(myControl)". After that I convert this project with VWD 2005. In runtime, my webpage seem to have the same behavior both V1.1 and V2.0. But after I copy the aspx file and rename it both aspx and code-file. When it executes, it loads the control twice into one placeholder. I don't know what happen and don't know how to solve this problem. Does anyone have any idea?
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
 |
Hi I notice that when you move a User control from 2003 to 2005 well it keep some code which is not needed in the code behind: private void InitializeComponent() { this.Load += new EventHandler(this.Page_Load);
}
just remove : this.Load += new EventHandler(this.Page_Load);
that help me from run twice Page_Load method
Hope that help
Lets make better software. Alexander at MGCA Ltd
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I think this is not dynamic or at least not what I was looking for (the artical header is misleadding), cause you know in advance the control that this page it will referance, so if you need 10 user-controls to be added according to user seletction, you will have to add 10 refernace in the page, Right ??
Thanks, Best Regards Waleed Seada
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
No youre not.
This is the way to dynamic add a usercontrol to a webpage. You could this code past it bewteen a for loop and you have youre dynamicely add webcontrol on youre site
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
This example is probably one of the most simplistic articles I've seen in a long time. It seems like this author reads a very simple article and spits it back out to say he published another article.
Ever thought of posting an original article that isn't in a Quick Start guide or newbie tutorial page and then claiming it as your own?
|
| Sign In·View Thread·PermaLink | 4.00/5 (3 votes) |
|
|
|
 |
|
|
 |
|
 |
Jeez man...It is actually a simple tutorial for a newbie... Helpful I think for people that are not used to UI. It's sad that people feel they should rip on someone elses correctly implemented work (however simple). Things would be different if he was wrong.
Play Nice
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
Well if its for newbies he could have explained things a little more - placeholders for instance. I think you need to put in a little more work than this before it can be called an article.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
Sure, this article is simple, but it helps. I am new to asp.net, and I didn't know how to dynamically instantiate a user control. I didn't know that a PlaceHolder needed to be used. Sure, mabye I could have used MSDN library or the quickstarts, but searching the MSDN Library inside Visual Studio to find what I wanted to know was a difficult task. (You must admit that the 'search' feature in Vis Studio, with MSDN Library installed, is primitive compared to Google.) What I am saying is this: sometimes it's much easier to search the web to find out what I need to know than it is to search the msdn library, and lucky for me this article was here to answer my newbie question.
It answered my question, but yes, it could have been more developed and talked about PlaceHolders a bit more. But since I now know PlaceHolders are needed, I can lookup PlaceHolders in the MSDN Library or something.
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
 |
Yes, maybe he should play nice, but then again the article is full of errors. Perhaps before making a simple tutorial, one should see if it's accurate.
First off, the "Reference Control" direct can go, if you are going to dynamically load a control then you don't need this, it goes against the philosophy of a dynamic load.
The rest can stay, it's relatively accurate, however it would be helpful to specify that it should be invoked in the Page_Init event and not in the Page_Load, as the Page_Load will play around with the viewstate and make the control forget it's current state.
I have the greatest respect for people that make the effort, but this gentleman should have worked on the article a bit more and do his research before posting it up.
Karell MCP, MCSE
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
First off let me say that this article is not "full of errors". It is absolutely correct from a syntactical standpoint. Can you show me how he is wrong from a syntactical standpoint? The author makes no representations that his "Sample", as it is adequately categorized here on Code Project, conforms to "Best Practices", or any other UI methodology. This is obviously not a best practices approach to User Controls or the loading thereof. The bottom line is that you are misleading many other newbies who read the first sentence of your post. I think that is ethically wrong.
I do agree with you about the philosophy of what is needed (or not needed) to dynamically load a control, though I think that if you really wanted to contribute to this site then perhaps you would offer an explanation as to why the user should not (and doesn't have to) add a page directive for the control reference.
As for your third sentence...there are many ways to manage ViewState for items on a page, or a page itself. Remember, the author did mention a couple of methods of loading the control(s) which you are simply reiterating.
The fact is that the gentleman that authored this sample could have put some more effort into this sample, but after all...it is just a sample. If you truly had respect for people that make the effort, then you would help those people learn by providing some constructive criticism as I am doing for you in this post.
You should always err on the side of being helpful, not critical. That is just part of being a more advanced developer.
All that to say; If anyone needs help figuring out how to dynamically load a user control I'd be happy to help out. 
Jason MCSD
-- modified at 21:05 Thursday 24th November, 2005
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |