locked
How to filter tables on the default.aspx page by id RRS feed

  • Question

  • User1395754811 posted

    Hi,

    I'm intrested in filtering the tabels collection on the default.aspx page. The filtering is based on a ID from the data base, selected on a previus page.

    my first question is how do i filter the tables by the selected ID?

    i have tables that not contain a colum with the selected ID but they have a foreign key table that contains the colum with the selected ID. the question is how can i select the tables based on the selected ID?

    protected void Page_Load(object sender, EventArgs e) {
    
                int id = Convert.ToInt32(Session["SelectedID"]);
    
                if (id != 0) {
    
                    if (User.Identity.IsAuthenticated && User.IsInRole(RoleEnum.Administrator.ToString())) {
    
                        //here i try to select the tables that contain the colum with the id but also i want the filter to select the tables that don't have the colum with the id but have a foreign key with a table that contains the id
    
                        System.Collections.IList visibleTables = Global.DefaultModel.VisibleTables.Where(tables => tables.ID == id );
    
                        if (visibleTables.Count == 0) {
                            throw new InvalidOperationException("There are no accessible tables.");
                        }
    
                        Menu1.DataSource = visibleTables;
                        Menu1.DataBind();
                    }
    
                    else {
                        Response.Redirect("~/Login.aspx");
                    }
                }
    
            }

    any help is most welcome

    Regards.

    Tuesday, June 5, 2012 7:12 AM

Answers

  • User-330204900 posted

    No worries OK I think you have two options (and an extra one when we have .Net 4.5 called Model Binding)

    Note: pages are pre filtered automatically for FK values passed in the query string (ULR) ofr RK columns e.g.:

    http://localhost:2580/Orders/List.aspx?EmployeeID=1

    as you can see from the above the FK Column is passed as a value to the List Page pre filtering it try clicking on a children list column to see the effect.

    1. Use Domain Service this makes it real easy to pre filter, but has xome draw backs.
    2. Create a custom filter you add to the QueryExtender, this has limitations
      1. you can only filter the List page not the Drop down list for FK columns etc.

    I will take option 2 here:

     

    const string columnName = "CustomerID";
    if (Session[columnName] != null)
    {
        var customExpression = new PropertyExpression();
        var parameter = new Parameter(columnName, DbType.Int32, Session[columnName].ToString());
        customExpression.Parameters.Add(parameter);
        customExpression.SetContext(GridQueryExtender, Context, GridDataSource);
        GridQueryExtender.Expressions.Add(customExpression);
    }

    here it assums you have a parameter you are passing in via session (you could use any means you wish) then adds a Property Expression to the to QueryExtender to pre filter everything.

    P.S. see my article here Adding a Multi-Column Search to the Default List page in Dynamic Data (UPDATED)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2012 9:07 AM

All replies

  • User-330204900 posted

    Sorry JesusUPB, there is no ID property on MetaTable which is what the VisibleTables collections holds?

    Tuesday, June 5, 2012 7:18 AM
  • User-330204900 posted

    Reading again do you mean you want to pre filter tables based on an ID column?

    Tuesday, June 5, 2012 7:19 AM
  • User1395754811 posted

    yes i know that the VisibleTables doesn't have a property " ID" i was trying too explain what i want to do. i'd like to filter the tables based on a ID that is kept in a table and has relationships with odor tables.

    ID_table1

    Name

    1

    Name1

    2

    Name2

     

    ID_table2

    ID_table1

    Names

    1

    1

    Name1_1

    2

    1

    Name1_2

    3

    2

    Name2_1

    4

    2

    Name2_2


    if i select ID_table1=1 from the first table the second table will be

    ID_table2

    ID_table1

    Names

    1

    1

    Name1_1

    2

    1

    Name1_2

    Is that possible?

    Tuesday, June 5, 2012 7:42 AM
  • User-330204900 posted

    sorry to appear a little dumb, but are you:

    1. Trying to filter which tables are visible on the Default.aspx page?
    2. Trying to filter the data displayed on a List page?

    Which ever you  are trying to do you can do it, and I will demonstrate how :)

    Tuesday, June 5, 2012 7:58 AM
  • User1395754811 posted

    sorry this is my first post on a forum, next time i'll try to be more specific :)

    i'm trying to filter the data displayed on a List page.  And I thought that i have to pre filter the tables with the information that i need.

    it will be very helpful if you can demonstrate how it's done :)

    i know that you can put a filter on the List page. I don't want that, I have a custom filter page, Filter.aspx before the Default.aspx page, after i select the id from the Filter.aspx I redirect to Default.aspx page and i need the information in the tables ( List pages) to be correlated like int the example i gave above

    thx a lot

    Tuesday, June 5, 2012 8:12 AM
  • User-330204900 posted

    No worries OK I think you have two options (and an extra one when we have .Net 4.5 called Model Binding)

    Note: pages are pre filtered automatically for FK values passed in the query string (ULR) ofr RK columns e.g.:

    http://localhost:2580/Orders/List.aspx?EmployeeID=1

    as you can see from the above the FK Column is passed as a value to the List Page pre filtering it try clicking on a children list column to see the effect.

    1. Use Domain Service this makes it real easy to pre filter, but has xome draw backs.
    2. Create a custom filter you add to the QueryExtender, this has limitations
      1. you can only filter the List page not the Drop down list for FK columns etc.

    I will take option 2 here:

     

    const string columnName = "CustomerID";
    if (Session[columnName] != null)
    {
        var customExpression = new PropertyExpression();
        var parameter = new Parameter(columnName, DbType.Int32, Session[columnName].ToString());
        customExpression.Parameters.Add(parameter);
        customExpression.SetContext(GridQueryExtender, Context, GridDataSource);
        GridQueryExtender.Expressions.Add(customExpression);
    }

    here it assums you have a parameter you are passing in via session (you could use any means you wish) then adds a Property Expression to the to QueryExtender to pre filter everything.

    P.S. see my article here Adding a Multi-Column Search to the Default List page in Dynamic Data (UPDATED)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2012 9:07 AM
  • User1395754811 posted

    thx it solved my problem :D

    but now i encounter other 2 problems

    1. the default filter on the List page is populated with all the other informations, i want it to be populated only with the id from the customExpression if it's possible can it be applied to the default insert, edit controls on the list page?
    2. i have other tables that not contain the columnName, but they have a relationship with a table that contains the columnName, the filter can be applied to those tables as well?

    thx for all your help :D

    Wednesday, June 6, 2012 4:12 AM
  • User-330204900 posted

    the default filter on the List page is populated with all the other informations, i want it to be populated only with the id from the customExpression if it's possible can it be applied to the default insert, edit controls on the list page?

    This is the more difficule one you have two choices;

    1. Create a custom filter for each FK column that you need this on.
    2. Customise the default FK filter

    1 is the easyest but the most work i.e. if you have 10 FK columns in your model that required this then that is ten custom field templates.

    2 is more difficult as you will need to do some generic Linq Expressions see my article here Cascading Filters and Fields – Dynamic Data Entity Framework Version and look particularly at the PopulateListControl method as this could be adapted to achive what you want.

    Sorry not sure what you mean in point 2 :(

     

    Wednesday, June 6, 2012 5:50 AM