在未过滤的GridView中显示记录

时间:2011-06-14 03:52:46

标签: c# asp.net gridview

我有一个GridView控件,根据搜索文本框中的用户输入显示记录,我还有DropDownList来过滤搜索。我想要做的是如果用户没有从文本框输入任何输入,则显示表中的所有记录。我已经尝试在EmptyDataTemplate中放入另一个表但看起来有点局促。还有另一种方式吗?

<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 />

非常感谢帮助。

1 个答案:

答案 0 :(得分:3)

你在寻找这个技巧吗?

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

您还可以更改测试以查看param是否等于空字符串。