开发者

Linq Search Query Results are not Displayed in Gridview using Linqdatasource

I am implementing a search feature for a table of pe开发者_Go百科rsonnel that is displayed using a GridView. The Linq query works but the GridView does not update it's display. The data source is a linqdatasource. This is the query that is implemented in a method called Search_Submit.

var personnel = from i in context.Personnel
                where SqlMethods.Like(i.PersonnelName, query)
                where SqlMethods.Like(i.PersonnelOffice, query)
                where SqlMethods.Like(i.Username, query)
                where SqlMethods.Like(i.Department, query)
                select new
                {
                    PersonnelName = i.PersonnelName,
                    PersonnelOffice = i.PersonnelOffice,
                    Username = i.Username,
                    Department = i.Department
                };

This is the button that calls the method.

<asp:Button ID="PersonnelSearchButton" runat="server" Text="Search" OnClick="Search_Submit" />

Here is the code for the linqdatasource.

<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="PersonnelDataContext" TableName="Personnel">
</asp:LinqDataSource>


Are you binding the results of the LINQ query in Submit_Search to the GridView? It would probably be helpful to show some more of your code (like the complete Serach_Submit).

i.e., I'd expect something like this:

protected void Search_Submit(object sender, EventArgs e)
{

    var personnel = (from i in context.Personnel
                     where SqlMethods.Like(i.PersonnelName, query)
                     where SqlMethods.Like(i.PersonnelOffice, query)
                     where SqlMethods.Like(i.Username, query)
                     where SqlMethods.Like(i.Department, query)
                     select new
                         {
                             PersonnelName = i.PersonnelName,
                             PersonnelOffice = i.PersonnelOffice,
                             Username = i.Username,
                             Department = i.Department
                          }).ToList();

    GridView1.DataSource = personnel;
    GridView1.DataBind();
}


Are you using a linqdatasource? Or are you executing your query in your button? They are two different things.

You have to assign your query to the linqdatasource, and then Refresh it. I don't recall off hand, but I think you set the the linqdatasource.Select to the text version of your query, then probably call DataBind() again.


Thank you for the help! I was able to solve this problem by adding the onselecting attribute and creating a new function which was executed for selecting purposes.

The website that led me to my final conclusion was: Linqdatasource onselecting tut

I also made a very silly mistake when I transplanted the query from T-SQL to Linq, I forgot the OR '||' after my where statement.

Here is a final output of the code.

Button

<asp:Button ID="PersonnelSearchButton" runat="server" Text="Search" />

Gridview

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AutoGenerateColumns="False" BorderColor="Black" BorderStyle="Solid" 
    BorderWidth="1px" CellPadding="3" DataKeyNames="ID"
    DataSourceID="LinqDataSource1" onrowupdating="GridView1_RowUpdating" 
    ShowFooter="True" EnableModelValidation="True" 
    onrowcommand="GridView1_RowCommand">

Linqdatasource

<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="PersonnelDataContext" TableName="Personnel" OnSelecting="LinqDataSource1_Selecting">
</asp:LinqDataSource>

C#

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    String query = "%%";

    if (PersonnelSearchTextBox.Text != String.Empty)
    {
        query = "%" + PersonnelSearchTextBox.Text + "%";
    }

    var personnel = (from i in context.Personnel
                         where SqlMethods.Like(i.PersonnelName, query) ||
                                 SqlMethods.Like(i.PersonnelOffice, query) ||
                                 SqlMethods.Like(i.Username, query) ||
                                 SqlMethods.Like(i.Department, query)
                         select new
                         {
                             ID = i.ID,
                             PersonnelName = i.PersonnelName,
                             PersonnelOffice = i.PersonnelOffice,
                             Department = i.Department,
                             Username = i.Username
                         });


    e.Result = personnel;
}

I hope this helps others in the future.

Quick Edit: This line of code should be in your Page_Load method.

GridView1.DataSourceID = LinqDataSource1.ID;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