2011-06-14 49 views
2

我有一個GridView控件,它根據用戶在搜索文本框中的輸入顯示記錄,我也有DropDownList來過濾搜索。我想要做的是顯示錶中的所有記錄,如果用戶不從文本框輸入任何輸入。我試過在EmptyDataTemplate裏放另一張表,但看起來有點侷促。有另一種方法嗎?在未過濾的GridView中顯示記錄

<td> 
     Book Reservation<br /> 
    <br /> 
     Search for book title 
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
     &nbsp; 
     <asp:DropDownList ID="DropDownList1" runat="server" 
      DataSourceID="categoryDataSource" DataTextField="name" 
      DataValueField="categoryid" AppendDataBoundItems="true" > 
      <asp:ListItem Value="-1" Selected="True">-- Choose a category --</asp:ListItem> 
     </asp:DropDownList> 
     <asp:SqlDataSource ID="categoryDataSource" runat="server" 
      ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
      SelectCommand="SELECT [categoryid], [name] FROM [TblCategory]"> 
     </asp:SqlDataSource> 
&nbsp;<asp:Button ID="Button1" runat="server" Text="Search" /> 
     <br /> 
     <br /> 

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      DataKeyNames="bookid" DataSourceID="bookDataSource" Width="800px" AllowPaging="true" 
      AllowSorting="true" > 

      <Columns> 

       <asp:BoundField DataField="bookid" HeaderText="bookid" ReadOnly="True" 
        SortExpression="bookid" Visible="false" /> 

       <asp:HyperLinkField DataTextField="booktitle" DataNavigateUrlFields="bookid" HeaderText="Title" 
        DataNavigateUrlFormatString="Reserving.aspx?bookid={0}" ItemStyle-Width="250px" 
        SortExpression="booktitle" /> 

       <asp:BoundField DataField="booktitle" HeaderText="Title" 
        SortExpression="booktitle" Visible="false" /> 

       <asp:BoundField DataField="lastname" HeaderText="Author" 
        SortExpression="lastname" /> 

       <asp:BoundField DataField="firstname" HeaderText="" 
        SortExpression="firstname" /> 

       <asp:BoundField DataField="description" HeaderText="Description" 
        SortExpression="description" /> 

       <asp:BoundField DataField="categoryid" HeaderText="categoryid" 
        SortExpression="categoryid" Visible="false" /> 

       <asp:BoundField DataField="name" HeaderText="Category" 
        SortExpression="name" /> 

       <asp:BoundField DataField="dateadded" HeaderText="Dateadded" 
        SortExpression="dateadded" Visible="false" /> 

       <asp:BoundField DataField="statusid" HeaderText="statusid" 
        SortExpression="statusid" Visible="false" /> 

       <asp:BoundField DataField="quantity" HeaderText="Quantity" 
        SortExpression="quantity" /> 

      </Columns> 
      <EmptyDataTemplate> 

      <span class="style2">Complete List</span> 

      <asp:GridView ID="GridView2" runat="server" 
      AutoGenerateColumns="False" AllowPaging="true" PageSize="8" AllowSorting="true" 
      DataKeyNames="bookid" DataSourceID="completebookDataSource" Width="800px"> 

      <Columns> 

       <asp:BoundField DataField="bookid" HeaderText="bookid" ReadOnly="True" 
        SortExpression="bookid" Visible="false" /> 

       <asp:HyperLinkField DataTextField="booktitle" DataNavigateUrlFields="bookid" 
        DataNavigateUrlFormatString="Reserving.aspx?bookid={0}" HeaderText="Title" 
        SortExpression="booktitle" ItemStyle-Width="250px" /> 

       <%--<asp:BoundField DataField="booktitle" HeaderText="Title" 
        SortExpression="booktitle" />--%> 

       <asp:BoundField DataField="lastname" HeaderText="Author" 
        SortExpression="lastname" /> 

       <asp:BoundField DataField="firstname" HeaderText="" 
        SortExpression="firstname" /> 

       <asp:BoundField DataField="description" HeaderText="Description" 
        SortExpression="description" /> 

       <asp:BoundField DataField="categoryid" HeaderText="categoryid" 
        SortExpression="categoryid" Visible="false" /> 

       <asp:BoundField DataField="name" HeaderText="Category" 
        SortExpression="name" /> 

       <asp:BoundField DataField="dateadded" HeaderText="dateadded" 
        SortExpression="dateadded" Visible="false" /> 

       <asp:BoundField DataField="statusid" HeaderText="statusid" 
        SortExpression="statusid" Visible="false" /> 

       <asp:BoundField DataField="quantity" HeaderText="Quantity" 
        SortExpression="quantity" /> 

       <asp:CheckBoxField DataField="isdeleted" HeaderText="isdeleted" 
        SortExpression="isdeleted" Visible="false" /> 

      </Columns> 

     </asp:GridView> 

     <asp:SqlDataSource ID="completebookDataSource" runat="server" 
      ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 

      SelectCommand="SELECT dbo.TblBooks.bookid, dbo.TblBooks.booktitle, dbo.TblBooks.lastname, dbo.TblBooks.firstname, dbo.TblBooks.description, dbo.TblBooks.categoryid, dbo.TblBooks.dateadded, dbo.TblBooks.statusid, dbo.TblBooks.quantity, dbo.TblBooks.isdeleted, dbo.TblCategory.name FROM dbo.TblBooks INNER JOIN dbo.TblCategory ON dbo.TblBooks.categoryid = dbo.TblCategory.categoryid ORDER BY dbo.TblBooks.booktitle ASC"> 
     </asp:SqlDataSource> 

      </EmptyDataTemplate> 

     </asp:GridView> 
     <asp:SqlDataSource ID="bookDataSource" runat="server" 
      ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 

      SelectCommand="SELECT dbo.TblBooks.bookid, dbo.TblBooks.booktitle, dbo.TblBooks.lastname, dbo.TblBooks.firstname, dbo.TblBooks.description, dbo.TblBooks.categoryid, dbo.TblBooks.dateadded, dbo.TblBooks.statusid, dbo.TblBooks.quantity, dbo.TblCategory.name FROM dbo.TblBooks INNER JOIN dbo.TblCategory ON dbo.TblBooks.categoryid = dbo.TblCategory.categoryid WHERE (dbo.TblBooks.categoryid = @categoryid) AND (dbo.TblBooks.booktitle LIKE '%' + @booktitle + '%')"> 
      <SelectParameters> 
       <asp:ControlParameter ControlID="DropDownList1" Name="categoryid" 
        PropertyName="SelectedValue" Type="Int32" /> 
       <asp:ControlParameter ControlID="TextBox1" Name="booktitle" PropertyName="Text" 
        Type="String" /> 
      </SelectParameters> 
     </asp:SqlDataSource> 
     <br /> 
     <br /> 

幫助將不勝感激。

回答

3

你在找這個把戲嗎?

...WHERE (@Param1 IS NULL OR Field1 >= @Param1) 
    AND (@Param2 IS NULL OR Field2 LIKE '%' + @Param2 + '%') 

您還可以更改測試以查看param是否等於空字符串。

+0

看起來像。那麼如何在我的數據列中添加一個參數呢?在我的代碼中,我只有一個指向TextBox和DropDownList的控件參數,因此無論輸入用戶類型,它都會顯示具有匹配值的記錄。 – Loupi 2011-06-14 05:16:40

+0

它看起來像你已經有了參數:@categoryid @booktitle你也可以把選擇''放在DropDownList中,並測試它,而不是null。 – 2011-06-14 12:13:48