2017-07-24 57 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标记中后,按钮会完全停止响应。 –