根据搜索条件更新Repeater控件

时间:2017-07-24 14:15:36

标签: c# asp.net updatepanel repeater

新手开发者在这里寻求建议。

我正在写一个简单的公告板,这是我的第一个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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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();
        }
    }