0

I just created a Default Dynamic Data Site. How Can I add search to it?

2 Answers 2

1

You can add search functionality by doing the following.

Firstly add the UI to the List.aspx page with the following code

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

Next, we want to add the code-behind for the Button, so on List.aspx.cs go down to

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

And change it to

 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();


                }
        }

And finally, beacause we are only searching the "Employees" table, I want to filter the visibility of the search box only to the employee's table.

So I add this code to List.aspx.cs in protected void Page_Load

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

And now the page is Searchable!

Sign up to request clarification or add additional context in comments.

1 Comment

The var EmployeeNameString = txbMultiColumnSearch.Text; line was missing the var from the beginning. Otherwise a valid tip, thanks.
1

More simple solution

  • Create folder for your mything -table, note that the DynamicData-system uses plural name of your table (mything ---> mythings)
    • DynamicData\CustomPages\mythings\
  • Copy DynamicData\PageTemplates\List.aspx to DynamicData\CustomPages\mythings\
  • Edit DynamicData\CustomPages\mythings\List.aspx

Change this line:

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

To this:

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

Change QueryExtender

From this:

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

To this:

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

Note change the myfirstfield,mysecondfield,myNthfield to the text field names at your mything -table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.