This topic is locked
[SOLVED]

 Add where condition based on which list page is loaded

9/2/2019 10:52:44 AM
ASPRunner.NET General questions
T
Tim author

Hello,
In a ASPR.Net 10.2 project I am using 2 different list pages for one table and I'd like to change the SQL "where" statement depending on which page is loaded. From the help file, to add to the where statement we use query.addWhere in the before table initialized. So I tried this:



if( pageObject.pageName == "list_ReconcileTotals" ) {

query.addWhere("Reviewed = 0 AND Received = 1");

}


But pageObject is not available in the "After table initialized" event. So I moved that code to the "List page:Before SQL query" event, but "query" is not available there. Kind of a catch 22.
Next, in the "List page:Before SQL query" event I tried this:



if( pageObject.pageName == "list_ReconcileTotals" ) {

strWhereClause="Reviewed = 0 AND Received = 1";

}


This worked. But when the user enters search criteria, the search doesn't work because the search's where statement is replaced by this code. So then I tried this:



if( pageObject.pageName == "list_ReconcileTotals" ) {

if (strWhereClause == "") {

strWhereClause="Reviewed = 0 AND Received = 1";

} else {

strWhereClause=strWhereClause + " AND Reviewed = 0 AND Received = 1";

}

}


What I found with this approach is that, if there is no search criteria, my additional where statement works. If there is a search criteria, the search works but it is not adding my additional criteria.
I would love to use the query.addWhere approach on the "After table initialized" event, since that is what it is meant for and it seems the cleanest, but I need to know which list page is loading, and it doesn't seem possible in that event.
Does anyone have ideas on how I can append an additional where clause depending on which list page is loaded?
Thanks,

Tim

jadachDevClub member 9/2/2019

I assume the 2 list pages are for 2 different roles? If so, perhaps you can create a session variable and then use that in the "After table initialized" event.

Sergey Kornilov admin 9/3/2019

Another option is to read the name of an additional page from the URL and apply your logic based on the "page" parameter:

cars/list?page=list1
T
Tim author 9/3/2019

Thanks Jerry, but the different list pages are needed for different steps in a process as opposed to different users. So the query for one list page needs to include "where reviewed = 0" and another page needs "where reviewed = 1". And actually. there is a 3rd page that shows all records so there is no where statement. I'm not sure how I could accomplish this with session variable.
Admin, thanks for the reply. I used to look for query string to determine which page was loading, but this didn't seem to be as reliable or clean as the pageObject.pageName. For instance, the default page usually doesn't have the "page" query string so it will throw an error.
But to try your suggestion, I used the "After table initialized" event and added this code:



if (HttpContext.Current.Request.QueryString["page"].ToString() == "list_ReconcileTotals") {

query.addWhere("Reviewed = 0 AND Received = 1");

}


The list page loaded fine, but as I start to type into a search panel field (which is a lookup with ajax, so the page is trying to create a list of options), I get an error on the page. It is an "Object reference not set to an instance of an object." error. I will try and create a test project that reproduces this error so I can upload it to the demo site.
Thanks to you both for your help.

Tim

T
Tim author 9/6/2019

It works! In the end, really simple. In the "After table initialized" event:



if (MVCFunctions.postvalue("page").ToString() == "list_ReconcileTotals") {

query.addWhere("Reviewed = 0 AND Received = 1");

}


The trick to get it to work with no errors was to use MVCFunctions instead of HttpContext.
Thanks all!

Tim