1

我正在根据上一页查询字符串上传递的内容构建一个 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>
4

1 回答 1

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 。

于 2012-11-30T12:55:43.890 回答