如何在.net 4中使用LINQ to SQL类实现动态数据站点的搜索



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

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

首先将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>

接下来,我们想要为按钮添加隐藏代码,所以在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"表,所以我想将搜索框的可见性只过滤到employee表。

所以我将这段代码添加到List.aspx.cs中的protected void Page_Load
 if (table.DisplayName == "Employees") { MultiSearchFieldSet.Visible = true; }     
            else
            { MultiSearchFieldSet.Visible = false; };

现在页面是可搜索的!

更简单的解决方案

  • 为你的mything -table创建文件夹,注意DynamicData-system使用你的table的复数名(mything -> mythings)
    • 动态数据 CustomPages mythings
  • 复制动态数据 PageTemplates 列表。aspx到DynamicDataCustomPagesmythings
  • 编辑动态数据 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 --> 

改变QueryExtender

从这个:

        <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更改为mything -table中的文本字段名称。

相关内容

  • 没有找到相关文章

最新更新