问题描述
查看Gif并查看问题,当我在其他页面上单击搜索条件后,在文本框中键入并搜索结果就很好
解决方法
<head runat="server">
<title></title>
<link href="App_Themes/Theme1/CSS/Usual.css" rel="stylesheet" />
<link href="Table.css" rel="stylesheet" />
<style>
body
{
font-family: Poppins !important
}
input
{
font: unset !important
}
</style>
<script>
function UpdatePanel() {
__doPostBack("<%=TextBox1.ClientID%>",'')
}
</script>
<link href="App_Themes/Theme1/CSS/Bootstrap.css" rel="stylesheet" />
与身体
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div class="table-b">
<div class="row d-flex justify-content-center">
<div class="col-md-6">
<div class="tables">
<div class="table-search">
<asp:TextBox AutoCompleteType="None" ID="TextBox1" onkeyup="UpdatePanel()" AutoPostBack="false" OnTextChanged="TextBox1_TextChanged" CssClass="key" runat="server" placeholder="Search"></asp:TextBox>
<div class="line-1"></div>
<div class="line-0"></div>
</div>
<asp:UpdatePanel ID="UpdatePanel1" RenderMode="Block" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="product_id">
<ItemTemplate>
<tr style="">
<td>
<asp:Label Text='<%# Eval("product_id") %>' runat="server" ID="product_idLabel" />
</td>
<td>
<asp:Label Text='<%# Eval("product_name") %>' runat="server" ID="product_nameLabel" />
</td>
<td>
<asp:Label Text='<%# Convert.ToInt32(Eval("list_price")).ToString("N0") %>' runat="server" ID="list_priceLabel" />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server" style="width: 100%; text-align: right">
<tr runat="server" class="thead">
<th runat="server">id</th>
<th runat="server">name</th>
<th runat="server">price</th>
</tr>
<tr runat="server" id="itemPlaceholder"></tr>
<tr runat="server">
<td runat="server" style="">
<asp:DataPager runat="server" ID="DataPager1">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField>
<asp:NumericPagerField></asp:NumericPagerField>
<asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField>
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:ProductConnectionString %>' SelectCommand="SELECT product_name,product_id,list_price FROM production.products"></asp:SqlDataSource>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TextBox1" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</div>
</div>
</div>
</form>
这是背后的代码
protected void TextBox1_TextChanged(object sender,EventArgs e)
{
SqlDataSource1.SelectCommand = "SELECT product_name,list_price FROM production.products where product_name like '%" + TextBox1.Text + "%'";
SqlDataSource1.DataBind();
ListView1.DataBind();
}