ASP.. NET GridView不能通过更改SqlDataSource来填充.如果“文本框”为空,则选择“命令”
本文关键字:文本框 文本 为空 命令 选择 如果 填充 不能 GridView NET SqlDataSource | 更新日期: 2023-09-27 18:08:15
我使用一个文本框和一个下拉列表来过滤数据库搜索。我使用带有空白SelectCommand的SQLDataSource,然后根据用户输入的内容和下拉列表中选择的内容在代码后面设置命令。我在后面代码中的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 &3工作,同时保持txtFindBook。文本空白?
通过向在其上定义的Title属性添加默认值NULL来修改SqlDataSource,这样应该就可以了。你不需要标签,反正我也不懂标签的事。
<asp:SqlDataSource runat="server" ID="SqlDataSourceSearchResults" ConnectionString='<%$ ConnectionStrings:DefaultConnection %>' SelectCommand="">
<SelectParameters>
<asp:ControlParameter ControlID="txtFindBook" PropertyName="Text" DefaultValue="NULL" Name="Title" Type="String"></asp:ControlParameter>
<asp:ControlParameter ControlID="ddlGenres" PropertyName="SelectedValue" DefaultValue="Select Genre" Name="Genre" Type="String"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource>