Oxite, An open source CMS based on ASP.NET MVC by Microsoft

By Mohammad Mahdi Ramezanpour at December 09, 2008 20:30
Filed Under: Microsoft, ASP.NET, ASP.NET MVC, LINQ, SQL Server

 Oxite Logo - Photo taken from http://www.codeplex.com/oxite I know it’s a little old but in this post I want to talk about Oxite.

Oxite is a new open source blogging system based on ASP.NET MVC which built by Erik Porter, Nathan Heskew, Mike Sampson and Duncan Mackenzie.

This is a simple blog engine written using ASP.NET MVC, and is designed with two main goals:

· To provide a sample of 'core blog functionality' in a reusable fashion. Blogs are simple and well understood by many developers, but the set of basic functions that a blog needs to implement (trackbacks, RSS, comments, etc.) are fairly complex. Hopefully this code helps.

· To provide a real-world sample written using ASP.NET MVC.

As they said, this blog engine built not only for developers and as sample and this has been used in MIX Online too.

Here is some other information from http://visitmix.com/lab/oxite:

Oxite was developed carefully and painstakingly to be a great blogging platform, or a starting point for your own web site project with CMS needs. Its line-up of sexy attributes includes: provider-based architecture allowing you to swap out database and search providers (SQL Server DB, local and Live search providers included), built for testability and hence most likely to be voted "hottest in class" by TDD fans (repositories, everything has an interface, etc.), database file and string resource storage so that files get stored in database and strings stored for localization, built to take full advantage of ASP.NET MVC but broken into assemblies so that even ASP.NET WebForm developers can use the data backend and utility code, supports use of Visual Studio Team Suite (DB Pro, Test, etc.), and Background Services Architecture (sending trackbacks, emails, etc. all done as a background process to prevent delays on the web site itself).

Download it now and get some other information by click here

Manage your users with the new ASP.NET 3.5 Personalization feature – Part 1

By Mohammad Mahdi Ramezanpour at December 01, 2008 11:59
Filed Under: ASP.NET, XML

Nearly all developers who’re developing web applications have been working with sessions, cookies, etc.

For example, when you want to authenticate your users you store you user’s authentication information in sessions and cookies. Previously, I posted about ASP.NET Form Authentication which most of ASP.NET developer are working with it because it’s one of the most stable ways for user authentications.

ASP.NET 3.5 provides a new feature named “Personalization” that has very flexible infrastructure enables you to store user’s information in server not client (Cookies store information on client computers). In this post, I’m going to show you how you can use profiles and I’m sure you’ll love it.

ASP.NET has a deep focus on web.config file. You can do lots of things in your configuration file. In order to work with profiles you need to let ASP.NET what user data you want to store. So you have to add some elements to you web.config file:

<system.web>
 
    <profile>
        <properties>
            <add name="FirstName"/>
            <add name="LastName"/>
            <add name="Age" type="System.Int32"/>
        </properties>
    </profile>
    
</system.web>

Web.Config file properties are accessible in code-behind - Photo taken by myselfAs you can see in the code, you determine what properties your profile should have. You can add them very easily and Visual Studio IntelliSense will help you too.

When you added your custom properties you can see them in code-behind so you can get or set you profile information.

Notice that “Age” property in the above code snippet, I just assigned a type attribute for it. It means that you can also specify the type of a property for example:

· string: System.String

· bool: System.Boolean

· int: System.Int32

· …

You can also assign your custom type. For example, imaging you want to store user’s cart information. You can store information in a class and then set the type of your profile property to that class. Here is an example:

<Serializable()> _
Public Class Cart
 
    Private _ProductName As String
    Private _Price As Decimal
    Private _Quantity As Int16
 
    Public Property ProductName() As String
        Get
            Return _ProductName
        End Get
        Set(ByVal value As String)
            _ProductName = value
        End Set
    End Property
 
    Public Property Price() As Decimal
        Get
            Return _Price
        End Get
        Set(ByVal value As Decimal)
            _Price = value
        End Set
    End Property
 
    Public Property Quantity() As Int16
        Get
            Return _Quantity
        End Get
        Set(ByVal value As Int16)
            _Quantity = value
        End Set
    End Property
 
End Class

Note that the class must be Serializable.

Now you can add your property with your custom type:

<add name="Cart" type="Cart" serializeAs="Binary" />

You can also provide default values for your properties. It means if you leave your property with no value, a default value will be replace with null:

<add name="Active" type="System.Boolean" defaultValue="false"/>

It possible to make your properties read-only:

<add name="Active" type="System.Boolean" readOnly="true"/>

I the next part, I’ll show you how to store profile information in a data source such as SQL Server, XML, etc.

How to create a tab control using ASP.NET and JavaScript

By Mohammad Mahdi Ramezanpour at November 16, 2008 19:54
Filed Under: .NET General, ASP.NET, Web design, JavaScript

It may happen to any ASP.NET Developer that the customers want to have a Tab Control in their websites. There are many ASP.NET components such as Telerik RAD Controls, ComponentArt, DevExpress, etc. which can help you out in this but sometimes they are expensive for your project capacity.

In this post, I’m going to show you how you can create a very simple Tab Control using ASP.NET and JavaScript.

You can do lots of thing in order to create a Tab Control but what I’m going to do is to create a class that is inherited from System.Web.UI.WebControl:

    public class TabControl : System.Web.UI.WebControls.WebControl
    {
    }

You can make your Tab Control flexible by adding some properties to it. For example, enable your developers to set CssClass to a TabItem:

 

private string _MainCssClass;
private string _ItemCssClass;
private string _InActiveCssClass;
private string _ActiveCssClass; 
private Unit _ItemWidth = 70;
private HorizontalAlign _HorizontalAlign = HorizontalAlign.NotSet;
private string _LinkCssClass;

They’re OK; but the interesting part is to use of a string array in order to store TabItem names:

private string[] _TabNames = { "Item1", "Item2" };

This is also a property, so developers can add their own items. Another important property is a list of Panels of those TabItems. Actually, Panels are the content of each TabName:

private List<Panel> _Panels = new List<Panel>();

In the field above, developers can add their specified panels for each TabItem. The most important method in creating custom web controls is the Render() method which enable you to design your control using HTML tags. Here is my Render() method:

 

