Thursday, July 9, 2009
Custom Paging In DataList in ASP.Net
Background
Few days back when I required to implement custom paging in DataList(like a google paging). I search out the Internet but did not find any perfect code. Then after some effort I wrote my own code. I decided to share it with other users. Surely this will help other developers.
For creating the custom paging in the DataList control we need two DataList control, one for displaying the Data from the Database to the DataList and other for handling the Custom Paging.
After it we create three properties
1. CurrentPage
2. FirstPage
3. NextPage
The definition of the CurrentPage Property is
private int CurrentPage
{
get
{
object objPage = ViewState["_CurrentPage"];
int _CurrentPage = 0;
if (objPage == null)
{
_CurrentPage = 0;
}
else
{
_CurrentPage = (int)objPage;
}
return _CurrentPage;
}
set
{
ViewState["_CurrentPage"] = value;
}
}
The definition for the FirstPage property is
private int FirstPage
{
get
{
int _FirstIndex = 0;
if (ViewState["_FirstIndex"] == null)
{
_FirstIndex = 0;
}
else
{
_FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
}
return _FirstIndex;
}
set
{
ViewState["_FirstIndex"] = value;
}
}
The definition for the LastPage property is
private int LastPage
{
get
{
int _LastIndex = 0;
if (ViewState["_LastIndex"] == null)
{
_LastIndex = 0;
}
else
{
_LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
}
return _LastIndex;
}
set
{
ViewState["_LastIndex"] = value;
}
}
Note:- We need two Member function
Declare it before writing the function, just below the Partial class
PagedDataSource _pageData = new PagedDataSource();
1. BindBlogDataList()
2. handlePaging()
Definition for BindBlogDataList()
private void BindBlogDataList()
{
DataTable dt = _objBlog.BlogContent.GetBlogDetails();
//dataListBlogDetails.DataSource = _objBlog.BlogContent.GetBlogDetails();
//dataListBlogDetails.DataBind();
_pageData.DataSource = dt.DefaultView;
_pageData.AllowPaging = true;
_pageData.PageSize = 10;
_pageData.CurrentPageIndex = CurrentPage;
ViewState["TotalPages"] = _pageData.PageCount;
this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _pageData.PageCount;
this.lbtnPrevious.Enabled = !_pageData.IsFirstPage;
this.lbtnNext.Enabled = !_pageData.IsLastPage;
this.lbtnFirst.Enabled = !_pageData.IsFirstPage;
this.lbtnLast.Enabled = !_pageData.IsLastPage;
ddlBlogDetails.DataSource = _pageData;
ddlBlogDetails.DataBind();
this. handlePaging();
}
Definition for handlePaging()
private void handlePaging ()
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
FirstPage = CurrentPage - 5;
if (CurrentPage > 5)
{
LastPage = CurrentPage + 5;
}
else
{
LastPage = 10;
}
if (LastPage > Convert.ToInt32(ViewState["TotalPages"]))
{
LastPage = Convert.ToInt32(ViewState["TotalPages"]);
FirstPage = LastPage - 10;
}
if (FirstPage < 0)
{
FirstPage = 0;
}
for (int i = FirstPage; i < LastPage; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
this. ddlPaging.DataSource = dt;
this. ddlPaging.DataBind();
}
Details of Default.aspx.cs file
protected void ddlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("Paging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
this.BindBlogDataList();
}
}
protected void ddlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Style.Add("fone-size", "14px");
lnkbtnPage.Font.Bold = true;
}
}
//Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindBlogDataList();
}
}
//First Link Button Click
protected void lbtnFirst_Click(object sender, EventArgs e)
{
CurrentPage = 0;
BindBlogDataList();
}
//Previous Link Button Click
protected void lbtnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
BindBlogDataList();
}
//Next Link Button Click
protected void lbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
BindBlogDataList();
}
//Last Link Button Click
protected void lbtnLast_Click(object sender, EventArgs e)
{
CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
BindBlogDataList();
}
Monday, February 2, 2009
Style And Control Template In WPF
In WPF, a style is also a set of properties applied to content used for visual rendering. A style can be used to set properties on an existing visual element, such as setting the font weight of a Button control, setting of the background color of the TextBox control or it can be used to define the way an object looks, such as showing the name and age from a Person object. In addition to the features in word processing styles, WPF styles have specific features for building applications, including the ability to associate different visual effects based on user events, provide entirely new looks for existing controls, and even designate rendering behavior for non-visual objects. All of these features come without the need to build a custom control.
In other words the function of the Style is very similar to the .CSS in web application programming.
These are the following different types of Style in WPF
- Inline Style
- Named Style
- Element typed Style
- Data Template and Style
- Control Template
As an example of how styles can make themselves useful in WPF, let’s take a simple implementation of Button control.
WPF Inline Style
Each and every "style-able" element in WPF has a Style property, which can be set inline using standard XAML property-element syntax. For setting the Style property of an element we use the Style and Setter. Trough Setter tag we set the value of the element properties .
Let's take an example of the Button

Because we want to bundle three property values into our style, we have a Style element with three Setter sub-elements, one for each property we want to set—i.e., FontSize , FontWeight and Background—those with the Button prefix to indicate the class that contains the property. Properties suitable for styling are dependency properties.
Limitation With Inline Style
Inline styles can’t be shared across elements, inline styles actually involve more typing than just setting the properties. For this reason, inline styles aren’t used nearly as often as named styles.
Named Style in WPF
By publishing the same inline style into a resource, and giving a name, then we can use it by its name in our element instance.
Note:- To use the Named Style we use the StaticResource element of the style attribute
Friday, January 30, 2009
ANTS Profiler :- For Performance profiling and Memory Profiling.
ANTS Profiler comes in two edition:-
1. Standards
2. Pro
You can download the trial version of the ANTS profiler for only 14 days.
ANTS Profiler is a code profiling tool designed for .NET developers who want to get to the bottom of these and other related issues. Technical testers also use ANTS Profiler to pin bugs down to a specific line of code or to determine scope for optimization.
ANTS Profiler allows you to identify slow lines of code in .NET applications within 5 minutes of installation, allowing you to get on with what you do best – writing code. The program can also be used to measure line-level timings and method execution times, and to understand how your application performs.
ANTS Prifiler profile the following application:-
1. Windows Application
2. ASP.Net Web Application
3. .Net Web Services
4. COM + Application
5. Other application written in any other languages that supports the .Net Framework
You can use ANTS Profiler with the following versions of the .NET Framework:
1.1 (32-bit applications only)
2.0 (32-bit or 64-bit applications)
3.0/3.5. (32-bit or 64-bit applications)
ANTS Profiler is built with .NET 2.0, you will need to install .NET 2.0 in order to be able to use the tool.
ANTS Profiler offers the following features
- Profile any .NET language.
- Profile ASP.NET web applications.
- Profile Windows Forms applications.
- Profile .NET 3.0 and .NET 3.5 applications:- WPF, WCF, WF, XBAP
- Profile web applications on IIS 5, IIS 5.1, IIS 6, IIS 7, and ASP.NET
- Support for 64-bit profiling on x64 processors.
- Integration with Visual Studio 2005 and 2008
- Call tree which auto-expands the worst performing stack traces.
Besides the above mentioned features, it also support some additional fearures
- Method-level profiling (faster mode) which introduces less overhead than when the profiler also records line-level timings.
- Profile memory to understand how your application uses memory, and to locate memory leaks. The memory profiler allows you to take snapshots at any point in the execution of your program, so you can see what memory is in use at that point. You can take multiple snapshots at different times while your application is running, so you can compare application memory states.
Tuesday, January 6, 2009
Display a list of all fonts installed on a computer using Windows Forms and WPF Application
This class returns an array containg all the installed font on your system. Lets see, the following example express how to use the InstalledFontCollection and display the list of fonts in the ListBox control.
The following steps are required to use InstalledFontCollection Class for displaying all the installed font of your computer in the ListBox control.
Step 1: Create a new WinForm application. Open VS 2005 or VS 2008 > File > New > Project > Choose either Visual Basic or Visual C# in the Project Types > Choose Windows Forms application in Templates> Give a Name to the project and click OK.
Step 2: Drag and drop a Listbox on the form. We will display the list of available fonts in the listbox.
Step 3: In the ‘Form1.cs’, add a reference to the System.Drawing.Text namespace. This namespace contains the ‘InstalledFontCollection’ class which contains the functionality to represent the installed fonts.
C#
using System.Drawing.Text;
Note:- If you r using VB.Net use the following lines of code for adding the System.Drawing.Text namespace
VB.NET
Imports System.Drawing.Text
Step 4: In the Form1_Load, we will create an instance of the ‘InstalledFontCollection’ class and then use the Families property to return an array of ‘FontFamily’ objects associated with the FontCollection class. The font family names are then added to the listbox on each loop.
C#
private void Form1_Load(object sender, EventArgs e)
{
InstalledFontCollection fonts = new InstalledFontCollection();
try
{
foreach (FontFamily font in fonts.Families)
{
listBox1.Items.Add(font.Name);
}
}
catch
{
}
}
Displaying all the installed font of your computer in WPF Application the following codes are useful.
The following XAML code is used for displaying all the installed font of your computer in the ComboBox control. For creating a WPF application choose the WPF templete from the new project dialog box. For creating a WPF application we need .Net 3.5 Framework and Visual Studio 2008.