I just created a basic ASP.NET website using Microsoft’s walkthrough here. It has one page with a form that takes in some text input, runs a database query based on that, and results the results in a GridView. I added the EmptyDataText property to my GridView to explicitly show users when their search returns no results.
It all works as expected with one exception:
IIS shows my EmptyDataText of “No results found.” even before the search form is submitted.
This defeats my purpose behind using EmptyDataText, which is to indicate to the user that the webpage successfully submitted their search but found no results, as opposed to took their search and threw it into the ether.
For example, a user who searches for something that cannot be found will see “No results found.” both before and after their search, as opposed to nothing before and “No results found.” after. The former behavior gives the impression that the search didn’t work.
How can I configure my GridView to show the EmptyDataText only after the search form is submitted?
As the other answers suggested, the
GridViewis being bound before the user makes a search because it has aDataSourceIDattribute. As explained on MSDN, this attribute causes theGridViewto automatically bind to the specified source:To get the behavior I was looking for, I removed that attribute from the
GridViewand instead added anOnClickattribute to the submit button for my search form. TheOnClickattribute refers to a methodBindGridViewthat gets called only when the user submits the form.All this method does is populate the
DataSourceIDwith the same value it had before as an attribute: