Let's say I am building a bunch of UserControls in an ASP.Net Web application, and that all those user controls inherit from a custom base class that I have created, which in turn inherits from System.Web.UI.UserControl.
Now let's say that I (via my custom base class) want the contents of each user control to be automatically wrapped in an UpdatePanel control.
How would I do this? I suspect it involves overriding the CreateControlCollection and/or CreateChildControls methods in my custom base class, but I am not sure how.
Any ideas?
The easiest thing would be to just inherit your base control from UpdatePanel. Then every child control you add to it is automatically inside of one, and you don't need to worry about which container they are being placed in.
If for whatever reason this is not possible, you could override any number of methods (OnLoad, OnPreRender, CreateChildControls, etc.) to add some logic where you move any child controls that were added to your base control's control collection, to an UpdatePanel control that your control contains.
Related
I am writing a composite control in c# asp.net as an experiment and would like to have all the instances of my composite control interact with one external control. Is this possible?
Things I have tried:
option 1: check from within the control whether an external control exists on the page, and add it if it doesn't
option 2: have the target control's id passed to the composite control at design time and then use this.Page.FindControl()
Obviously it was wishful thinking that it would be that simple :)
If I try do this from within the CreateChildControls Method, this.Page.FindControl(target control) always returns null. If I try to add the control to the page from within this method, it throws an exception:
"The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases."
is there another method / event where I can achieve this?
Why don't you expose a public property on your Composite Control of what output from them, then when rendering the Panel's contents, recurse through the page, find all instances of the composite control, grab the text, and add it to the panel?
You can create multiple instances on the same Web form by implementing the INamingContainer Interface. This basically helps prevent id clashes in the same namespace.
If you want to access another control set a property on it to expose the data you want made public.
Build Composite Controls
i want to create a user control that will display an image (the location of the image is store in my db) the control will have the ability to change the image or delete it.
i want to build another control that will hold some of that control.
what are the properties that i need to set or get from the first control to the second and then to the page that will hold them?
well, it seems that we are talking about inheritance,
in my first thinking to solve this quickly you have to create a class (maybe on app_code dir) that inherits from usercontrol. Here you gonna implements tha base methods that will be used for each derived classes.
when you have this "Base class" implemented your control will not inherits fom the default usercontrol, but will from your base class.
by this way you can create a kind of base class that provides adicional properies or methods for your purposes (thats a kind of extension if you dont understand much of oop)
I don't think I understand fully how ASP.NET does inheritance of controls.
I have a user control, ucBase, which has an asp.net label in the ascx file. Code behind references the label and it works fine during run time if the control is not a parent for another user parent.
If I have another user control, ucChild, inheriting from ucBase, the label in ucBase's code is always null. ucChild has no controls in its ascx file
The server controls (like the label) needs to be declared in the ascx file and not created programmatically.
What needs to be done for ucBase to see its own controls when it's a parent user control?
The issue:
Inheritance only inherits the code part of your user control. Markup is not something that can be inherited, since it is dynamically compiled at runtime.
The relationship between the markup and your code-behind is done through the .designer.cs partial class that comes with your user control. This designer file contains declarations for all the objects in your markup. This basically decorates the code-behind class with a bunch of fields that are null object references - these will be initialized with actual instances when the compiled markup code is run.
When you inherit from the .ascx file, you are inheriting all these null object placeholders. However, since the markup in your new control is not the same as the parent control, none of those objects are actually created when the new control's markup doesn't contain the corresponding markup, and when it is parsed and compiled, all the references stay null. Does this make sense?
The fix:
The best way to do this is to make your user controls self contained, i.e. favor code-based composition rather than markup based. In other words, instead of using markup, set up your user control using Page_Init and adding all the controls you need to the Controls collection in code behind.
Then when you inherit this class, all the same code will be executed, ensuring that your child usercontrol has the same UI controls in it.
I have a ProductBox.ascx user control which I use as ItemTemplate for a DataList as i have to repeat it.Now that datalist in inside a seperate user control ProductGrid.ascx.There is a third usercontrol ProductPopUp.ascx.All these controls are on page User.aspx.
I want to access ProductBox.ascx from ProductPopUp.ascx. I want a loosely coupled solution to it.
Please mind ProductBox.ascx is inside other usercontrol
Create a Public property member on the nested control and set this as required for basic passing in data/values.
Edit: If these controls are repeated etc you can then set these new properties like setting other Controls using 'FindControl' and getting a reference to the UserControl.
Is this what you're after?
I understand your desire for loose coupling as user controls ideally should be solely responsible for their appearance. However I can imagine circumstances where parent controls need to dictate their childrens' behavior.
You could bubble the DataList's data binding event up to the parent page and then manipulate each product. Just try to minimize the complexity of the interface between the parents & kids putting as much business logic inside the product controls as you can.
What are the differences between User Control and Custom Control in ASP.NET
AFAIK, user controls are controls that you can create out of existing controls and can be part of the project and have a designer surface for you to drag/drop.
Custom controls are generally external to the project & would require to be hand-coded (using various asp.net control events & html building in the code).
User Controls are inherit from UserControl class by system default and can combine controls in terms of specific UI case and can have UI logic as well and reuse again and again anywhere within project.
Custom Controls are inherits from Control class (that you can change any control type what you want to customize) and generally use to add extra ability to an existing UI controls.
Difference between CustomControl and UserControl
So, now you got the difference between Custom Control and User Control, I guess. Let's summarize the difference again. Read the comparison below to make it clear: