Wednesday, August 29, 2007

OutputCache! How did I miss it?

I've accidentally stumbled upon a thing called OutputCache. Weird little thing, I thought, how come I didn't hear about it? It seemed such a wonderful concept: just cache the output of ASP.Net pages and controls for a specific amount of time, specify for what parameters or controls or properties the cache is valid for and if it is shared between different users. Then just output the cached version and don't execute the entire thing all the time.

I tested it and it works. Most useful I find it for user controls. One can put a list of unchanging stuff like a list of cities or provinces or even customers in a user control, cache its output and forget about it.

How to use it:
A) declaratively. Put in the page or control something like this:
<%@ OutputCache Duration=100000 Shared="true" VaryByParam="none" %>
Just read the link above for the details.

B) programmatically. Put in the page or control code something like this:
CachePolicy.Cached = true;
CachePolicy.Duration = new TimeSpan(1, 0, 0, 0, 0);
CachePolicy.VaryByParams.IgnoreParams = true;

Update: it has come to my attention that this returns an error with the message "The CachePolicy cannot be used, since the control is not being cached." which is returned when the CachePolicy was not instantiated with a BasePartialCachingControl as a parameter. The solution, it seems, it is to also decorate the user control with the PartialCaching attribute.

ex: [PartialCaching(20)] - set the caching to 20 seconds.

C) One can even validate the cache programmatically after whatever of A or B methods were used like this:
Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(MyCacheValidatorMethod),null );

That's it!

Debugging ASP.NET by Jonathan Goodyear, Brian Peek and Brad Fox

This book is written by a guy that made a small company dedicated to programming with free tools. Thus, it tries to show how to debug ASP.Net assuming that you come from an ASP background and that you don't have Visual Studio .Net. So I don't think I have to tell you how much useless information there is in there. It even covers programmatic code tracing.

That doesn't mean it is a bad book, just useless for me. If you expect some wonderful debugging frameworks or code structure that would allow you to easily debug your applications, then this book is not for you. Not to mention that at the time it was written, Visual Studio 2005 was not out yet.

Bottom line: just a hands on approach and some Google when you meet problems will solve any problems that are solved by this book.

The Abbey by Dan Dobos

Dan Dobos is a Romanian writer and so I find it difficult to bad mouth his work. He also had bad luck with this one, as I'd just found one of the better writers of sci-fi and read a few of his books before trying to (re)read The Abbey.

Bottom line, The Abbey tries to be a Romanian Dune, with some Christian theology thrown about, a planet controlling the resources of the entire human dominion, people with heightened intelligence, instincts, subtle two way dialogs and verbal manipulations, etc. It fails. The characters are inconsistent, obvious, their best attempts at charade are ludicrous, their motivations are inedible, the whole human galactic evolution completely unbelievable. The writing style is also very shallow, made only of dialogue and inner thoughts, with little detail of what is actually going on.

Although I admire the attempt, the basic ideas and the fact that he got published in a country where people rarely buy books and if they do they don't buy Romanian authors, this series is a failure. Better luck next time.

Fallen Dragon by Peter F. Hamilton

Enough said, I started reading anything this guy wrote. I've found Fallen Dragon, a single book this time, but filled with ideas that obviously grew into the Commonwealth Saga. With this earlier novel I did find Hamilton's Achilles heal, the one thing that bothered me in his books more than the slight over detailing of everything: the characters have no freedom.

Let me expand on that. When you write something, you have the characters in your head, fighting and shouting to let them free to do what they want. If you do that before they are fully matured, they break havoc with your writing, even considering you have the skill. However, when you force them to do your bidding like slaves, you also damage your story, because they lose credibility and consistency.

In this book more than others, the characters were forced to do something that they should never have done. A soldier looses his squad to a bunch of idealistic galactic eco terrorists, but eventually joins their ranks. That is just not possible. I was wriggling in bed, waiting for the moment the hero would remove his mask and kill that brain damaged bitch. He did no such thing, just letting his mates die for no reason. Thinking back, the same happened with Judas Unchained, when all characters willingly marched against an alien that was clearly lacking any resources and just trying to escape in a wild wild west kind of quest "let's all personally go and kill the bad guy".

Back to Fallen Dragon, it is an interesting story, with some moral teachings as well as a delicate temporal paradox, but it's not comparable with Hamilton's latest books.

The Dreaming Void by Peter F. Hamilton

After finishing the Commonwealth Saga, by the same author, I started reading the next series: The Void Trilogy. As in the aforementioned saga, the story does not end in any way at the end of the first book, you have to read all three books to reach the end, which effectively makes this another huge monolithic story, not a series or saga.

Set in an even more technological advanced future, but in the same Commonwealth universe, it depicts the interactions between a powerful pseudo-religious void worshipers, ANA (a quantum artificial intelligence where people download when the are tired of living and the next evolutionary phase of the SI) and its competing factions, the different alien races and the void, which is a dark impregnable sphere in the middle of the galaxy that only some psychic humans seem to penetrate.

The style is the same, the writing as almost flawless as before. I do think that P.F. Hamilton is one of the greatest sci-fi writers I've read, up there with Tolkien and Herbert.

Stupeur et tremblements by Amélie Nothomb

I have seen the movie on TV a while ago, but didn't catch the start, so I never knew what the name was. Finally, someone in a Romanian book club found the translated print here and reminded me of it and told me that it was based on a book. I've decided to read it in its native language, a bit scared that I would understand nothing of it, but the French is pretty easy to understand. Maybe because the author is Belgian :)

The story of Fear and Trembling is about a Belgian girl, born in Japan (just like the author), employed in a big Japanese corporation as a translator. Due to her inability to understand the local customs and the irrational feelings of her employers, she is demoted time after time until she ends up replacing toilet paper in the water closets.

The book itself is not meant as a comedy. The main character sees everything with a stoic detachment, analysing both her feelings and the feelings of others, trying to make sense of a world that she can't seem to fit in. It made me understand more than ever the illusory nature of reality. Both her and the Japanese were occupying the same space and time, were observing the same events, but their realities were completely different. That clash of incongruous realities is the core of this small novel.

A bit too French for my taste, not in the language, but in the endless repetitions of metaphoric interpretations of the same event, that overwording of inner thoughts that makes French writing sound pompous. However, I did enjoy the book, I recommend it to anyone wanting a light and distracting reading.

Fruit of the Mall

I wanted it to sound like Fruit of the Loom, for the connoisseurs in gaming :), but failed miserably. This blog post is about the exotic (and shamelessly expensive) fruits one can found in market stores.

The first one is the Lychee fruit. It is small, has a rough skin that has to be removed and a big seed inside. It leaves little else, but the flesh is very tasty, with a taste like that of a grape, but less sour and stingy. No wonder it is called the Chinese grape. I recommend it, at least try it once.

The Mangosteen. Now that's a fruit. Big as a small apple, it has a similar hard skin to remove and a similar taste as the Lychee, with the added advantage that it has more flesh. I would venture to say that this is the most tasty fruit I'd ever tried, even if a little too sweet. There is a funny story here, too. When I was a child I used to keep newspaper bits of the "did you know?" column (back in the days when newspapers has a science and information section). One of them said that the Mango is the king of the fruits, the very best in taste, but hard to transport because it rots easily. I was disappointed when I first tasted mango. It wasn't even tasty. But apparently, the author of the article made a confusion between the Mango and the Mangosteen. 20 years later, the article is vindicated ;)

The Carambola. A yellow star shaped fruit that can be bitten into, has the taste of a Quince fruit and a bit of its texture, but a bit finer. I would prefer it to quince, even if I don't particularly enjoy apple/pear tasting fruits.

The Kiwano, or horned melon. This is an odd fruit. It can be cut in quarters and eaten like a melon, has a greenish yellow color and taste like a combination of cucumber and banana. It is an interesting experience :)

I have tried another fruit, one that looks like a plum shaped (but smaller) orange. And it is :) It is like an orange pill. If it weren't for the price, I would use it to make arranciata. The name is kumquat and it can be hybridised with lime to fruit limequats. There are a lot of citrus fruits in the world, not only the orange, lemon and grapefruit!

For other exotic (for me) fruits, check out Part 2.

Tuesday, August 28, 2007

The general area of Sibiu, Romania

I've been to a small village near Sibiu, home of my parents in law, called Sarata. First major trip outside Bucharest by car, we managed to reach Sibiu, Ocna Sibiului, Sighisoara, Basna, Balea Lac, Paltinis and to travel the entire area back and forth.

We have become intimately familiar with dust and washing the car, but it was almost worth it, if it wasn't for the permanent noise (yes, I went there for the silence) and I got people (and their atomic children) shouting, screaming (and that without actually fighting, simply communicating), phones ringing (and I thought it was bad they didn't have a phone) and roosters making all kind of noises (man, I hate that rooster!). On top of everything, I've spent around two nights fighting the excessive warmth and another two fighting an intense case of indigestion.

But you won't be interested in that :) Anyway, let me review some of the touristic impressions I had.

Firstly, Sibiu. Because of the "European capital of culture" thing, Sibiu is now transformed. Not that it's not a city like any other, but it is now completely crowded by cars, parking spaces, tourists, etc.

The same impression I got from Sighisoara, where I was expecting a medieval town with cute little taverns and interesting castles, yet I found a city in reconstruction, filled with cafes and "Authentic Souvenir" shops filled with things of incredibly bad taste.

Ocna Sibiului is a small tourist place with salty and iodised waters. I'd expected a place full of old people coming to treat their illnesses, but I did not expect the level of mismanagement of the place. Imagine a few holes in the ground filled with naturally salty water, but not cleaned, with a few old wooden stairs that looked ready to crumble at every step. We had to pay to enter, the prices inside were huge and in order to find a place to change your clothes you had to find a bush somewhere. I had fun in the water, as I could leisurely fall asleep in a water that seemed to easily sustain people of ... lesser gravitational pull as myself.

Basna seemed nice, but we didn't stay long. An expensive hotel is placed there, with pools and everything, and then there are the mountains right there, ready to be hiked.

Balea Lac, just a short trip, even nicer place than Basna, but really commercialized, souvenirs and stuff. The hotel being just a normal mountain hotel.

Paltinis is just like Balea, but villas and hotels are sprouting there like mushrooms.

Some very nice villages and locations are found by travelling between the major place of tourism. We've found a 702 years old evangelic church in Valea Viilor, a small village near Copsa Mica, with an old woman greeting us with Guten Tag, and then by Buna Ziua, even if we were in Romania. The wife loved the architecture and I loved the cool air. Also a nice place was in Cisnadioara.

What was a little off putting was that we were guiding ourselves by a map made in 2000 and after reaching Valea Viilor, for example, we tried taking a road on the map and a villager said "Oh, that road? It isn't functional for more than 20 years now". Also, getting out from a temperature of 19C in one of 30C was not "cool", either, so we preferred watching the beauty of nature from inside the car. Not having to smell the nature was a plus for me, as well.

Sorry for a lack of pictures, but I don't have a camera :) I will remedy this for sure, but it's not a priority right now.

Friday, August 17, 2007

Sonata Arctica - Broken

Am I turning metal? I used to hate songs played by long haired guys barking aimlessly on a heavy and repeating guitar riff. But I really enjoy listening to Sonata Arctica. Maybe because they're Finnish? :) I am developing a taste for Finnish music, apparently.

Anyway, here is a live performance of the song Broken, which I like best so far. I did look for an official video on this song, but drowned in home made crap videos. I did find official videos for other songs, but the band seemed even less charismatic in those, trying to seem really upset and managing to look like a shampoo commercial.

Another song of theirs I like is Replica.

Thursday, August 16, 2007

InvalidOperationException: A circular reference was detected while serializing an object of type ...

You are trying to use a WebMethod or a web service ScriptMethod in Javascript and you get an InvalidOperationException saying something about a circular reference. It happened to me when trying to read a DataTable in Javascript.

Why. The Javascript serialization of DataSets, DataTables and DataRows was available once in the ASP.Net Ajax web extensions. That's why you probably found a lot of Google results with people that could either only serialize DataSets, but not DataTables, or people that made it work by magic by adding some lines in the converters section of web.config, things that can't possibly work with your setup. Then, the option was removed in the final version of ASP.Net Ajax, only to be readded in the ASP.Net Futures, which is a test playground for future features of the platform.

What. There are several options, one being to reference an older version of ASP.Net Ajax and uses the converters there. But why bother? It's unlikely you use the DataTable or some other object in Javascript with all the options of the C# object. You probably just want to itterate through rows and read properties. So build your own converter.

How. Create a new library project. Add a class named DataTableConverter that inherits from JavaScriptConverter, and implement: IEnumerable<Type> SupportedTypes, IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) and object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer).

You probably won't need to Deserialize anything, you can leave that unimplemented. The list of convertible types is easy enough, all you are left with is the Serialize code, which is actually very easy, too. Then all you need to do is add this in the web.config file:
<jsonSerialization >
<add name="DataTableAjaxFix" type="AjaxTypeConverters.DataTableConverter"/>

And here is the complete C# code of my DataTableConverter, but you can easily adapt it to anything:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web.Script.Serialization;

namespace AjaxTypeConverters
    public class DataTableConverter : JavaScriptConverter
        public override IEnumerable<Type> SupportedTypes
            get { return new Type[] {typeof (DataTable)}; }

        public override object Deserialize(IDictionary<string, object> dictionary, Type type,JavaScriptSerializer serializer)
            throw new NotImplementedException();

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
            DataTable listType = obj as DataTable;

            if (listType != null)
                // Create the representation.
                Dictionary<string, object> result = new Dictionary<string, object>();
                ArrayList itemsList = new ArrayList();
                foreach (DataRow row in listType.Rows)
                    //Add each entry to the dictionary.
                    Dictionary<string, object> listDict = new Dictionary<string, object>();
                    foreach (DataColumn dc in listType.Columns)
                        listDict.Add(dc.ColumnName, row[dc]);
                result["Rows"] = itemsList;

                return result;
            return new Dictionary<string, object>();

Wednesday, August 15, 2007

ASP.Net. The connection to the server has been reset!

It would have been animated if the image upload at Blogger wasn't screwed! ~x(Yesterday I was trying desperately to understand why my web site was crashing without any error, the only information I could get being that the connection to the server has been reset. I've spent hours trying to determine what was wrong. Apparently I needed a break, because today it took me a few minutes to realize what it was.

First of all, duh! If there are issues with the connection server, look into the Windows Application Event Log. But we'll get there.

The "error" appeared at any postback after I loaded a certain page, but only if that page displayed a minimum of data. Above that threshold I would get the server reset thing that you can see both in IE7 and FireFox2 in the animated GIF. Basically the error messages were:
The connection was reset
The connection to the server was reset while the page was loading.

Internet Explorer
Internet Explorer cannot display the webpage
Internet connectivity has been lost.
The website is temporarily unavailable.
The Domain Name Server (DNS) is not reachable.

Ajax UpdatePanel
Server returned error 12031

So, today I realised I should look in the Application Event Log and this Web Event Warning was displayed (shortened it a bit):
Event code: 3004
Event message: Post size exceeded allowed limits.

Process information:
Process name: aspnet_wp.exe

Exception information:
Exception type: HttpException
Exception message: Maximum request length exceeded.

Stack trace: at System.Web.HttpRequest.GetEntireRawContent()
at System.Web.HttpRequest.FillInFormCollection()
at System.Web.HttpRequest.get_Form()
at System.Web.HttpRequest.get_HasForm()
at System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull)
at System.Web.UI.Page.DeterminePostBackMode()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

It turns out I was putting a lot of data into the ViewState, which, as you know, is saved as a HiddenField (a.k.a. hidden html input) and the size of it exceeded the set up maximum POST size.

A. Add this code to your page: (NET 2.0)
 protected override PageStatePersister PageStatePersister
//return base.PageStatePersister;
return new SessionPageStatePersister(this);

This should put your ViewState into the Session, rather than in the page. This solves some other issues as well, obviously.

B. Increase the maximum Request limit (default is 4Mb)
- In the Machine.config file, change the maxRequestLength attribute of the <httpRuntime> configuration section to a larger value. This change affects the whole computer.
- In the Web.config file, override the value of maxRequestLength for the application. For example, the following entry in Web.config allows files that are less than or equal to 8 megabytes (MB) to be uploaded:
<httpRuntime maxRequestLength="8192" />

This is an exact quote from the Microsoft support page.

That's it, folks!


The maxRequestLength maximum value is 2097151, that is less than 2.1Gb. No file that exceeds this size can be uploaded through the default upload mechanism.

Thursday, August 09, 2007

Visual Studio data visualizers or Writing your own debug data viewer

You may have noticed that in debug mode, in Visual Studio, you have a little magnifier glass next to some of the variables in Autos, Local or Watch debug windows. Once you click on it, you get to visualize your data in a more comprehensive way. A good example are the DataSet Visualizer or the DataTable Visualizer which show you in a normal DataGridView a DataSet or DataTable.

The good news is that you can build your own visualizers and that in a very simple way. Here are the quick steps to achieving this, followed by some links to other people detailing:

  1. Create a new Visual Studio class library project
  2. Add a reference to the Microsoft.VisualStudio.DebuggerVisualizers library you can find directly in the .NET tab
  3. Go to Add New Item and choose Debugger Visualizer. That will create a small class for you with ToDos and stuff like that. What is important is that you don't really need to declare the Type of your data object in the class, as suggested by the template.
  4. Remove everything from the class except the override of the Show method
  5. Change the Show method in order to use your own data type.
  6. Add a reference to System.Windows.Forms
  7. Add a Windows Form to your library and make it display your data the way you like it
  8. Add the following lines to decorate the namespace of your visualizer class:

[assembly : DebuggerVisualizer(typeof (--YourVisualizer--),
Target = typeof (--Your Type--),
Description = "--Your Type-- Visualizer")]
namespace ...

Warning: use a different description from whatever default or own visualizers that are already there. Use stuff like "Siderite's DataTable Visualizer" not "DataTable Visualizer", since there is already an out-of-the-box visualizer with the same name and you won't get to see yours.

Now compile. The resulting DLL can be either copied in My Documents\Visual Studio 2005\Visualizers in this case any contained visualizers will be available only to that user or in C:\Program Files\Microsoft Visual Studio 8\Common7\Packages\Debugger\Visualizers to make the available to all users.

That is it! Now links to others explaining in more detail:
Writing a Visualizer at MSDN
Post on Debugger Visualizers from 4GuysFromRolla
Julia Lerman on Debug Visualizers

A problem with this solution is that the debugger visualizer expects your target type to be ISerializable. But what if it is not? The solution is to add another parameter to the DebuggerVisualizerAttribute like this:
[assembly : DebuggerVisualizer(typeof (--YourVisualizer--),
typeof (--YourVisualizerObjectSource--),
Target = typeof (--Your Type--),
Description = "--Your Type-- Visualizer")]
namespace ...

You see, the debugging is done through communication between a debuggee and a debugger. The VisualizerObjectSource is the debugee and the default one tries to serialize the target object and send it to the debugger. What you have to do it create your own class, inheriting from VisualizerObjectSource and overriding public void GetData(object target, System.IO.Stream outgoingData). This method has access to the actual object, so you can transform it into any other object, one that can be serializable.

A simple example is a DataView or a DataRow. You take the DataRow, you add it to a Table and you return the DataTable, which is serializable.

Another issue you might stumble upon is a weird Access Denied error for the DLL containing the visualizers, especially after adding a VisualizerObjectSource to the library. The solution is to add trust level to full to the site you are debugging. I am looking for a more elegant solution, but so far what you have to do is add this to the web.config of the site you are debugging:

<trust level="Full" originUrl="" />

More links, specific to this update:
RemoteObjectSourceException: Graphics is not marked as serializable
Visualizers For Web Debugging

Tuesday, August 07, 2007

FindControl finds controls outside the calling Control

Yes, the situation is fairly simple, you do a parentControl.FindControl("someOtherControl") in ASP.Net and you get controls that are not in parentControl. Why?

Because apparently, FindControl wants a NamingContainer in which to search. If the parentControl is not an INamingContainer, it will look into the parentControl's NamingContainer.

Sollution? Create your own FindControl method, one that recursively goes through the child controls and looks for the specific ID.

Monday, August 06, 2007

Very interesting... stuff...

I've accidentally stumbled upon a very interesting video of a science conference about sleep. Called "Sleep, Waking and Arousal" it details a very interesting compendium of information about sleep in humans, mammals, other vertebrates and even fruit flies. I am posting the link here, but what is even more interesting is that it is part of something called the "The University of California Television" that has its own web site with a lot of (presumably) interesting videos.

Sleep, Waking and Arousal
The University of California Television

The Birthday Massacre - Blue

Here is a song that I found most interesting and I am referring here to the high pitched piano with the distort guitar in the background. The video isn't that exceptional, but watchable. I am trying to imagine a scene of emotional destruction in a movie that would have the particular instrumental part as a soundtrack.

If you want to see the live performance, click here.

Friday, August 03, 2007

Using the AjaxControlToolKit AutoCompleteExtender

Well, it's just as in the AjaxControlToolKit page for the AutoCompleteExtender control. But here is a quick dirty list of the steps:

Option A: (the static page method option)

  1. Add a TextBox to your page
  2. Add the AutoCompleteExtender
  3. Set the ScriptManager property EnablePageMethods to true
  4. Add a static method to the page, one that gets a string and an integer as parameters and returns a string array
  5. Decorate it with [WebMethod(true/false)]. If you set the WebMethod parameter to true, it will have access to the Session
  6. Make sure to return the list of strings depending on the string parameter (which represents what was typed in the TextBox)
  7. Warning! If the method is faulty, you will get no error message, the autocomplete will simply not work.
  8. Warning! Having a different method signature will also cause this to not work.
  9. Set the properties for the AutoCompleteExtender: TargetControlID with the ID of the TextBox, MinimumPrefixLength with the count of typed characters from which to attempt autocomplete, ServiceMethod with the name of the static page method and CompletionInterval with the miliseconds before it attempts autocomplete.

Option B: (the web service option)

  1. Add a TextBox to your page
  2. Add the AutoCompleteExtender
  3. Add a webservice to your web site
  4. The webservice must have [ScriptService] decorating it's class in the codebehind
  5. Add a NOT static method to the webservice, one that gets a string and an integer as parameters and returns a string array
  6. Decorate it with [WebMethod(true/false)] and [ScriptMethod]. If you set the WebMethod parameter to true, it will have access to the Session
  7. Make sure to return the list of strings depending on the string parameter (which represents what was typed in the TextBox)
  8. Warning! If the method is faulty, you will get no error message, the autocomplete will simply not work.
  9. Warning! Having a different method signature will also cause this to not work.
  10. Set the properties for the AutoCompleteExtender: TargetControlID with the ID of the TextBox, MinimumPrefixLength with the count of typed characters from which to attempt autocomplete, ServiceMethod with the name of the WebMethod in the webservice, CompletionInterval with the miliseconds before it attempts autocomplete and ServicePath to the path of the asmx path.

Now it should work.


//=== AutoComplete.cs - the web service ===
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AutoComplete : WebService

public string[] GetList(string prefixText, int count)
string[] arr=new string[] {'list','of','words'};
return arr;

//=== Web page codebehind
public static string[] GetList(string prefixText, int count)
string[] arr=new string[] {'list','of','words'};
return arr;

=== Web page ===

<asp:TextBox ID="textboxWithAutoComplete" runat="server">
<cc1:AutoCompleteExtender ID="autoCompleteExtender1" runat="server" TargetControlID="textboxWithAutoComplete"

WARNING! The parameters of the web method must be named prefixText and count or the AutoCompleteExtender will NOT WORK!

A small paragraph that most people miss on the AjaxControlToolKit sample page says: Note that you can replace "GetCompletionList" with a name of your choice, but the return type and parameter name and type must exactly match, including case.

Thursday, August 02, 2007

Changing GridView PageCount

Update (21Th of March 2008): Very important, you need to add the CreateChildControls override in order to work. Otherwise, because of something I can only consider a GridView bug, this will happen: the last page in a mock paging grid will have, let's say, 2 items when the PageSize is 10; on a postback, the number of rows created by the gridview will be 10! even if only 2 have data. Thus, after a postback that doesn't rebind the data in the grid, the GridView.Rows.Count will be PageSize, not the actual bound number.

Update: Recreated the code completely. Now it has both PageIndex and ItemCount.
Also: Actually there is a way to get only the rows that you need in SQL Server 2005. It is a function called Row_Number and that returns the index number of a row based on a certain ordering. Then you can easily filter by it to take items from 20 to 30, for example. In this case, another interesting property of the PagedDataSource is CurrentPageIndex, to set the displayed page number in the pager.

Now, for the actual blog entry.

Why would anyone want to change the PageCount, you ask? Well, assume you have a big big table, like hundreds of thousands of rows, and you want to page it. First you must put it in a DataTable from the SQL server, so that takes time, then the table set a datasource to the GridView, then it implements the paging.

Wouldn't it be nicer to only get the data that you need from the SQL Server, then change the PageCount to show the exact page count that should have been? However, the PageCount property of the GridView is read-only. One quick solution is to get only the data you need, then fill the resulting DataTable with empty rows until you get the real row count. However, adding empty rows to DataTables is excruciatingly slow, so you don't really gain anything, and the Grid works with a big table anyway.

