Posts

Showing posts from April, 2012

Generic CheckBoxList in ASP.Net MVC

In ASP.Net  web forms you can easily create a list of check boxes and bind it into a data collection using CheckBoxList control and specify which property will be the text of each check box and which one will be the value of the check box. You even don't need to write it, you can drag it from tools window, drop it into your page and set its properties and style with wizards that Visual studio provide. But in ASP.Net MVC there is no any server controls like that exists in web forms, all what we have is a helper methods to create HTML tags. In  ASP.Net MVC we have two helpers methods for creating a check box, Html.CheckBox and Html.CheckBoxFor that will be rendered as just one check box. If we want to create many check boxes or check boxes list we can call it inside a loop as follows @foreach (var item in MyCollection) { @Html.CheckBox("checkBoxName", new {value=item.checkBoxValue })@item.CheckBoxText<br /> } Here is a generic extension method for creat

Generic Lambda Expression to sort collections of different types

Suppose you have many collections with different types that you want to sort, of course you can do it by OrderBy or OrderByDescending extension methods but you have to specify the order criteria every time you call these methods. If the order criteria is determined in runtime, then you have to build a separate function to each order criteria of each type. To clear what I want to tell you suppose you have two classes,  Employee and Department . public class Employee { public String Name { get; set; } public Int32 Age { get; set; } public DateTime HiringDate { get; set; } } public class Department { public String DepartmentName { get; set; } public String Code { get; set; } } And you want to order the employee collection by one of its properties (Name,Age,or HiringDate) and order department collection by one of its properties. OrderBy and OrderByDescending methods need Func<TSource,TKey> as selector to compare the collection items and order them. So

How to fire RowCommand event of nested GridView?

Image
In ASP.Net you can use a Repeater or GridView controls to simply display any data collection came from any data source(database,XML file,...etc). Sometimes each item of that collection has a property that is another collection and to display it you will use another Repeater or another GridView(nested Repeater or nested GridView). To edit, delete or to execute a command on any GridView row you use OnRowCommand event to handle it, but what about the commands on the child(nested) GridView rows. In case of nested Repeater you can set the OnItemCommand event of the child Repeater. You can find many articles explain how to do that. The problem which my friend faced is handling the OnRowCommand event of nested GridView. We googled it but we didn't find a lot of article that explain how to handle it, so I write this post to help any one facing the same issue. Assume we have a set of Inventories, each Inventory have a set of products. To display All inventories with all products of