 |
|
 |
Hi,
It is working fine for me, able to add the user control at runtime.
One issue is when there is a image button in the user control along with other controls like telerik chart control, and when I click on the image button the main page is getting refreshed and giving unwanted result.
Note: I am adding the same user control multiple times with different values (property procedures in user control) using for loop
While dtfromtemp <= dttotemp
Dim uc As Object
uc = Page.LoadControl("~/attendancereport.ascx")
CType(uc, attendancereport).resourceselected = "all"
CType(uc, attendancereport).fromdate = dtfromtemp
CType(uc, attendancereport).todate = dtfromtemp
CType(uc, attendancereport).cameatindex = Convert.ToInt16(DropDownList2.SelectedValue.Trim)
CType(uc, attendancereport).isFirstTime = True
PlaceHolder1.Controls.Add(uc)
End while
Please suggest me, how can I restrict the main refresh when user clicks on button in user control. I tried placing the not ispostback property in main page but second time the user control itself not getting displayed.
Thanks in advance,
Praveen Kumar Palla.
|
|
|
|
 |
|
 |
I get "The Type or Namespace '[usercontrol declaration]' could not be found...".
As this is on a content page derived from a master page, possibly there are issues with the LoadControl path?
|
|
|
|
 |
|
 |
Incorrect and too shallow
|
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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?
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
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")
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
Very poor, even for newbies
|
|
|
|
 |
|
 |
I am new to ASP.NET. For me this is very helpful. This kind of articles help newbies.
|
|
|
|
 |
|
 |
They do not fire! If you have a button inside the dinamically loaded web user control, this controls won't be fired!
|
|
|
|
 |
|
 |
Take the canister and squishhh the squishy into it...
|
|
|
|
 |
|
 |
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?
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
You're right Waleed. The title is very misleading.
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
How to put this in a loop? Could you show me the sample code?
|
|
|
|
 |
|
 |
This is really misleading. i think the writer should stop acting smart and remove this post.
|
|
|
|
 |
|
 |
how can i load a conrtol a run-time without i have to reference the control?
|
|
|
|
 |
|
|
 |