I'm using a Textbox and a DropDownList to filter database searches. I'm using an SQLDataSource with a blank SelectCommand and then setting the command in the codebehind depending on what the user has typed in and what is selected in the DropDownList. My IF statements in the codebehind only work if txtFindBook != "".
Before I explain anymore here is my code:
Default.aspx
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="jumbotron">
<h1>Find a book...</h1>
<p>
<asp:TextBox ID="txtFindBook" runat="server" Width="700px"></asp:TextBox>
<asp:DropDownList ID="ddlGenres" runat="server"></asp:DropDownList>
<asp:Button ID="btnFindBook" runat="server" Text="Search" OnClick="btnFindBook_Click" Height="36px" />
<p>Enter your search terms in the box above, then click "Search" to begin your search.</p>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
<div class="searchresults">
<asp:GridView ID="gvSearchResults" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSourceSearchResults">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" InsertVisible="False" SortExpression="ID"></asp:BoundField>
<asp:BoundField DataField="BookID" HeaderText="BookID" SortExpression="BookID"></asp:BoundField>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title"></asp:BoundField>
<asp:BoundField DataField="Author" HeaderText="Author" SortExpression="Author"></asp:BoundField>
<asp:BoundField DataField="ISBN_10" HeaderText="ISBN_10" SortExpression="ISBN_10"></asp:BoundField>
<asp:BoundField DataField="ISBN_13" HeaderText="ISBN_13" SortExpression="ISBN_13"></asp:BoundField>
<asp:BoundField DataField="Dewey" HeaderText="Dewey" SortExpression="Dewey"></asp:BoundField>
<asp:BoundField DataField="Genre" HeaderText="Genre" SortExpression="Genre"></asp:BoundField>
<asp:CheckBoxField DataField="isCheckedOut" HeaderText="isCheckedOut" SortExpression="isCheckedOut"></asp:CheckBoxField>
<asp:BoundField DataField="Checked_Out_To_Whome" HeaderText="Checked_Out_To_Whome" SortExpression="Checked_Out_To_Whome"></asp:BoundField>
<asp:BoundField DataField="Due_Date" HeaderText="Due_Date" SortExpression="Due_Date"></asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource runat="server" ID="SqlDataSourceSearchResults" ConnectionString='<%$ ConnectionStrings:DefaultConnection %>' SelectCommand="">
<SelectParameters>
<asp:ControlParameter ControlID="txtFindBook" PropertyName="Text" Name="Title" Type="String"></asp:ControlParameter>
<asp:ControlParameter ControlID="ddlGenres" PropertyName="SelectedValue" DefaultValue="Select Genre" Name="Genre" Type="String"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource>
</div>
Default.aspx.cs
public partial class _Default : Page
{
static string connString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
/*There is no point in populating
the dropdownlist everytime there
is a post back. */
populateddlGenres();
}
}
protected void btnFindBook_Click(object sender, EventArgs e)
{
if (txtFindBook.Text != "" && ddlGenres.SelectedItem.Text != "Select Genre")
{
SqlDataSourceSearchResults.SelectCommand = "SELECT * FROM [Books] WHERE (([Title] LIKE '%' + @Title + '%') AND ([Genre] = @Genre)) ORDER BY [Title]";
Label1.Text = "If Statement 1.";
}
else if (txtFindBook.Text == "" && ddlGenres.SelectedItem.Text != "Select Genre")
{
Label1.Text = "If Statement 2.";
SqlDataSourceSearchResults.SelectCommand = "SELECT * FROM [Books] WHERE ([Genre] = @Genre)";
}
else if (txtFindBook.Text == "" && ddlGenres.SelectedItem.Text == "Select Genre")
{
SqlDataSourceSearchResults.SelectCommand = "SELECT * FROM [Books]";
Label1.Text = "If Statement 3.";
}
else if(txtFindBook.Text != "" && ddlGenres.SelectedItem.Text == "Select Genre")
{
SqlDataSourceSearchResults.SelectCommand = "SELECT * FROM [Books] WHERE ([Title] LIKE '%' + @Title + '%') ORDER BY [Title]";
Label1.Text = "Original.";
}
}
private void populateddlGenres()
{
try
{
using (SqlConnection con = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Genres", con);
con.Open();
ddlGenres.DataSource = cmd.ExecuteReader();
ddlGenres.DataTextField = "GenreText";
//ddlGenres.DataValueField = ""; //We aren't using this because the text is it's own value.
ddlGenres.DataBind(); //MUST GO LAST!
}
}
catch (Exception ex)
{
// Handle the error
}
ddlGenres.Items.Insert(0, new ListItem("Select Genre", "0"));
}
}
Now If I add txtFindBook.text = "a"; at the beginning of If Statements 2 & 3 then it will actually populate gvSearchResults on PostBack.
Is there someway I can still have If statements 2 & 3 work while keeping txtFindBook.Text blank?