Saturday, March 31, 2012

Sharing functions between controls

I am building an application with custom controls. All of these controls share common features and interface objects (like an MS window does: close, minimize, etc). I wanted to know if there was a way to build a template that would hold all of the shared functions and interface objects.

Thanks for your help.

Build a base class, and have all other classes inherit that one. All child classes will have the same functionality unless they override the parent's function.


Create a template base class which inherits from system.Web.UI.Page, write all your functionalities in this base class. Now in your custom control inherits the template base class.


That sounds like exactly what I was looking for. I am relatively new to the base class / inherit process so please forgive me if I ask any dumb questions.

1. I create aclass filein my App_Code folder called ControlTemplate.cs.
2. I use ControlTemplate : System.UI.Page so it inherits from Page.
3. I have my controls use MyControl : ControlTemplate so it inherits from Control Template.

Does that sound right? Would I have to program the border and navigation buttons on every control or is there a way to create those within the template? How would I tell the template where the control body should be displayed?

Thanks for all of your help.


those 3 steps are correct. To make it so that the ControlTemplate cannot be created directly using new (i.e., a child control must be created), make it an abstract class.

You can use a master page with your controls to supply the border and navigation buttons, and you can apply the masterpage in either the web.config (to apply it to all pages in the application) or you can override the MasterPageFile property of the ControlTemplate (set this to return the file name of the master page). This property will then be inherited by all child classes.


That is very interesting. I have not worked with abstract classes before so I am not familiar with that process.

I am currently using a master page for the entire page layout. Within a content page is where these custom controls will be found. Can I have master pages setup for each control? That is exactly what I need if it's possible. Can you provide any example of how I would implement this?

Thank you for all of your help.


Can I have master pages setup for each control?

Yes, of course, you can, but the purpose of the master page is mostly for consistent layout (look and feel). If you just want to share the UI code (html makeup), that's probably what you are look for, but if you want to share some other functionalities of different page, like security check, or more business logic, then it makes more sense to create a base class to share those code.

Also, please take a look at this : http://www.developer.com/net/asp/article.php/3605646

HTH


That base class is a great idea. Thank you for the link.

When I create a custom control the "Select Master Page" checkbox is greyed out. How do I specify the master page for the custom control?


I simply don't like Enable property which grey out the Label. What you can do for the checkbox is to override the Enable property and if it is set to true in the Render Method put two images like one for checked and for uncheck. In this manner your its always readable.
You have to make sure to maintain the view state for check one if you want to do some thing with it.
You can follow same for any control which have enable property.


If you are only going to be using a single masterpage (not changing dynamically), add the MasterPage property in the <%@. Page > directive:

<%@. Page MasterPageFile="~/MasterPage.master" %>

Don't forget to keep the rest of the attributes, though.

The easiest way to configure the page is to add a regular web form to your app, and select the masterpage. Then, in the code behind, change the "Inherits System.Web.UI.Page" to make the page inherit from your custom control.


Wouldn't it be <%@. Control instead of <%@. Page? Can I have a Page that inherits UserControl?


I have done some testing and found that <%@. Control does not allow a MasterPageFile. When I created an ASPX file and link that as the control I got an error that it didn't inherit UserControl. I changed it to : UserControl and now I get the following error.

CS0115: 'ASP.test_controls_testcontrol2_aspx.GetTypeHashCode()': no suitable method found to override

Any suggestions?


You said you were creating a custom control that inherited from page. Why did you switch to a usercontrol? You should create your custom page in a class, not in a .ascx user control.


Sorry for the confusion. mihirpathak said I should inherit from page, but I am not sure that is the proper direction for what I'm looking for. All I want is to have multiple custom controls all share a similar template and functions. I want something to wrap around my custom controls the same way a master page wraps around a content page.


Ah. If the design of the "wrapping control" is going to be the same in all control types, then I would create a control for the wrapping control. Then the base class for your other controls should be a composite control that uses this wrapping control as the outer-most container, and add all the other controls inside of it.

0 comments:

Post a Comment