Displaying the record in an unfiltered GridView
I have a GridView control which displays records depending on the user input in a search textbox, I also have DropDownList to filter search. What I want to do is to display all the records from a table if a user doesnt type any input from the textbox. I've tried putting another table inside the EmptyDataTemplate but it looked a bit cramped. Is there another way?
<td>
Book Reservation<br />
<br />
Search for book title
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<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>
<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:BoundFie开发者_开发技巧ld 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 />
Help would be much appreciated.
Are you looking for this trick?
...WHERE (@Param1 IS NULL OR Field1 >= @Param1)
AND (@Param2 IS NULL OR Field2 LIKE '%' + @Param2 + '%')
You could also change the test to see if a param is equal to an empty string.
精彩评论