简体   繁体   中英

How to stop ListView from populating ASPX on pageload?

I have an ASP Search page, which on pageload calls the SearchValves function, even if there aren't any filters. This causes basically all of the records to return, which makes the page load take forever. Is there anyway I can prevent the page from calling this function on pageLoad? Here's the code:

<asp:ObjectDataSource ID="ValvesDataSource" runat="server" SelectMethod="SearchValves"
    TypeName="ApexRemington.BLL.ValveWorkTicketBLL" SortParameterName="sortExpr">
    <SelectParameters>
        <asp:ControlParameter ControlID="CustomerID" Name="customer" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="WorkTicket" Name="workTicket" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="SalesOrder" Name="salesOrder" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="StartAddDate" Name="startAddDate" PropertyName="Text" Type="DateTime" />
        <asp:ControlParameter ControlID="EndAddDate" Name="endAddDate" PropertyName="Text" Type="DateTime" />
        <asp:ControlParameter ControlID="StartCompletionDate" Name="startCompletionDate" PropertyName="Text" Type="DateTime" />
        <asp:ControlParameter ControlID="EndCompletionDate" Name="endCompletionDate" PropertyName="Text" Type="DateTime" />
        <asp:ControlParameter ControlID="Area" Name="area" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="LocationDescription" Name="locationDescription" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="EquipmentProtected" Name="equipmentProtected" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="ValveSerial" Name="valveSerial" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="ValveNumber" Name="valveNumber" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="BasketNumber" Name="basketNumber" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="Manufacturer" Name="manufacturer" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="ValveModel" Name="valveModel" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="TypeList" Name="valveType" PropertyName="SelectedValue" Type="Object" />
        <asp:ControlParameter ControlID="ValveSize" Name="valveSize" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="ValveRating" Name="valveRating" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="SetPressure" Name="setPressure" PropertyName="Text" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

The "SearchValves" function:

public static IEnumerable<Valve> SearchValves(
        string customer,
        string workTicket,
        string salesOrder,
        string area,
        string locationDescription,
        string equipmentProtected,
        DateTime? startAddDate,
        DateTime? endAddDate,
        DateTime? startCompletionDate,
        DateTime? endCompletionDate,
        string valveSerial,
        string valveNumber,
        string basketNumber,
        string manufacturer,
        string valveModel,
        ValveType? valveType,
        string valveSize,
        string valveRating,
        string setPressure,
        string sortExpr)
    {
        using (MiniProfiler.Current.Step("SearchValves"))
        using (var context = rempscoDataContext.CreateContext())
        {
            IQueryable<valve_history> data = context.valve_histories;

            var flag = false;


            bool valve_flag;
            var valve_filter = _BuildValveFilter(
                customer,
                valveSerial,
                valveNumber,
                basketNumber,
                manufacturer,
                valveModel,
                valveType,
                valveSize,
                valveRating,
                setPressure,
                out valve_flag);

            data = data.Where(valve_filter);
            if (valve_flag)
                flag = true;

            bool ticket_flag;
            bool only_store_flag;
            var ticket_filter = _BuildWorkTicketFilter(
                customer,
                workTicket,
                null,
                salesOrder,
                startAddDate,
                endAddDate,
                startCompletionDate,
                endCompletionDate,
                area,
                locationDescription,
                equipmentProtected,
                string.Empty,
                string.Empty,
                string.Empty,
                out ticket_flag,
                out only_store_flag);

            if (ticket_flag)
            {
                data = data.WhereAny(v => v.valve_work_tickets, ticket_filter);
                flag = true;
            }

            if (!flag)
            {
                data = data.Where(v =>
                    v.valve_work_tickets.Any(wt => Security.CurrentStores.Contains(wt.store.s_store_id)) &&
                    v.valve_work_tickets.Any(wt => wt.status == (char)ValveWorkTicketStatus.Invoiced));
            }

            var retData = (
                from vh in data
                group vh by vh.valve_id into vhgroups
                let vhid = vhgroups.Max(x => x.history_id)
                from vh in vhgroups
                where vh.history_id == vhid
                let v = vh.valve
                select new Valve()
                {
                    ValveID = v.valve_id,
                    Type = (ValveType)v.valve_type,
                    Manufacturer = v.manufacturer,
                    InternalValveNumber = v.internal_valve_number,
                    InternalBasketNumber = v.internal_basket_number,
                    SerialNumber = v.serial_number,
                    ModelNumber = v.model_number,
                    Size = v.size,
                    Temperature = v.temperature,
                    ConnectionType = v.connection_type,
                    LastServiceDate = v.last_service_date,
                    CustomerID = v.customer_id,
                    Comments = v.comments,

                    AddTime = v.add_time,
                    AddUser = v.add_user,
                    EditTime = v.edit_time,
                    EditUser = v.edit_user,

                    //PSV Specific Fields
                    TopType = v.top_type,
                    SetPressureType = v.set_pressure_type,
                    SetPressure = v.set_pressure,
                    Capacity = v.capactity,
                    InFlangeSize = v.in_flange_size,
                    OutFlangeSize = v.out_flange_size,
                    BackPressureType = v.back_pressure_type,
                    BackPressure = v.back_pressure,
                    CodeSymbol = v.code_symbol,
                    ColdSetPressureType = v.cold_set_pressure_type,
                    ColdSetPressure = v.cold_set_pressure,
                    CRNNumber = v.crn_number,

                    //API Specific Fields
                    Rating = v.rating,
                    BodyMaterial = v.body_material,
                    TrimMaterial = v.trim_material,
                    PartNumber = v.part_number,
                    APIValveType = v.api_valve_type,

                    WorkTickets = (from wt in v.valve_work_tickets
                                   select new ValveWorkTicket()
                                   {
                                       WorkTicketID = wt.work_ticket_id,
                                       Status = (ValveWorkTicketStatus)wt.status,

                                       FullWorkTicket = wt.store.s_store_id + '-' + wt.ticket_number,

                                       AddTime = wt.add_time,

                                       Customer = new Customer()
                                       {
                                           CustomerNumber = wt.customer.c_cust_id,
                                           Name = wt.customer.c_name,
                                       },
                                       SalesOrder = wt.order_id.HasValue ? new SalesOrder()
                                       {
                                           OrderID = wt.sales_order.order_id,
                                           FullOrder = wt.sales_order.store.s_store_id + '-' + wt.sales_order.order + '-' + wt.sales_order.order_ext,
                                       } : new SalesOrder() { FullOrder = string.Empty, OrderID = 0 },
                                   }).ToList(),

                    Customer = new Customer()
                    {
                        Name = v.customer.c_name,
                        CustomerNumber = v.customer.c_cust_id,
                    },
                });

            retData = string.IsNullOrEmpty(sortExpr) ?
                retData.OrderBy(v => v.SerialNumber) :
                retData.OrderBy(sortExpr);

            return retData.ToList();
        }
    }

Currently the valve flag will return false and prevent data from returning if the search parameters are empty. Is there another way to accomplish this without actually calling the function on pageload?

Difficult to say without seeing your ListView aspx code, but don't have to attach a datasource in the ListView itself, you could do it in code behind. If you use it on the aspx page it will always be bound.

I think you have something like this:

<asp:ListView ID="ListView1" runat="server" DataSource="ValvesDataSource"></asp:ListView>

Change that to

<asp:ListView ID="ListView1" runat="server"></asp:ListView>

Code behind

//only bind data after a specific action
protected void Button1_Click(object sender, EventArgs e)
{
    ListView1.DataSource = ValvesDataSource;
    ListView1.DataBind();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM