posted on Tuesday, January 04, 2005 4:49 PM
by
Saravana
A Tip on ASP.NET 2.0 Gridview Control
GridView is coming with a new element called EmptyDataTemplate and a property EmptyDataText. As you guessed, this template or property will be showed when you bind an empty datasource( for example dataset without any rows) to GridView. Though it is a simple feature, it was not there in previous databound controls like repeater and datagrid. We used to manually find out whether datasource is empty or not, depending upon that we will show some panel or message. Now you can use this feature for that.
Empty DataText property can be used to show a message to the user in case of empty datasource.
EmptyDataTemplate can be used to mention template( i.e. any controls..) to show in case of empty datasource. The template can also be used to show simple message. For example
<asp:GridView ID="GridView1" Runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" EmptyDataText="No Product" >
<EmptyDataTemplate >
There in no product in your cart.
</EmptyDataTemplate>
<Columns>
<asp:BoundField HeaderText="au_lname" DataField="au_lname" />
<asp:BoundField HeaderText="au_fname" DataField="au_fname" />
<asp:BoundField HeaderText="phone" DataField="phone" />
<asp:BoundField HeaderText="address" DataField="address" />
</Columns>
</asp:GridView>
If you mention both EmtpyDataText and EmptyDataTemplate, then EmptyDataTemplate takes precedence.