ASP.NET 4.0 – Persisting Row Selection in Data Controls

Problem Statement
In older versions of ASP.NET, When we select any row in a GridView or ListView controls, the selection is based on the row index of the page. For example, If we select row no 3 on page 1 and then move to page 2, we will see row no 3 again selected on page 2. which is undesirable.

Solution
ASP.NET 4.0 introduces the feature of Persisted Selection. When this feature is enabled, the selected item is based on the row datakey and not row index. For example, in a Grid showing products, i can specify that DataKey for my products is “ISBN”. Hence the rows will now be selected on the basis of ISBN(i.e. Datakey). Which further means that if i select row no 3 in products grid on page 1 and then move to page 2, no row will be selected. But if i again go back to page 1, row 3 will still be selected.

Feature can be enabled/disabled by setting the property EnablePersistedSelection. Also the property DataKeyNames be specified so that DataKey is set for the rows.

References: MSDN

kick it on DotNetKicks.com

Shout it

Introduction to MVVM pattern in WPF

The Model View ViewModel (MVVM) is an architectural pattern used in software engineering that originated from Microsoft as a specialization of the Presentation Model design pattern introduced by Martin Fowler. Largely based on the Model-view-controller pattern (MVC), MVVM is targeted at modern UI development platforms (Windows Presentation Foundation and Silverlight) in which there is a UX developer who has different requirements than a more “traditional” developer (i.e. oriented toward business logic and back end development).

Model
The Model is defined as in MVC; it is the data or business logic, completely UI independent, that stores the state and does the processing of the problem domain. The Model is written in code or is represented by pure data encoded in relational tables or XML.

View
A View is defined in XAML and should not have any logic in the code-behind. It binds to the view-model by only using data binding.
In simple examples, the View is data bound directly to the Model. Parts of the Model are simply displayed in the view by one-way data binding. Other parts of the model can be edited by directly binding controls two-way to the data.

ViewModel
The term means “Model of a View”, and can be thought of as abstraction of the view, but it also provides a specialization of the Model that the View can use for data-binding. In this latter role the ViewModel contains data-transformers that convert Model types into View types, and it contains Commands the View can use to interact with the Model.
The ViewModel exposes public properties, commands, and abstractions. The ViewModel has been likened to a conceptual state of the data as opposed to the real state of the data in the Model.

References
Wikepedia
Tales from the Smart Client
Model-View-ViewModel Pattern

kick it on DotNetKicks.com

Shout it

pimp it

Nullable Type in C#

Nullable type in C#
We can declare a variable as nullable when we want to know that a value has been assigned to that variable or not. Declaring a variable as nullable enables the HasValue and Value members. We can use the HasValue property on the variable to check if the value has been assigned to the variable.

Example of Using Nullable
Let’s take an example of boolean variable. As we know the default value for a boolean variable is false. But we want that we should be able to know if user has selected true or false. So we define the bool variable as nullable like this:

Nullable<bool> testVariable= null;

Now we have the testVariable as a boolean variable having a value of null. Now when use the properties like this :

if(testVariable.HasValue)
MessageBox.Show(String.Format("Value is {0}",testVariable.value )); 

kick it on DotNetKicks.com

Shout it

pimp it