新手開發人員在這裏尋求建議。根據搜索標準更新直放站控制
我正在寫一個簡單的公告板,這是我第一個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標記中後,按鈕會完全停止響應。 –