• Telerik Extensions for ASP.NET MVC - The Editor

    by Venkata Koppaka | Jan 19, 2011

    This is third in the series posts I am doing to cover Telerik Extensions for ASP.NET MVC.

    1. Getting Started with Telerik Extensions for ASP.NET MVC 
    2. Telerik Extensions for ASP.NET MVC - The Grid

    In today's post I will talk about using Telerik Extensions for ASP.NET - The Editor.

    I will start with a ASP.NET MVC2 application which is already configured to run Telerik Extensions, if you have not already done that, I encourage you to read my first post on getting started(link above).

    Let us create a new model for holding our data that we want to POST through the editor.

    For this demo let us add a Person Class with two properties.

        public class Person 
        { 
            public int Id { get; set; } 
            public string Name { get; set; } 
     
            public Person(int id, string name) 
            { 
                Id = id; 
                Name = name; 
            } 
        } 

    Now let us add a ActionMethod with just returns a View, let the View be strongly typed to the Person Class we created above.

    public ActionResult CreatePerson() 
            { 
                return View(); 
            } 
    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TelerikEditor.Models.Person>" %> 

    Adding a Telerik Editor is simple and it allows adding properties to the Editor with the help of a Fluent API like other Telerik MVC controls.

    Let us look at basic markup needed to add the Editor.

    <% Html.Telerik().Editor().Name("PersonDescription") 
    .Render();%> 

    If we run the application adding the basic markup for the editor, you should see the editor -

    Let us look deeply into other properties that Editor offers

    <%  
                        Html.Telerik().EditorFor(model=>model.Name) 
                                      .Name("PersonDescription") 
                                      .HtmlAttributes(new { style = "height:200px" }) 
                                      .Value("Please enter your description here") 
                                      .Tools(tool => tool 
                                                    .Clear() 
                                                    .Bold() 
                                                    .Italic() 
                                                    .Underline() 
                                                    .Snippets(snippet=>snippet.Add("Greeting","<h1>Hello World</h1>"))) 
                                      .Render(); 
                    %> 

    Let us look in detail about each of these properties.

    EditorFor for strongly binding the property

    EditorFor is variant of Editor for strongly typing model properties. It takes in the model property in the form of Func<T> you want to type to.

    Name

    The property "Name" is the actual clean HTML id of the table(textarea is wrapped inside aHTML table) that will be used for this editor. After you run the application do a View Source from your browser, you will see a div with the Id you mentioned in this Name property.

    HtmlAttributes

    This property takes in an object of HTML attributes you can apply to the editor.

    Value

    This property is used to set the editor content, the value can be a plain string or a mix of HTML and plain text. If we add some HTML to the value like -

    .Value("<h1>Please enter your description here</h1>") 
    It will automatically be rendered when the editor loads.

    Tools

    There are lots of tools that the editor supports - like Bold, Italic, Underline etc.

    Clear() - clears the current toolbar of the editor and every tool you add after calling clear will appear as in the toolbar.

    Snippets() - Snippets lets you insert pre-configured HTML into the editor.

     

    ClientAPI

    There is a very good client API for the editor that lets you do certain tasks using the editor. To dig more into these I recommend you read Telerk Editor Client API documentation.

    That's it for this post, in the next post I will be talking about how to use the Menu control.

    Hope you found this useful.

    Cheers,

    Venkata

    Go comment!
  • Happy New Year 2011

    by Venkata Koppaka | Jan 02, 2011
    I wish you all a very Happy New Year 2011.
     
    Go comment!
  • 2010 - A Year in review

    by Venkata Koppaka | Dec 29, 2010

    This is that time of the year, where I have to look back at what I have achieved in the year. It is also that time of the year to make the next years resolutions.

    Looking back

    In 2010 I did a total of 25 blog posts (26 including this one), concentrating on ASP.NET MVC, ASP.NET, SQL Server and Windows Phone 7, which are main focus areas of my blog.

    I have to admit that I didn't reach my initial goal of 40 blog posts this year when I started my blog. I also postponed building my own blog on MVC2 which I wanted to start early this year.

    Looking at the bright side of things, I did learn new technologies like Silverlight for Windows Phone 7. 3 Of my articles were featured in www.asp.net as the community articles of the day.

    Here are the list of top 5 Blog posts that I did this year -

    1. ASP.NET MVC2 Client Validation
    2. Using ASP.NET Charting in ASP.NET MVC 
    3. Sorting Tabular Data in ASP.NET MVC
    4. Using TinyMCE Editor with ASP.NET MVC
    5. Using Elmah in your website 

     

    And here is a list of 3 of my articles that were featured in asp.net community articles.

    1. ASP.NET MVC2 Client Validation
    2. Using ASP.NET Charting in ASP.NET MVC 
    3. Sorting Tabular Data in ASP.NET MVC 
    Apart from these I have started a series of blog posts covering how to build a Windows Phone 7 App and how to use Telerik Extensions for ASP.NET MVC.

    All in all it was a good year for me.

    What's next in 2011

    In 2011,

    1. I would like to write at least 40 blog posts.
    2. I would like to start building for Windows Phone 7 more.
    3. I would like to contribute to one Open source project in some way.
    4. I would like to migrate my blog to Sitefinity 4.0(given the difficult migration experience I had till now, I am not entirely sure how this one is going to go)
    5. Also I would like to re-skin my blog to a more lively color templates.

    Let us see how far I will get in my new year goals.

    That's it for this year,

    Have a great holiday everyone.

    Cheers

    Venkata


    Go comment!
  • Telerik Extensions for ASP.NET MVC - The Grid

    by Venkata Koppaka | Dec 19, 2010

    This is second in the series posts I am doing to cover Telerik Extensions for ASP.NET MVC.

    1. Getting Started with Telerik Extensions for ASP.NET MVC 

    In today's post I will talk about using Telerik Extensions for ASP.NET - The Grid.

    I will start with a ASP.NET MVC2 application which is already configured to run Telerik Extensions, if you have not already done that, I encourage you to read my first post on getting started(link above).

    Add a Controller and create a method to return IQueryable<YourObject>. For sample code you can use something like this -

        public class AccessLevelController : Controller 
        { 
            private readonly AccessLevelRepository repository = new AccessLevelRepository(); 
     
            public ActionResult Index() 
            { 
                IQueryable<AccessLevel> accessLevelList = repository.GetAll().AsQueryable(); 
                return View(accessLevelList); 
            } 
        } 

    Although it is not mandatory to return a IQueryable<>, Telerik's Grid's Linq Engine will take advantage of IQueryable<> to do sorting, filtering, grouping etc, the engine will fire the queries with the exact filter, sort clauses rather than bringing all the data and trying to filter,sort afterwards. This is a huge performance gain in most of the scenarios.

    Now add a View for the Action Method Index, Strongly type the View to AccessLevel (object I am getting the data for) and leave the view content Empty(we will define our List content using Telerik Grid rather than using the default ASP.NET MVC's List Content)

    Once you add the View change the Model of the view to be IQueryable<AccessLevel> which is what we returned from the Action Method.

    This is how your View should like after changing the Model

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IQueryable<DomainModel.Database.AccessLevel>>" %> 
     
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
        Index 
    </asp:Content> 
     
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
     
        <h2>Access Level List</h2> 
     
    </asp:Content> 

    Now, let us add the Telerik Grid.

    Use the below snippet to add Grid -

    <% Html.Telerik().Grid(Model) 
               .Name("AccessLevelList") 
               .Columns(c =>  
               {  
                   c.Bound(o=>o.Access_Level_Id).Title("Access Level Id"); 
                   c.Bound(o => o.Access_Level_Role).Title("Role Name"); 
               }) 
               .PrefixUrlParameters(false) 
               .Sortable() 
               .Pageable() 
               .Filterable() 
               .Groupable() 
               .Render(); 
                %> 

    To test that we did everything correctly let us run the application and see if we see Telerik's Grid.(I will go over each property below).

    If you see a grid which looks like the above image, Great! we are done configuring the Grid.

    Now let us go over what each property in the above HTML means -

    DataBinding using (Grid(Model))

    There are few ways to bind the data to grid, the most easiest and convenient way being to pass theModel to  the grid directly. This model refers to IQueryable<AccessLevel>  which we made the Model of the View. Other ways to bind data are passing data using ViewData collection.

    Name

    The property "Name" is the actual clean HTML id of the div which wraps the table(grid translates to HTML table) that will be used for this list of data. After you run the application do a View Source from your browser, you will see a div with the Id you mentioned in this Name property.

    Columns

    Columns property defines the columns that will be on the grid, it allows the Lambda Syntax to define properties to each column. In the example we are making use of "Title" property for each column to define what the header text of the column will be.

    PrefixUrlParameters

    The PrefixUrlParameters property will tell the Grid whether to prefix the grid name in URL while doing operations like Sorting and Filtering. For example if you run the application with PrefixUrlParameters(true) and sort on Role Name column this is how the URL would change to -

    See how AccessLevelList is added to the URL. This is useful if we have multiple Grid's on the same page, since for the sake of this demo I am using only one grid on the page, I will set PrefixUrlParameters(false) and sort on Role Name column to see how the URL would change to -

    See how AccesLevelList is no longer added in the URL.

    Sortable

    Sortable property makes the grid Sortable, however this property can be turned off on a column by column basis using the Columns property for example if you wish to turn off sorting for AccessLevelId this is how you would do it.

    .Columns(c =>  
               {  
                   c.Bound(o=>o.Access_Level_Id).Title("Access Level Id").Sortable(false); 
                   c.Bound(o => o.Access_Level_Role).Title("Role Name"); 
               }) 

    Pageable

    Pageable property allowing paging for the grid.

    Filterable

    Filterable property allows filtering in the grid, however this property can be turned off on a column by column basis using the Columns property for example if you wish to turn off filtering for AccessLevelId this is how you would do it.

    .Columns(c =>  
               {  
                   c.Bound(o=>o.Access_Level_Id).Title("Access Level Id").Filterable(false); 
                   c.Bound(o => o.Access_Level_Role).Title("Role Name"); 
               }) 

    Groupable

    Groupable property allows grouping of columns in the grid, however this property can be turned off on a column by column basis using the Columns property for example if you wish to turn off Grouping for AccessLevelId this is how you would do it.

    .Columns(c =>  
               {  
                   c.Bound(o=>o.Access_Level_Id).Title("Access Level Id").Groupable(false); 
                   c.Bound(o => o.Access_Level_Role).Title("Role Name"); 
               }) 

    Render

    This property should be called to render any Telerik Extensions control.

    That's it for this post, in the next post I will be talking about how to use the Editor control.

    Hope you found this useful.

    Cheers,

    Venkata

    Go comment!
  • Getting Started with Telerik Extensions for ASP.NET MVC

    by Venkata Koppaka | Dec 12, 2010

    Telerik provides UI extensions to ASP.NET MVC, In this three part series I will  show how to get started with Telerik Extensions for ASP.NET MVC and how to use the Grid and Editor Controls. In this post I will walk through how to get started with these extensions.

    To use Telerik Extensions for ASP.NET MVC go to Telerik Extensions for ASP.NET MVC Page, and download the extensions, you will download a .ZIP, extract them and we are ready to add them to an application.

    With the download you will get Binaries, Content, Scripts and some examples. We will need Binaries, Content and Scripts to start using the Extensions.

    Create a new ASP.NET MVC 2.0 application. Open your Web.config file and look for "Pages" section of your web.config and under "namespaces" add a "Telerik.Web.Mvc.UI" namespace.

    <add namespace ="Telerik.Web.Mvc.UI"/> 

    Then add a reference to "Telerik.Web.Mvc", this DLL can be found under "Binaries" folder of your download.

    Then go to "Scripts" folder of your download, typically you will find a Year, Month, Version folder at the time of this writing it is "2010.3.1110". Inside that folder you will find javascript files for various Telerik UI extensions.

     

    Pick whatever controls you need and copy them to scripts folder of your ASP.NET MVC application. Please copy the folder the extensions where ("2010.3.1110" in this case) these Javascript files are found.

    Once you copy the scripts, the scripts folder in your ASP.NET MVC application should look similar to (of course you could have copied more javascript files)  -

    At the minimum you would need jquery min file and telerik.common.min.js file.

    Then you need to copy the Content(Styles and images) for these controls.These are found under "Content" folder of your download. For this example I am going to use "Hay" theme.Copy the "Hay" folder and telerik.common.min.css and telerik.hay.min.css files to Content folder of your ASP.NET application, once you have done that your Content folder should look similar to -

    Now, open your Site.master file and add link references to these CSS files -

        <link href="/Content/Site.css" rel="stylesheet" type="text/css" /> 
        <link href="/Content/telerik.common.min.css" rel="stylesheet" type="text/css" /> 
        <link href="/Content/telerik.hay.min.css" rel="stylesheet" type="text/css" /> 

    Now towards the bottom of the page, register the Telerik Script Registrar, using this line -

    <% Html.Telerik().ScriptRegistrar().Render(); %> 

    That's it you are now ready use Telerik UI Extensions. In the next post I will show you how to add "Grid" Control to your View page and how to do various operations with it.

    Hope this helped,

    Cheers,

    Venkata

    Go comment!