ASP.NET DataGrid is one of the coolest controls (well its quite late now to remark, but I cannot stop from starting like this) that has been used by almost everyone who does coding in ASP.NET. Though there are other controls like Repeater, DataList, the popularity DataGrid has got among developers is enviable.
One of the primary concerns when using DataGrid web server control is the Paging functionality. In real world scenario you will always end up with huge data that cannot be showed once for all in a single page. The DataGrid needs to implement paging by which you show only limited number of records and provide navigation links for seeing the next set of records, previous records, etc., However, there are certain limitations in using the built-in paging functionality. They are
1. The Pager Style mode is limited to "Next Previous" or Numeric "1,2,.." etc., whereas in certain places we would like to have custom text which says 1-5 of 10 records, First page, Last Page in addition to the previous, next page etc.,
2. Everytime, the page link is clicked, the entire set of records is going to be retreived though only the actual number of items will be shown. Assume you have 10,000 records. Everytime the 10,000 records will be retrieved when you navigate between the pager links. This can be heavy over the network.
3. You need to bind the DataGrid everytime on the PageIndexChanged event.
While the above are certain performance overheads, still, for basic paging requirement, the built-in paging mechanism is more than enough. However, if you care for performance and want to implement your own way of efficient paging, DataGrid still offers you the way to do it. It welcomes you to implement custom paging for which you have to write the code. In built-in paging, it does the plumbing work internally whereas, in custom paging, you need to dirty your hands in code.
There are several ways in which you can implement custom paging. There have been wealth of articles on the same ever since ASP.NET 1.0 got released and I am sure there must be some articles already on the topic I am writing now. But this is just another approach to help people who want to implement custom paging and expand the possibilities with DataGrid.
Having said that, lets start our first step in implementing Custom Paging.
1. Declare the DataGrid with AllowPaging="true"
<ASP:DATAGRID id="DataGrid1" Runat="server" AutoGenerateColumns="true" PageSize="5" AllowPaging="true" PagerStyle-Visible="False">
If you notice in the above declaration, we have set AllowPaging="true" which is normal in any case where you implement paging. Secondly, we have set the PagerStyle-Visible="false" to ensure that DataGrid doesnt show up its default pager style "< >" since we have set the allowpaging to true.
2. The next step is declaring a Panel which contains the Pager links
<asp:Panel id="Panel1" Runat="server" Visible="False">
<TD id="LinkButtons" colSpan="2" runat="server">
<ASP:LINKBUTTON id="FirstPage" Runat="server" text="First" OnCommand="NavigationLink_Click" CommandName="First"></ASP:LINKBUTTON>
<ASP:LINKBUTTON id="PreviousPage" Runat="server" text="Previous" OnCommand="NavigationLink_Click" CommandName="Prev"></ASP:LINKBUTTON>
<ASP:LABEL id="lblPageDetails" runat="Server" ></ASP:LABEL>
<ASP:LINKBUTTON id="NextPage" Runat="server" text="Next" OnCommand="NavigationLink_Click" CommandName="Next"></ASP:LINKBUTTON>
<ASP:LINKBUTTON id="LastPage" Runat="server" text="Last" OnCommand="NavigationLink_Click" CommandName="Last"></ASP:LINKBUTTON>
In the above code you can notice that we have 4 linkbuttons one each for "First, Last, Previous, Next". All of them link to a single method "NavigationLink_Click", but pass different CommandNames such as "First", "Previous", "Next", "Last" to specify which button is clicked.
Also, we have a panel covering these link buttons. We will explore on how to implement the NavigationLink_Click method in the next part of this article.
Cheers and Happy Programming !!!
One of the most ignored or rather not talked about control in ASP.NET 2.0 (no more code name "whidbey") is the MultiView Control.
How many times we have wanted a scenario where we need to show only certain portions of a section and show the other based on a click event and allow users to toggle between the two. The solution for this in ASP.NET 1.X versions is to either go for a Tabstrip control or have a Button, Image Button etc., and have Panels which you can show and Hide.
Neither of the two were very efficient due to the following reasons.
It wasnt a 100% solution for Tabbed Pages. It was a wonderful Web Control but still had its own limitations.
Validators cannot exist happily across tabs where submission of neither of them will happen unless all the validators are satisfied.
You have to explicitly declare the Controls in the Code Behind unlike the traditional way, where you drag and drop a control and automatically, it adds the control declaration in the codebehind.
Microsoft does not support the IE Webcontrols and therefore there is no proper documentation / support for them and nor there is any reliability of performance.
You have to explicitly show a Panel and hide the other panels. Say you have 3 panels.
Then, you need to set Panel1.Visible = true and others Panel2.visible = false & Panel3.visible = false explicitly. You have to repeat these steps everytime you want to change between the panels.
Maintaining the Code becomes too tough if more panels get added.
ASP.NET 2.0 Provies a wonderful solution by means of MultiView Control. The MultiView Control combined with the View Control can provide different UI on different scenarios as and when required.
Herebelow, is the syntax for the same
<asp:MultiView ID="MultiView1" runat="server" >
<asp:View ID="View1" runat="server">
This is one section where you can have a set of controls / contents.
<asp:View ID="View2" runat="server">
This is another section where you can have a different set of controls / contents.
Now, to show the different tabs based on different events, we just need to set the following code:-
protected void Button1_Click(object sender, EventArgs e)
protected void Button2_Click(object sender, EventArgs e)
Thats all I need to show the different parts of the UI based on scenario. This is really useful in cases of Lengthy DataForms, collecting different information from different control types based on user's input.
The MultiView control for sure is going to change the way we have been coding heavily using Panels.
Cheers and Happy Programming !!!
Somasegar, Corp. Vice President, Developer Division, Microsoft, keenly listening to one of our MVPs' query
Myself, Sudhakar (Microsoft) and Anjana (SQL Server MVP)
Keenly into a Serious Discussion
My Fellow MVP 'Anil', Community Member Sasi, Arun - XML WebServices MVP, and Community Member Venkata Murthy
Thanks to Rayappa, MVP for sharing the snaps !!!