How do I call a class from an data source's control event?

Posted on 2009-04-29
Last Modified: 2013-12-17
I have written a C# web site using visual studio 2008. I am using an 3.5 listview control to display my data. I have written the attached classes within my project where I am now looking to call the "public partial class Trust" within the OnSelecting event of my linqdatasource which is bound to my listview control. Can anyone give me some help on how to do this?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Linq.Expressions;

using System.Web;


/// <summary>

/// Summary description for LinqPredicateBuilder

/// </summary>

public static class LinqPredicateBuilder


    public static Expression<Func<T, bool>> True<T>() { return f => true; }

    public static Expression<Func<T, bool>> False<T>() { return f => false; }


    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,

                                                        Expression<Func<T, bool>> expr2)


        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());

        return Expression.Lambda<Func<T, bool>>

              (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);



    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,

                                                         Expression<Func<T, bool>> expr2)


        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());

        return Expression.Lambda<Func<T, bool>>

              (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);




public partial class Trust


    public static Expression<Func<Trust, bool>> ContainsInDescription(params string[] keywords)


        TrustSystemMARTINSPALDINGDataContext db = new TrustSystemMARTINSPALDINGDataContext();


        var predicate = LinqPredicateBuilder.True<Trust>();

        foreach (string keyword in keywords)


            string temp = keyword;

            predicate = predicate.Or(p => p._ClientName.Contains(temp));

            predicate = predicate.Or(p => p._MatterNo.Contains(temp));

            predicate = predicate.Or(p => p._PartnerLookup.Entity.PartnerDescription.Contains(temp));

            predicate = predicate.Or(p => p._FeeExecLookup.Entity.FeeExecDescription.Contains(temp));

            predicate = predicate.Or(p => p._TypeLookup.Entity.TypeDescription.Contains(temp));


            var query = (from tt in db.Trusts.Where(predicate)

                         join felt in db.FeeExecLookups on tt.FeeExecCode equals felt.FeeExecCode

                         join ilt in db.IncomeLookups on tt.IncomeCode equals ilt.IncomeCode

                         join plt in db.PartnerLookups on tt.PartnerCode equals plt.PartnerCode

                         join tlt in db.TypeLookups on tt.TypeCode equals tlt.TypeCode

                         // perform a left join on TrustTypeLookups so all fields are returned including NULLs

                         join ttlt in db.TrustTypeLookups on tt.TrustTypeCode equals ttlt.TrustTypeCode into tc

                         from ttlt in tc.DefaultIfEmpty()

                         orderby tt.ClientName ascending

                         // TrustTypeDescription is selected on different cases below: whether it's null, nothing or something

                         select new { tt.TrustID, tt.ClientName, tt.MatterNo, tt.PartnerCode, tt.FeeExecCode, tt.TypeCode, tt.TrustTypeCode, tt.Nominee, tt.TEA, tt.IncomeCode, tt.TrustNotes, tt.Archive, felt.FeeExecDescription, ilt.IncomeDescription, plt.PartnerDescription, tlt.TypeDescription, TrustTypeDescription = ttlt.TrustTypeDescription == null ? "" : ttlt.TrustTypeDescription });


        return predicate;



Open in new window

Question by:Shepwedd
    LVL 10

    Expert Comment

    I'm not a c# programmer (i program in VB.NET & ASP.NET usually).  But when you want to link that to a listbox, usually your class must inherits from a datasource object like SQLDataSource and you must implements every function needed to extract the data...

    I see no such thing in your code.

    Once you have done this, just set the DataSourceID of your listbox to an instance of your class.

    Accepted Solution

    My problem was resolved by moving the code within my "public partial class trust" to my "OnSelecting" event of my linqdatasource. I guess I miss understood my class code.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Title # Comments Views Activity
    Need Regex Money No $ 5 43
    Help file 1 28
    Need help error handling a windows service 2 25
    call function 8 32
    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…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now