根据搜索条件更新 Repeater 控件
Update Repeater control based on search criteria
新手开发者在此求教。
我正在编写一个简单的公告板,这是我的第一个 ASP 项目。当我的主页加载时,它会在转发器控件中显示最新的三十个线程。在转发器控件上方有一个搜索框,用户可以在其中搜索所有线程(不仅仅是显示的三十个)以查找特定术语。不幸的是,当他们单击搜索按钮时,转发器控件就消失了。所需的行为显然是 table 刷新自身并带回与给定搜索词相关的结果。
按钮上的 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>
在更新面板中使用文本框和按钮
<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>
问题已解决!
我在另一个论坛上发布了同样的问题,我收到了非常有帮助的回复,为我提供了解决方案。我的代码的问题是我将 DataSource 和 DataSourceID 都设置为 Repeater4。这导致了异常,因为按下按钮时数据未绑定到转发器。
为我后面的 C# 代码提供了以下示例代码。我用这个例子来解决我的问题,希望它能在将来帮助其他人。不要忘记也从 *.*aspx 页面上的 repeater 标签中删除数据源!
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();
}
}
新手开发者在此求教。
我正在编写一个简单的公告板,这是我的第一个 ASP 项目。当我的主页加载时,它会在转发器控件中显示最新的三十个线程。在转发器控件上方有一个搜索框,用户可以在其中搜索所有线程(不仅仅是显示的三十个)以查找特定术语。不幸的是,当他们单击搜索按钮时,转发器控件就消失了。所需的行为显然是 table 刷新自身并带回与给定搜索词相关的结果。
按钮上的 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>
在更新面板中使用文本框和按钮
<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>
问题已解决!
我在另一个论坛上发布了同样的问题,我收到了非常有帮助的回复,为我提供了解决方案。我的代码的问题是我将 DataSource 和 DataSourceID 都设置为 Repeater4。这导致了异常,因为按下按钮时数据未绑定到转发器。
为我后面的 C# 代码提供了以下示例代码。我用这个例子来解决我的问题,希望它能在将来帮助其他人。不要忘记也从 *.*aspx 页面上的 repeater 标签中删除数据源!
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();
}
}