如何在未填充GridView时显示消息

时间:2012-11-30 12:53:43

标签: asp.net

我正在根据前一页查询字符串传递的内容构建一个GridView。

如果没有填充GridView,是否有办法显示“找不到记录”的消息?即查询不会生成任何记录来填充GridView。

代码:

<div class="centergrid">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" CssClass="bookgridview">
        <Columns>
            <asp:TemplateField HeaderText="Choose">
            <ItemTemplate>
                 <asp:HyperLink ID="lnkSelect" runat='server' NavigateUrl='<%# String.Format("~/bookingform.aspx?ID={0}", Eval("ScheduleId")) %>'>Book</asp:HyperLink>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="TName" HeaderText="Name" SortExpression="TName" />
            <asp:BoundField DataField="SDate" HeaderText="Date" SortExpression="SDate" DataFormatString="{0:d}" />
            <asp:BoundField DataField="STime" HeaderText="Time" SortExpression="STime" />
            <asp:BoundField DataField="TPriceadult" HeaderText="Price (Adult)" 
                SortExpression="TPriceadult" DataFormatString="{0:C}"  /> 
            <asp:BoundField DataField="TPricesenior" HeaderText="Price (Student/Senior)" 
                SortExpression="TPricesenior" DataFormatString="{0:C}"  /> 
            <asp:BoundField DataField="TPricechild" HeaderText="Price (Child)" 
                SortExpression="Tpricechild" DataFormatString="{0:C}"   /> 
            <asp:BoundField DataField="TDuration" HeaderText="Duration"
                SortExpression="TDuration" />
            <asp:BoundField DataField="ScheduleId" HeaderText="ScheduleId" 
                SortExpression="ScheduleId"  Visible="False" />
        </Columns>
    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ChinatowndbConnString %>" 
        SelectCommand="SELECT * FROM [vwSchedule] Where TourId=@tid">

        <SelectParameters>
            <asp:QueryStringParameter DbType="Int32" Name="tid" QueryStringField="tourid" />
        </SelectParameters>
    </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:2)

  

有没有办法显示一条消息,说“找不到记录”   GridView没有填充?

是的,使用EmptyDataTemplate

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="SqlDataSource1" CssClass="bookgridview">

    <emptydatarowstyle backcolor="LightBlue"
      forecolor="Red"/>

    <emptydatatemplate>

      <asp:image id="NoDataImage"
        imageurl="~/images/Image.jpg"
        alternatetext="No Image" 
        runat="server"/>

        No records found.  

    </emptydatatemplate> 

    <Columns>
        <asp:TemplateField HeaderText="Choose">
        <ItemTemplate>
             <asp:HyperLink ID="lnkSelect" runat='server' NavigateUrl='<%# String.Format("~/bookingform.aspx?ID={0}", Eval("ScheduleId")) %>'>Book</asp:HyperLink>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="TName" HeaderText="Name" SortExpression="TName" />
        <asp:BoundField DataField="SDate" HeaderText="Date" SortExpression="SDate" DataFormatString="{0:d}" />
        <asp:BoundField DataField="STime" HeaderText="Time" SortExpression="STime" />
        <asp:BoundField DataField="TPriceadult" HeaderText="Price (Adult)" 
            SortExpression="TPriceadult" DataFormatString="{0:C}"  /> 
        <asp:BoundField DataField="TPricesenior" HeaderText="Price (Student/Senior)" 
            SortExpression="TPricesenior" DataFormatString="{0:C}"  /> 
        <asp:BoundField DataField="TPricechild" HeaderText="Price (Child)" 
            SortExpression="Tpricechild" DataFormatString="{0:C}"   /> 
        <asp:BoundField DataField="TDuration" HeaderText="Duration"
            SortExpression="TDuration" />
        <asp:BoundField DataField="ScheduleId" HeaderText="ScheduleId" 
            SortExpression="ScheduleId"  Visible="False" />
    </Columns>
</asp:GridView>

或者,您可以通过设置EmptyDataText属性而不是此属性来使用内置UI作为空数据行。