protected override void Render(System.Web.UI.HtmlTextWriter w)
{
    if (_Panels.Count == 0 || _Panels == null)
        throw new NullReferenceException("Please specify at least one panel.");
 
    w.WriteLine("<!-- Developed by Mohammad Mahdi Ramezanpour. © 2008 Marlik. -->");
    w.WriteLine(String.Format(@"<table cellpadding=""0"" cellspacing=""0""
    class=""{0}""><tr id=""MarlikTabControl"">", _MainCssClass));
    foreach (Panel p in this.Panels)
    {
        cols.Add("Tab_" + _itemIndex);
        w.WriteLine(String.Format(@"<td onmouseover=""javascript:this.style.cursor='hand';""
        class=""{0} {3}"" align=""{5}"" id=""Tab_{4}"" 
        onclick=""ShowTabContent('{1}', 'Tab_{4}');"" width=""{2}"">",
         _ItemCssClass, p.ClientID, this._ItemWidth, this._InActiveCssClass, 
         this._itemIndex, this._HorizontalAlign.ToString()));
        w.WriteLine(String.Format(@"<a class=""{1} {2}"">{0}</a>",
        _TabNames[_itemIndex], _LinkCssClass,  _ItemCssClass));
        _itemIndex++;
        w.WriteEndTag("td");
    }
    w.Write("</tr></table>");
    w.WriteLine(JavaFunctions().ToString());
    base.Render(w);
}

How it works?

In the first I checked for available panels and if there was no panels so an exception will be throwing using NullReferenceException.

As you know, there is a writer parameter as System.Web.UI.HtmlTextWriter in Render() method that enable you to generate HTML codes in your web component. You have to use that if you want to use HTML or any client-side code in your web control. Add I did above is to create a HTML <table></table> tag but rows and columns will be generating dynamically using HtmlTextWriter.

You can download the full code here: TabControl

How to access a control in a web user control from its parent page

By Mohammad Mahdi Ramezanpour at October 17, 2008 21:45
Filed Under: ASP.NET, .NET General

Yesterday I was answering some questions in ASP.NET forums. One of the questions I answered was “How can I access a TextBox control in a web user control from an .aspx page”.

Today I decided to post this blog to explain how you can do it.

It’s like a piece of cake. I create a web user control named “WebUserControl.ascx” and add a TextBox to it like following:

<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:TextBox runat="server" ID="TB_Test"></asp:TextBox>


Then, I create a web form named “Default.aspx” and add my user control to it:

<%@ Page Language="C#" AutoEventWireup="true"  
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register src="WebUserControl.ascx" 
tagname="WebUserControl" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
        <asp:Button ID="Button1" runat="server" 
        onclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>

As you can see, I also added a button control that reads value of our TextBox in web user control and then write it to the page. Let’s see the event handler of Button1.Click:

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox t = (TextBox)WebUserControl1.FindControl("TB_Test");
        Response.Write(t.Text);
    }

The nice part is the use of FindControl method that gives a control that I’m looking for.

Finally, I just used a simple cast in order to convert the returned control from FindControl method. Because my control was a TextBox, I created a variable as a TextBox and then fill it with my casted control. When I cast it, I can access all properties and methods related to this control.

How to use LINQ to SQL for binding data in CrystalReports 2008

By Mohammad Mahdi Ramezanpour at September 10, 2008 01:54
Filed Under: .NET General, ASP.NET, LINQ

Today I just decided to create some reports for one of our customers in Visual Studio 2008 and .NET Framework 3.5. Because I'm developing this application using LINQ, I wanted to use LINQ to SQL in CrystalReports. In this post I want to show you how you can use LINQ in order to bind data in CrystalReports.

Add CrystalReportViewer

At first, all sections are regular. You need to create a new application (I used Windows Application) and then add a Crystal Report Viewer to you specified form. When you add this control, you will see that Visual Studio added five new references to your application. These references are:

  • CrystalDecisions.CrystalReports.Engine
  • CrystalDecisions.Enterprise.Framework
  • CrystalDecisions.Enterprise.InfoStore
  • CrystalDecisions.ReportSource

You will need these libraries in order to let your application work with CrystalReports.

You made a place to show your reports in. Now you have to get started and create some reports. I want to add a new report by right clicking on my project, point to Add and then New Item. In the list of new items you need to point to Reporting section on the left side of "Add New Item" form and select Reporting. You items on templates side will be filtering to just 3 items:

  • Crystal Report
  • Report
  • Report Wizard

We want to work with CrystalReport here but for now I want to tell you other 2 items will work with Microsoft SQL Server Reporting Service that enables you to create flexible reports like CrystalReports but because CrystalReports is more common, most of developers prefer to use CrystalReports.

We just select Crystal Report and then select Add.

Because we want to work with LINQ classes, I need to create a LINQ to SQL class in order to add my tables to it and then use it in my CrystalReport file (For information about how you can create a LINQ to SQL class, Just check out my previous post: How to use SQL "IN" keyword in LINQ). Here is my tables structure:

LINQtoSQLClass

All I want is to show records from my Articles table. So I should implement this in my report. As you know when you're working with LINQ to SQL, tables become classes and fields become properties of those classes. When you want to add a new data source to your CrystalReport file (Data Fields section), there are some possibilities. You can use ADO.NET Datasets that enables you to use datasets you implemented in your application; and also you can use .NET Objects. It means that you can use classes in your application to be a data source for your report file. Because LINQ is based on classes, You must use .NET Objects in order to use LINQ to SQL as your Report's data source.

When you expand .NET Objects, you can see all classes in your application and also you can see the classes that made by LINQ class designer. Because I want to use my articles table I have to select Article class in the list as shown below:

Selecting Article Class

If you select this class as your data source you can see all fields available in the Articles table in database field section of our crystal report file. It means you can add each field you want to your report. So I just want to add Title field to my report (Details section):

Add Title To My Report

OK. That's all we need in our report file. Just one section left and that's let your report file know what data to show. You need to assign a database query to your file.

Let's go back to our report viewer file and go to code-behind section. In order to set your report file's properties, you have to create a new instance of your report file as following:

CrystalReport1 rpt = new CrystalReport1();

Now it's time to specify a query to your report file. A CrystalReport report file has a method named SetDataSource() that takes a datasource as the following types:

  • DataSet
  • DataTable
  • IDataReader
  • IEnumerable

LINQ to SQL, enables you to convert your data to a List<entityType>, Array and more. As you know List inherits IEnumerable, so All you need is to create a LINQ object and add a ToList() method to it and finally set the "SetDataSource()" method to our LINQ object as you can see in the code below:

ReportSampleDBDataContext db = 
    new ReportSampleDBDataContext();
var data = (from records in db.Articles 
            select records).ToList();
rpt.SetDataSource(data);


There is a property in our CrystalReportViewer control name ReportSource that enable you to specify which report file you want show in your CrystalReportViewer control. So you must set it up:

crystalReportViewer1.ReportSource = rpt;


It's working fine, isn't?

Quote of the day