N2 CMS

For some time now, I've been searching for a good ASP.NET Web Forms CMS. I had been working on a project with Sitefinity but they recently stopped supporting the free community edition and that took the sheen off it for me. In fairness to Telerik, they offered to provide free support for that project but I also found Sitefinity somewhat over-engineered and producing some pretty messy markup. I looked at Umbraco but its over-reliance on XSLT was a total turn-off. I've been criticized because as a developer I should embrace XSLT as an extra skill; well I do, and I choose not to use it when it's not needed or called for in a given situation.

Web Deployment Tool

The open source N2 CMS on CodePlex has been on my radar for a while now. There are both Web Forms and MVC versions available. I experienced some frustration in getting it up and running and vented my usual rants in the forums - but that's what they're for, right? ;-) I took one look at how the template code was created months ago and realized that was exactly what I was looking for. The documentation is lacking, but this is true of a lot of the projects on CodePlex, which is sad. That said, N2 is going to fill a glaring hole in the ASP.NET CMS market that has existed for a long time. Neither of Microsoft's efforts, MWPSK or Orchard, has gained a lot of traction or support in the community.

Installing Web Deployment Tool using Web Platform Installer

I'm going to outline the basic steps required to create a Web Forms version of the N2 CMS and how to import the downloaded zip file as a package in IIS7. The Web Deployment Tool gives us the ability to package our Web application or Web site as well as an associated database. The simplest aproach is to use the Web Platform Installer to install the Web Deployment Tool on your machine. Much thanks to Cristian Libardo for stepping me through this process.

If you haven't done so already, install the Web Platform Installer on your machine. After you run the MSI file, you should be able to see the WPI icon in IIS. Click on the icon and install the Web Deployment Tool. You should now be able to see a new Deploy panel in IIS with links to import and export site packages.

IIS Deploy Panel

Next step is to download the N2 Web Forms Template pack from CodePlex. You can save it anywhere convenient for now. Next, go to your wwwroot folder and create the top level empty folder for your new site. The N2CMS folder in the download will go directly under this top level folder. When you open IIS, you need to right-click on the new folder and select "Convert to Application".

Next, look to the new Deploy panel in IIS, in the actions column. Click on the "Import Application" link and use the wizard to pull in the zip file containing the downloaded project. If you are using SQL Server, you will need to be operating in mixed authentication mode. Create an account and password for site's DB and update the config file, I don't usually use a login when developing locally but the wizard insists on it; you can revert to integrated security afterwards, if you wish:

<connectionStrings>
    <remove name="N2CMS"/>
    <add name="N2CMS"        
        connectionString="Initial Catalog=CMS;Data Source=MYMACHINE;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>

 

You should now be able to navigate to the new CMS site in your browser thus:

  http://localhost/mysite

 

You can use the "Export Server Package" to create a zipped file to deploy to your server. When on the server use the same steps as before for importing the zip file, and don't forget to update your Web.config file with the live DB login details.

Project Structure in VS 2008N2 CMS

 

As a developer I need to have the ability to easily customize the different areas of the site using CSS. You can access the CSS for the existing themes in the App_Themes folder and use one of these as the basis for creating your own theme. The related master pages can be located in the Templates/UI/Layouts folder. I am currently working on a new CMS site for our local Farmers Market and plan to use the Stripes theme as a starting point. My first task is to widen the layout to 960px, get rid of the logo and flags and adjust the column width to fit the new layout.

I may come back and post again on the customization steps for N2 :-)

UPDATE 08-22-2010:
SQL Server sripts can be obtained here.

 

kick it on DotNetKicks.com

Tags: , ,

ASP.NET | CMS




I recently had a DropDownList being pre-populated from a database. I also wanted to insert "Select" at the top of the list and make it a required field. Googling this yielded much misinformation for what turned out to be pretty simple to solve in the end.

Say you have a DDL like this:

<asp:DropDownList id="testDDL" runat="server">
    <asp:ListItem Text="Select"></asp:ListItem>
</asp:DropDownList>

 

If we try to populate this DropDownList from a database with the values 1,2 and 3, and then try and add a RequiredFieldValidator, that validator will not fire.

Some people resort to creating a CustomValidator and/or JavaScript here. This is not necessary. We need to take a step back here and re-phrase the problem: We already have data in our list in the shape of the "Select" item. We now want to append data from the database. A quick search of the MSDN documentation leads to the AppendDataBoundItems property for the DropDownList.

From the documentation: "The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs. After data binding, the items collection contains both the items from the data source and the previously added items." The italics are mine...

We can use the InitialValue of the RequiredFieldValidator in conjunction with the AppendDataBoundItems property of the DropDownList to arrive at a working solution:

<asp:DropDownList id="testDDL" AppendDataBoundItems="true" runat="server">
    <asp:ListItem Text="Select"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="salaryReqVal" ControlToValidate="salaryDDL"
    ErrorMessage="*" Display="dynamic" InitialValue="Select"
        runat="server"></asp:RequiredFieldValidator>




Incorrect Format

 

Here's the scenario: I'm building a basic 3-layer Web application for a client with business and data layers. Queries are performed in stored procedures, just basic ADO.NET. The table I am attempting to insert a record into has a range a field types such as strings, ints, decimals, etc.

If the user is entering integer or decimal values via texboxes, which is usually the case, then you have to expect either no data or a string of possibly mal-formed data. You must filter out the mal-formed data with validation and/or convert the resulting input data to the expected database type.

If you have a database field called SquareFeet of type int and you attempt to insert the following in your code-behind insert method, you will probably see this error:

// Results in error message
AddSomething(int squareFeet)
{
    Convert.ToInt32(SquareFeetTxtBox.Text.Trim());   
}

 

You must be careful if you are trying to fetch, say, an int or decimal as input:

// Correct way if SquareFeet field is an int
int squareFeet
if (int.TryParse(squareFeetTxt.Text.Trim(), out squareFeet))
    squareFeet = Convert.ToInt32(squareFeetTxt.Text.Trim());
else
     squareFeet = 0;
// Now insert the value as part of the record

 

// Correct way if SquareFeet field is a decimal
int squareFeet
if (decimal.TryParse(squareFeetTxt.Text.Trim(), out squareFeet))
    squareFeet = Convert.ToDecimal(squareFeetTxt.Text.Trim());
else
     squareFeet = 0.0m;
// Now insert the value as part of the record

 

References

Refer to these links for the differences between Convert.ToInt, int.Parse and Int.TryParse:
http://dotnetperls.com/int-parse-conversion
http://dotnetperls.com/int-tryparse

Refer to this link for an explanation of the 0.0m decimal syntax:
http://msdn.microsoft.com/en-us/library/364x0z75(VS.80).aspx

Moral of the Story
If you are learning any language, your first priority must be to understand the types thoroughly. Resist the temptation to gloss over the grammar just to get to the fancy stuff! :-)

kick it on DotNetKicks.com

Tags: , ,

ASP.NET | C#