2017-07-24 39 views
0

新手開發人員在這裏尋求建議。根據搜索標準更新直放站控制

我正在寫一個簡單的公告板,這是我第一個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> 

回答

0

問題解決了!

我在另一個論壇上發佈了同樣的問題,我收到了一個非常有幫助的回覆,向我提供了一個解決方案。我的代碼的問題是我已經將DataSource和DataSourceID都設置爲Repeater4。這是因爲按下按鈕時數據未綁定到中繼器導致異常。

以下示例代碼是爲我提供給我的C#代碼。我用這個例子來解決我的問題,並希望它會在未來幫助別人。不要忘記從*。* aspx頁面上的轉發器標籤中刪除數據源!

protected void Page_Load(object sender, EventArgs e) 
    { 
     string titleSearch = customSearchTextBox.Text; 
     SqlConnection conn; 
     SqlDataAdapter myCommand; 
     string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString2"].ConnectionString; 
     conn = new SqlConnection(connectionString); 
     conn.Open(); 
     myCommand = new SqlDataAdapter("SELECT top 30 * FROM Bulletin ", conn); 
     DataSet ds = new DataSet(); 
     myCommand.Fill(ds); 
     Repeater4.DataSource = ds; 
     Repeater4.DataBind(); 
     conn.Close(); 

    } 
    protected void customSearchButton_Click(object sender, EventArgs e) 
    { 
     string titleSearch = customSearchTextBox.Text; 
     SqlConnection conn; 

     SqlDataAdapter myCommand; 
     string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString2"].ConnectionString; 
     conn = new SqlConnection(connectionString); 
     try 
     { 
      conn.Open(); 
      myCommand = new SqlDataAdapter("SELECT * FROM Bulletin WHERE [AdvertTitle] LIKE '%" + titleSearch + "%' ", conn); 
      DataSet ds = new DataSet(); 
      myCommand.Fill(ds); 
      Repeater4.DataSource = ds; 
      Repeater4.DataBind(); 
     } 
     catch (Exception ex) 
     { 
      Response.Write(@"<SCRIPT LANGUAGE=""JavaScript"">alert('" + "Message:" + ex.Message + "')</SCRIPT>"); 
     } 
     finally 
     { 
      conn.Close(); 
     } 
    } 
0

使用文本框和按鈕

<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> 
+0

謝謝你的迴應沙燕。不幸的是,在將這些控件移動到UpdatePanel標記中後,按鈕會完全停止響應。 –