Solved

ASP.NET + Custom LinqDataSource + Bind to ListView / GridView

Posted on 2009-05-20
7
1,277 Views
Last Modified: 2013-11-08
I am doing a custom search based on keywords, location and category. I have built the linq query in the code behind fine. I now want to bind it to a listview but also enable paging on the listview.

How is this done? What event do i use, or is it all done from code behind?

Many thanks.
var searchQuery = from s in db.RecruitmentJobs
                          where s.job_end_date >= DateTime.Now
                          select new 
                          {
                              s.job_title, 
                              s.job_location,
                              s.job_categoryID
                          };
 
 
        if (keywords != "")
            searchQuery = searchQuery.Where(s => s.job_title.Contains(keywords));
 
        if (location != "")
            searchQuery = searchQuery.Where(s => s.job_location.Contains(location));
 
        if (category > 0)
            searchQuery = searchQuery.Where(s => s.job_categoryID.Contains("¬" + category + "¬"));

Open in new window

0
Comment
Question by:iziizi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24434973
well, i don't know what a Custom LinqDataSource is... but you can surely do something like this:

GridView.DataSource=searchQuery.Execute

??
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24436517
you can bind the listview to the linq object - also you can do the paging straight from the link source and implement custom paging in the listview to limit the data being sent over the web. like this
public var GetData(<search parameters>, int pageIndex, int pageSize)
{
var searchQuery = from s in db.RecruitmentJobs
                          where s.job_end_date >= DateTime.Now
                          select new 
                          {
                              s.job_title, 
                              s.job_location,
                              s.job_categoryID
                          };
 
 
        if (keywords != "")
            searchQuery = searchQuery.Where(s => s.job_title.Contains(keywords));
 
        if (location != "")
            searchQuery = searchQuery.Where(s => s.job_location.Contains(location));
 
        if (category > 0)
            searchQuery = searchQuery.Where(s => s.job_categoryID.Contains("¬" + category + "¬"));
 
    return searchQuery.Skip((pageIndex - 1) * pageSize).Take(pageSize);
}
 
// calling code - first page, 10 items per page;
var data = GetData(<seach parameters>, 1, 10);
listView1.DataSource = data;
listView1.DataBind();

Open in new window

0
 

Author Comment

by:iziizi
ID: 24436681
in order to do the paging as you have done, i would first need to do a count on the records, and create my own page numbers for the user to click wouldnt i?

I read that this isnt neccasay as u can get it to work with the inbuilt paging in listview / datapager.

i read it here:http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx

"One of the really cool things to notice above is that paging and sorting still work with our GridView - even though we are using a custom Selecting event to retrieve the data.  This paging and sorting logic happens in the database - which means we are only pulling back the 10 products from the database that we need to display for the current page index in the GridView (making it super efficient)."

just not sure how to implement it.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 100 total points
ID: 24436844
ok - in that example Scott has used a LinqDataSource on the page with the GridView. In the Selecting event of the LinqDataSource is where scotts linq query is, so you should do the same. You'll need some way of setting the parameters - querystring, session, controls on the form etc...
Then it looks like he has enabled paging in the gridview in the standard way - its interesting, I've not used this method myself.
0
 

Author Comment

by:iziizi
ID: 24436990
"You'll need some way of setting the parameters - querystring, session, controls on the form etc..."

the params come from QS. the problem i am having is understanding the selecing event - when does the selecting event fire?
0
 

Accepted Solution

by:
iziizi earned 0 total points
ID: 24438963
figured it out. All i was missing was the datasource ID on the listview / gridview doh!
<asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDataSource1">
            
                <ItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Label ID="idLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
                        </td>
                    </tr>
                </ItemTemplate>
                <EmptyDataTemplate>
                    <table runat="server" style="">
                        <tr>
                            <td>
                                No data was returned.</td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
                
            
            <LayoutTemplate>
            
                <table runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                                <tr runat="server" style="">
                                    <th runat="server">
                                        jobt</th>
                                </tr>
                                <tr ID="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="">
                        </td>
                    </tr>
                </table>
            
            </LayoutTemplate>
            
                
            </asp:ListView>
            
            <%--<asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSource1" AutoGenerateColumns="false">
            <Columns>
            <asp:BoundField DataField="JobTitle" HeaderText="Job Title" />
            </Columns>
            </asp:GridView>--%>
        
            <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
                onselecting="LinqDataSource1_Selecting" TableName="AdPlacements" 
                ContextTypeName="GeneralDataContext">
            </asp:LinqDataSource>
 
//code behind
 
var searchQuery = from s in db.RecruitmentJobs
                          where s.job_end_date >= DateTime.Now
                          select new 
                          {
                              JobTitle = s.job_title,
                              s.job_title, 
                              s.job_location,
                              s.job_categoryID
                          };
 
 
        if (keywords != "")
            searchQuery = searchQuery.Where(s => s.job_title.Contains(keywords));
 
        if (location != "")
            searchQuery = searchQuery.Where(s => s.job_location.Contains(location));
 
        if (category > 0)
            searchQuery = searchQuery.Where(s => s.job_categoryID.Contains("¬" + category + "¬"));
 
        e.Result = searchQuery;

Open in new window

0
 

Author Comment

by:iziizi
ID: 24497599
allgood
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question