PhilChoc
PhilChoc

Reputation: 1

Gridview error message

when I go onto my product search, the text from my EmptyDataTemplate shows up before I have even searched! can anyone shed light on this situation?

<asp:GridView ID="gvProducts" runat="server" CellPadding="4" DataSourceID="sdsProducts" ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" />
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />
     <EmptyDataTemplate>
         <asp:Label Text="Sorry, no results found" SkinID="ErrorMessage" runat="server" />
     </EmptyDataTemplate>
     </asp:GridView>

Thanks.

Upvotes: 0

Views: 357

Answers (1)

j.f.
j.f.

Reputation: 3949

This is because of how you bind your data source. When you declare a data source like DataSourceID="sdsProducts" in your GridView's markup, that data source will bind its data to your GridView each time the page loads.

So when your page loads for the first time, your data source retrieves an empty set of data and binds it to the GridView, showing your EmptyDataTemplate.

An alternative approach is to bind via the code behind. This way you tell it exactly when you want it to bind, avoiding any binding on the first page load if you so choose.

Upvotes: 1

Related Questions