From my previous posts, I talked about creating a custom module that complies into a dll. I've discovered that is THE hardest way possible to create a custom control. The experience was good, but not exactly fun.
Luck would have it, they have a much stupid simpler way. It's not as shareable, you have to add the files to each and evey site instead of just changing up the web.config. So what? There's a download they provide in their documentation that's kind of burried, but no sweat, you can get it here. It's a full project and it takes a minute to unzip.
Before we get started, let me explain what this custom module does and what we're going to do to it. The JobsModule consists of 4 user controls, your tried and true ascx files, that do various things, a couple classes, nothing intimidating. One neat thing they did in this example was use an interface, "IJobsControlPanel", (app_code/jobsmodule.cs:142) to switch between two modes, "Category" and "Type", but in my examples I removed this for simplicity and the real module I was creating does not call for it. Other files you need to note are all within the /Jobs folder. Control panel and Toolbox are for the admin side, where the "JobList" and "JobListSummary" are for the user side.
If you haven't went though the pain and agony of unzipping that file, do so now and let's take a look at the "JobsModule.cs" under the app code directory. Line 28 begins the Nolics database init which later on, we won't need, but note where it is. Under the properties region, lies the name/title/descript that will show up on the admin side. On line 93/94, these are the files that will show up for the user side which will get more into later, just note that is where they are. Next, take a look at line 112 and 121 as these are you admin controls. Very easy to do. Finally, there's the enum and interface for the other items they use for this module. This is 95% of what it takes to make a module.
For this example, I am going to completely ignore the code behind for the user controls on the public facing side as they are unimportant. Why? Because user controls are user controls are user controls. Make one, make 'em all, which is a very good thing! Now, let's make a custom module, shall we? Let's say we have a customer that wants a very very basic control that displays how many orders recieved today with the option to look at yesterday. Our database table will look something like this...
TableName : Orders
Id uniqueidentifier, not null, primary key
OrderDate datetime, not null
Quantity int, not null
DollarAmount money, not null
ItemOrdered nvarchar(1000), not null
Very simple, nothing fancy. For the next step, I went away from the example just a bit and created a "CustomModules" folder under the App_Code directory along with a "DAL" folder with two folders within named "Generated" and "Extended". See the image below for a bit of clarity. Now move "JobsModule.cs" into the CustomModule folder. Create yourself a folder in the root named "CustomControls" and a folder within it called "Orders" -- this is where the user controls will live.
At this point, if you've never touched subsonic, I would highly suggest jumping over there to brain up on how it works as I will not be covering that aspect. Also, if you are unfamiliar with subsonic generating only certain tables, add the tag includeTableList="Orders" to your subsonic service. For reference, this is a comma seperated list and will restrict subsonic to generating only those tables defined there. Very handy since sitefinity has around 100 tables out of the box. Generate this table and dump the files into App_Code/DAL/Generated.
Next, make a new class in App_Code/CustomModules and name it "OrdersModule.cs". On the class declaration, inherit the WebModule from the Telerik namespace, implement the abstract class and change out the constructor from public to static as seen below.
Hop over to the JobsModule and copy the Methods region (override CreateControlPanel and Override CreateToolBoxControls), paste those into your OrdersModule -- don't worry about the string values yet, we'll get to those. Also copy over the get in JobsModule:83-99 and paste that into the "Controls" override. Finally, create a private variable of IList<Telerik.Web.IToolboxItems> (look to JobsModule:40 for example).
Now that's prepped, hop over to the CustomControl folder and create 3 user controls - "ControlPanel.ascx", "ToolboxPanel.ascx" and "OrderList.ascx".
ControlPanel needs to inherit the Telerik.IControlPanel and go ahead and give some string values, similar to this...
#region IControlPanel Members private readonly string status = "Orders";
private readonly string title = "Orders";
string IControlPanel.Status
{
get { return status; }
}
string IControlPanel.Title
{
get { return title; }
}
#endregion
In the ToolboxPanel, inherit the Telerik.IControlPanelCommand and give values where necessary (Title for one).
Finally, for OrderList, we don't have to do anything yet. If you feel like it, drop some text on the page just for rendering reasons. Guess what? Most of the "behind the scenes" work is already done. No really!
At this point, it should build and give you a custom module within your sitefinify project. For the next post, I'll talk about how to wire up the subsonic stuff and even get at some data within sitefinity!
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5