新手开发人员在这里寻求建议。根据搜索标准更新直放站控制
我正在写一个简单的公告板,这是我第一个ASP项目。当我的主页加载时,它显示中继器控件中的最新三十个线程。在中继控制器上方有一个搜索框,用户可以搜索所有线程(不仅仅是所显示的30个)以查找特定的术语。不幸的是,当他们点击搜索按钮时,中继控制器就会消失。理想的行为显然是让表格自我刷新并带回与给定搜索词相关的结果。
C#按钮后面的代码:
protected void customSearchButton_Click(object sender, EventArgs e)
{
string titleSearch = customSearchTextBox.Text;
SqlConnection conn;
SqlCommand comm;
SqlDataAdapter myCommand;
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT * FROM Threads WHERE [AdvertTitle] LIKE '%" + titleSearch + "%' and [ThreadDeleted] = 'No'", conn);
myCommand = new SqlDataAdapter("SELECT * FROM Threads WHERE [AdvertTitle] LIKE '%" + titleSearch + "%' and [ThreadDeleted] = 'No'", conn);
DataSet ds = new DataSet();
myCommand.Fill(ds);
try
{
//if (IsPostBack)
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
Repeater4.DataSource = ds;
Repeater4.DataBind();
}
catch (Exception ex)
{
Response.Write(@"<SCRIPT LANGUAGE=""JavaScript"">alert('" + "Message:" + ex.Message + "')</SCRIPT>");
}
finally
{
conn.Close();
}
}
而* aspx页面本身上的相应代码:更新面板内
<asp:TextBox ID="customSearchTextBox" runat="server"></asp:TextBox><p></p>
<asp:Button ID="customSearchButton" runat="server" Text="Search Adverts!" OnClick="customSearchButton_Click" /><p></p>
<asp:UpdatePanel runat="server" ID="UpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource4">
<HeaderTemplate>
<table id="w3TableSearch" class="w3TableSearch"><tr class="w3TableSearch">
<th style="width:140px;" class="w3TableSearch">Advert Type</th><th style="width:auto;" class="w3TableSearch">Advert Title</th><th style="width:auto;" class="w3TableSearch">Posted By</th><th style="width:auto;" class="w3TableSearch">Post Created</th></tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="w3TableSearch"><td class="w3TableSearch"><%# Eval("AdvertType") %></td><td class="w3TableSearch"><b><a href="viewThread.aspx?id=<%# Eval("Ad_ID") %>"><%# Eval("AdvertTitle") %></b></a></td><td class="w3TableSearch"><%# Eval("AdvertiserName") %></td><td class="w3TableSearch"><%# Eval("PostCreationDateTime", "{0:d/M/yyyy <i> hh:mm:ss tt}") %></td></tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater></ContentTemplate>
</asp:UpdatePanel>
谢谢你的回应沙燕。不幸的是,在将这些控件移动到UpdatePanel标记中后,按钮会完全停止响应。 –