So this is what you do:
First of all determine how much of the data to gather.

Afterwards you need to trick the GridView into creating a Pager that shows the real row count (and possibly page index). Unfortunately you can't do this from outside the GridView. You need to inherit the GridView control and add your stuff inside. After you do this, you need to override the InitializePager method, which is just about the only protected virtual thing related to Paging that you can find in the GridView.


using System.Web.UI.WebControls;

namespace Siderite.Web.WebControls
public class MockPagerGrid : GridView
private int? _mockItemCount;
private int? _mockPageIndex;

/// Set it to fool the pager item Count
public int MockItemCount
if (_mockItemCount == null)
if (ViewState["MockItemCount"] == null)
MockItemCount = Rows.Count;
MockItemCount = (int) ViewState["MockItemCount"];
return _mockItemCount.Value;
_mockItemCount = value;
ViewState["MockItemCount"] = value;

/// Set it to fool the pager page index
public int MockPageIndex
if (_mockPageIndex == null)
if (ViewState["MockPageIndex"] == null)
MockPageIndex = PageIndex;
MockPageIndex = (int) ViewState["MockPageIndex"];
return _mockPageIndex.Value;
_mockPageIndex = value;
ViewState["MockPageIndex"] = value;

///Initializes the pager row displayed when the paging feature is enabled.
///<param name="columnSpan">The number of columns the pager row should span. </param>
///<param name="row">A <see cref="T:System.Web.UI.WebControls.GridViewRow"></see> that represents the pager row to initialize. </param>
///<param name="pagedDataSource">A <see cref="T:System.Web.UI.WebControls.PagedDataSource"></see> that represents the data source. </param>
protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
if (pagedDataSource.IsPagingEnabled && (MockItemCount != pagedDataSource.VirtualCount))
pagedDataSource.AllowCustomPaging = true;
pagedDataSource.VirtualCount = MockItemCount;
pagedDataSource.CurrentPageIndex = MockPageIndex;
base.InitializePager(row, columnSpan, pagedDataSource);

protected override int CreateChildControls
(System.Collections.IEnumerable dataSource, bool dataBinding)
PageIndex = MockPageIndex;
return base.CreateChildControls(dataSource, dataBinding);

What, what, whaaat? What is a PagedDataSource? Inside the GridView, the paging is done with a PagedDataSource, a wrapper around a normal DataSource, which has some of the GridView paging properties like PageSize, PageCount, etc. Even if the PageCount is also a read-only property, you have the AllowCustomPaging property and then the VirtualCount and CurrentPageIndex properties that you can set.

In other words: the pager is initialized at databinding. Set MockItemCount and MockPageIndex before MockPagerGrid.DataBind();

That's it.

Update: People keep asking me to provide a code sample. Let's try together. First, let's see a classic GridView use example:

As you can see, we provide a data source programatically, then set the pageindex (let's assume we took it from the URL string) and then call DataBind(). In this situation, we would load the entire data source (say, 10000 rows) then give it to the grid, which would only render something like 20 rows. Very inefficient.

Now, let's replace the original GridView control with the with MockPagerGrid. The code would look like this:

This gets the rows for the second page, sets the mock ItemCount and PageIndex to the total number of rows and the page we want and then calls DataBind(). In this situation getDataSource would load only the 20 rows of page 2, would display it, then the pager would show that it is on page 2 out of 500.

This is a very simple example. It assumes you already know the total number of rows returned. A more complete example would look like this:

// starting with an arbitrary page index
var pageIndex=getPageIndex();
// do operations on the database that would return the rows for the page
// with that index, having the size of the page size of the grid
// and also get the total number of rows in the data source
CustomDataSource dataSource=getDataSource(pageIndex,mockPagerGrid.PageSize);
// set the returned rows as the data source
// set the page index
// set the total row count
// databind

// CustomDataSource would only have two properties: Rows and TotalRowCount
// The sql for getDataSource(index,size) would be something like
// SELECT COUNT(*) FROM MyTable -- get the total count
// SELECT * FROM MyTable WHERE RowIndex>=@index*@size
// AND RowIndex<(@index+1)*@size

// for convenience, I assumed that there is a column called RowIndex in
// the table that is set to the row index

Hopefully, this will help people use this code.