简体   繁体   中英

Page Load not firing when input control placed in an asp GridView

I have a gridview which displays product information on the index page of my site for a user. I want to extend this to allow the user to tick a checkbox if they have reviewed a product.

However, after adding the check box column into my gridview template, when i try to search multiple times the Page_Load event of my index page stops firing, this causes an issue as some of the events further down the execution tree require the object that is initialised at page load.

The problem seems to be that placing any asp input control inside the gridview is somehow preventing Page_Load from firing before the DataSources OnSelecting and OnSelected and the Grids OnRowDataBound events but i can't see why.

Here is my sample code, I can't see what I'm doing wrong here.

Index.aspx.cs

private ProductSearch productSearch

protected void Page_Load(object sender, EventArgs e)
{
    productSearch = new ProductSearch(GetSearchParameters());
    productSearch.PageLoad()
}

protected void ProductsSelecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    e.InputParameters["searchParams"] = productSearch.GetSearchParams();
}

protected void ProductsSelected(object sender, ObjectDataSourceStatusEventArgs e)
{
    productSearch.SetExportToCsvButton();
}

protected void ProductsPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    dgProducts.PageIndex = e.NewPageIndex;
}

protected void ProductsOnRowDataBound(object sender, GridViewRowEventArgs e)
{
    productSearch.ProductsRowDataBound(e.Row);
}

Index.aspx

<%@ Page Language="C#" MasterPageFile="~/Admin/AdminMaster.master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Web.Admin.Index" %>
<asp:Content ContentPlaceHolderID="DataFrame" runat="server">
    <asp:GridView ID="dgProducts" runat="server" AllowPaging="True" AllowSorting="True" EnableViewState="False"
            AutoGenerateColumns="False" DataSourceID="dsProducts" PagerSettings-Position="TopAndBottom" DataKeyNames="ProductNo, KitID"
            OnPageIndexChanging="ProductsPageIndexChanging" OnRowDataBound="ProductsOnRowDataBound"
            EmptyDataText="There are no products matching your search." meta:resourcekey="dgProducts" onrowcreated="ProductsRowCreated">
        <HeaderStyle Font-Size="Small" />
        <Columns>
            <asp:TemplateField HeaderText="Reviewed">
                <ItemTemplate>                        
                    <asp:CheckBox runat="server" ID="chkReviewed" class="reviewedCheckbox" Checked="False" />                        
                </ItemTemplate>
            </asp:TemplateField>
        </Columns
    </asp:GridView>
    <asp:ObjectDataSource ID="dsProducts" runat="server" EnablePaging="True" SelectMethod="ProductAndKitSearchByParams"
            TypeName="ProductSearchController.ProductSearch" onselecting="ProductsSelecting" SortParameterName="SortParameter"
            SelectCountMethod="SelectVirtualCount" OnSelected="ProductsSelected">
        <SelectParameters>
            <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="searchParams" Type="Object" />
        </SelectParameters>
    </asp:ObjectDataSource>
</asp:Content>

Check you master page contentplaceholderid and provide it in content page like this:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"></asp:Content>

and also check your content page codebehind file name should be same as mention in page directives.

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