Reputation:
I have an ASP.NET GridView that uses an EmptyDataTemplate. This template is used to collect data in the event that no records exist in my data source. My GridView source looks like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="Lecturer" CellPadding="4"
ForeColor="#333333" GridLines="None" style="text-align: center" allowsorting="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" />
<asp:HyperLinkField HeaderText="URL" SortExpression="url" DataNavigateUrlFields="url" Text="Link" ItemStyle-Width="100" />
</Columns>
<EmptyDataTemplate>
No data found!
</EmptyDataTemplate>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
The problem here is that i want the empty data template to be shown after the button search has been click but right now everytime i open the page, it will show the empty data template eventhough i didnt do any searching yet.
Upvotes: 2
Views: 28891
Reputation: 28990
Try this:
private DataTable GetDataTable(Boolean blnIsReturnNull)
{
DataTable tblReturnTable = new DataTable();
tblReturnTable.Columns.Add("name");
tblReturnTable.Columns.Add("URL");
if (blnIsReturnNull)
{
return tblReturnTable;
}
for (int intCounter = 0; intCounter < 10; intCounter++)
{
//Build of tblReturnTable
}
return tblReturnTable;
}
protected void cmdEmptyGridView_Click(object sender, EventArgs e)
{
GridView1.DataSource = GetDataTable(true);
GridView1.DataBind();
}
<asp:Button ID="cmdEmptyGridView" runat="server" Text="Empty GridView" OnClick="cmdEmptyGridView_Click"
CssClass="Button" /></td>
Upvotes: 0
Reputation: 7539
Sounds like you are binding your GridView in your Page_Load event instead of the Search button click. Move your binding code into a separate method and call that method when the search button is clicked in its click event.
Upvotes: 1
Reputation: 22323
use:
EmptyDataText="No data found!"
in a grid like:
<asp:GridView ID="GridView1" EmptyDataText="No data found!" ....>
Your way is incorrect.EmptyDataTemplate
isn't use for this purpose.
Upvotes: 11
Reputation: 63095
Remove EmptyDataTemplate
, when you search if no records match with the search, set EmptyDataText
of your gridview.
Upvotes: 1