如何在。net 4的动态数据站点中使用LINQ来实现对SQL类的搜索?

时间:2021-10-06 21:29:40

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

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

1 个解决方案

#1


0  

You can add search functionality by doing the following.

您可以通过以下步骤添加搜索功能。

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

首先将UI添加到列表中。具有以下代码的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>

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

接下来,我们要为按钮添加代码,所以在List.aspx上。cs去

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.

最后,因为我们只搜索“Employees”表,我想要将搜索框的可见性过滤到employee表中。

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

我将这个代码添加到List.aspx。在受保护的void Page_Load中。

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

And now the page is Searchable!

现在页面可以搜索了!

#1


0  

You can add search functionality by doing the following.

您可以通过以下步骤添加搜索功能。

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

首先将UI添加到列表中。具有以下代码的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>

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

接下来,我们要为按钮添加代码,所以在List.aspx上。cs去

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.

最后,因为我们只搜索“Employees”表,我想要将搜索框的可见性过滤到employee表中。

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

我将这个代码添加到List.aspx。在受保护的void Page_Load中。

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

And now the page is Searchable!

现在页面可以搜索了!