Friday, December 07, 2007

ASP.NET GridView Prevent Automatic Binding Upon Page Startup

Problem:

I have set up the ObjectDataSource from the Table Adapter using the Visual Studio 2005 in Design Mode. Now the page comes up fine, but I now want to add some query parameter filed, and do not want to show the grid right when the page comes up.

Fix:

There may be many ways of fixing it, but so far this technique is working well for me.
  1. Go to the ObjectDataSource's property and switch to the Events view (the lightening bolt icon on top).
  2. Double click in Selecting event so that the VS will create an event handler.
  3. Go to the event handler.
private bool quiversGridEnabled = false;


protected void ObjectDataSourceQuives_Selecting(object sender,
ObjectDataSourceSelectingEventArgs e)
{
if (quiversEnabled == false) e.Cancel = true;
}

At another point in the code, for example, when the button to query a new quiver is pressed then I would set quiversGridEnabled to true, then subsequent calls to Bind() to the Gridview will show the data based on the query.

Time To Find The Solution:

It took about an hour to find out how I'd do this. If there are better and cleaner ways of doing it, be sure to comment on this entry.

No comments: