我正在使用Textbox和DropDownList来过滤数据库搜索。我正在使用带有空白SelectCommand的SQLDataSource,然后在代码隐藏中设置命令,具体取决于用户输入的内容以及DropDownList中选择的内容。代码隐藏中的我的IF语句只有在txtFindBook!=“”。
时才有效在我解释之前,这是我的代码:
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"));
}
}
现在如果我添加txtFindBook.text =“a”;在If语句2和开头的开头3然后它实际上会在PostBack上填充gvSearchResults。
有没有我仍然可以使用If语句2&amp; 3,同时保持txtFindBook.Text空白?
答案 0 :(得分:0)
通过在其上定义的Title属性中添加默认值NULL来修改SqlDataSource,这应该可以解决问题。你不需要标签,我也不懂标签。
var testdata = [String]()
override func tableView(tableView: UITableView, cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("controlcell",
forIndexPath: indexPath) as! TableViewCell
// Configure the cell...
cell.clabel.text = testdata[indexPath.row]
return cell
}