Sunday, December 21, 2008

How to implement paging in Silver Light2 DataGrid

Introduction

Silver light2 is one of the most new and exciting technology from Microsoft. As my company has decided to develop application by using Silver Ligth2 that’s why I am exploring the silver light2 controls. When I started to work with Silver Light2 Datagrid control, I saw there is no built-in paging facility. The paging facility is extremely necessary to our application. Silver light2 is still Beta1. So I am expecting these sorts of features will be added to the release version. Nevertheless I tried to develop a user control which shows you how paging can be implemented in DataGrid.

Using the code

In order to implement paging in DataGrid, I used RowDetailsTemplate property of the DataGrid. This RowDetailsTemplate gets or sets the template that is used to display the content of the details section of rows. This property is visible at the last row of the DataGrid where I put the navigation buttons. To visible the RowDetailsTemplate property for the last row, DataGrid subscribes the PreparingRow event.

private void grdPaging_PreparingRow(object sender, DataGridRowEventArgs e)
{
//_countrow is the current preparing row number, totalRowsInGrid is the total row in
// grid.
//totalRowsInGrid can be equal to or less then chunk size

if (_countrow == totalRowsInGrid)
{
e.Row.DetailsVisibility = Visibility.Visible;
}
_countrow++;
}

To put the navigation buttons, DataGrid subscribes the PreparingRowDetails event. Here I used a stackpanel to house all the navigation buttons.

private void grdPaging_PreparingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
//Get the stackPanel from the RowDetailsTemplate
StackPanel stk = e.DetailsElement.FindName("stk") as StackPanel;
if (stk != null)
{
stk.Children.Clear();
int totalpage = Convert.ToInt32(Math.Ceiling(this.TotalRow/(double) this.ChunkSize));
for (int i = _currentPage; i <= totalpage; i++)
{
Button btn = new Button();
btn.Width = 20;
btn.Content = i;
btn.Click += new RoutedEventHandler(btn_Click);
btn.Margin = new Thickness(3);
stk.Children.Add(btn);
}
}
}
As you will see in the source code, the rest of the statements are very typical to implement paging in DataGrid.

Monday, February 11, 2008

Some Differences Between ASP.Net Controls

Some Differences Between ASP.Net Controls

In order to increase the productivity of development in ASP.NET, there are many built-in controls in ASP.Net. If you use those controls efficiently, the performance of your web application can be increased. Using controls efficiently means use the control where it should be used.

There are many controls in ASP.Net which look like very similar. Sometimes it is very difficult to distinguish those controls. In the following section, I try make some differences between ASP.Net controls which look like very similar.

Difference between Literal control and Label Control:
Label and Literal controls both are server controls and both are used to show text. Literal has some limitations compare to Label. You can’t use style on literal control and you can’t get ClientID of Literal control. However, you can set style and get ClientID of label control.

Difference between Literal control and Localized Control:
There is no sharp difference between literal control and Localized control. Localized control is driven from Literal Control. One difference what I get between these controls is that Localized control can be edited in design view where as you can’t edit literal control on design view.

Difference between PlaceHolder Control and Panel Control:
Both the Placeholder and Panel controls work as a container. Panel uses Div to contain its child controls but PlaceHolder reserves place to render its child controls without any parent control. So if you want to use CSS on all the child controls, you should use Panel.

Difference between FormView and DetailView:
DetailsView renders a single tuple vertically instead of horizontally. It converts columns into rows. So you can see a single tuple on every page. Formsview is like Detailsview with the exception that it shows data with the help of template. In order to show data on FormViews, you have to use template. Gridview with single template column works as a FormView.

Difference between Listview and gridView:
Listview is a very new control which is come with ASP.Net3.5. It shows data as an OL or UL. Listview gives the developer high flexibility to render its content. You have the full control on HTML that how you want to show data on your page. In gridview, we can use template to get the control over HTML. But Listview lets you more facility over gridview. You can’t use paging on listview. In order to use paging on listview, you have to use datapager control. But gridview has a very good facility on paging.

Hope this help!