【问题标题】:Update Repeater control based on search criteria根据搜索条件更新中继器控件
【发布时间】:2017-07-25 17:01:23
【问题描述】:

新手开发者,求指教。

我正在编写一个简单的公告板,这是我的第一个 ASP 项目。当我的主页加载时,它会在转发器控件中显示最新的 30 个线程。在转发器控件上方有一个搜索框,用户可以在其中搜索所有线程(不仅仅是显示的 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>

【问题讨论】:

    标签: c# asp.net updatepanel repeater


    【解决方案1】:

    在更新面板中使用文本框和按钮

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

    【讨论】:

    • 感谢您的回复Shayan。不幸的是,将这些控件移动到 UpdatePanel 标记后,按钮完全停止响应。
    【解决方案2】:

    问题解决了!

    我在另一个论坛上发布了同样的问题,我收到了非常有用的回复,它为我提供了解决方案。我的代码的问题是我将 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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      相关资源
      最近更新 更多