0

我刚刚创建了一个默认动态数据​​站点。如何添加搜索?

4

2 回答 2

1

您可以通过执行以下操作来添加搜索功能。

首先使用以下代码将 UI 添加到 List.aspx 页面

<fieldset id="MultiSearchFieldSet" class="DD" runat="server" visible="False">
 <asp:TextBox ID="txbMultiColumnSearch" CssClass="DDTextBox" runat="server" />
    <asp:Button ID="btnMultiColumnSearchSubmit" CssClass="DDControl" runat="server" Text="Search"
        OnClick="btnMultiColumnSearch_Click" />
    <asp:Button ID="btnMultiColumnSearchClear" CssClass="DDControl" runat="server" Text="Clear"
        OnClick="btnMultiColumnSearch_Click" />
</fieldset>

接下来,我们要为 Button 添加代码隐藏,所以 List.aspx.cs 向下到

protected void btnMultiColumnSearch_Click(object sender, EventArgs e)
        {
        }

并将其更改为

 protected void btnMultiColumnSearch_Click(object sender, EventArgs e)
        {
            var button = (Button)sender;
            if (button.ID == btnMultiColumnSearchClear.ID)
                txbMultiColumnSearch.Text = String.Empty;
            else
                using (PhoneListDataContext Data = new PhoneListDataContext())
                {
                    EmployeeNameString = txbMultiColumnSearch.Text;
                    var SearchResults = Data.Employees.Where
                       (Employee => (Employee.FirstName.Contains(EmployeeNameString) || (Employee.LastName.Contains(EmployeeNameString))));


                    GridView1.DataSourceID = ""; 
                    GridView1.DataSource = SearchResults;
                    GridView1.DataBind();


                }
        }

最后,因为我们只搜索“Employees”表,我想过滤搜索框的可见性只对员工表。

所以我将此代码添加到受保护的 void Page_Load中的List.aspx.cs

 if (table.DisplayName == "Employees") { MultiSearchFieldSet.Visible = true; }     
            else
            { MultiSearchFieldSet.Visible = false; };

现在该页面是可搜索的!

于 2013-03-22T15:29:38.100 回答
1

更简单的解决方案

  • 为您的 mything -table 创建文件夹,注意 DynamicData-system 使用您的表格的复数名称(mything ---> mythings)
    • DynamicData\CustomPages\mythings\
  • 将 DynamicData\PageTemplates\List.aspx 复制到 DynamicData\CustomPages\mythings\
  • 编辑 DynamicData\CustomPages\mythings\List.aspx

更改此行:

 <h2 class="DDSubHeader"><%= table.DisplayName%></h2>

对此:

<h2 class="DDSubHeader"><%= table.DisplayName%></h2>
 <!-- Custom: 
    - Added SearchTextBox and SearchButton. 
        - Attention: Requires SearchExpression to work, 
        see next Custom -comment later in this page. 
--> 
<table>
    <tr>
        <td>
            <asp:TextBox ID="SearchTextBox" runat="server" />
        </td>
        <td>
            <asp:Button ID="SearchButton" runat="server" Text="Search" />
        </td>
        <td>
            <div class="DDBottomHyperLink">
                <a  href="<%= HttpContext.Current.Request.Url.AbsoluteUri %>">Clear search filter</a>
            </div>
        </td>
    </tr>
</table>
<!-- Custom ends --> 

更改查询扩展器

由此:

        <asp:QueryExtender TargetControlID="GridDataSource" ID="GridQueryExtender" runat="server">
            <asp:DynamicFilterExpression ControlID="FilterRepeater" />
        </asp:QueryExtender>

对此:

        <!-- Custom: edit QueryExtender --> 
        <asp:QueryExtender TargetControlID="GridDataSource" ID="GridQueryExtender" runat="server">
            <asp:DynamicFilterExpression ControlID="FilterRepeater" />
               <asp:SearchExpression SearchType="Contains" DataFields="myfirstfield,mysecondfield,myNthfield" >
               <asp:ControlParameter ControlID="SearchTextBox" />
            </asp:SearchExpression>
        </asp:QueryExtender>
        <!-- Custom ends --> 

请注意myfirstfield、mysecondfield、myNthfield更改为您的神话表中的文本字段名称。

于 2019-12-27T14:54:09.820 回答