C# 更改页面后gridview将消失

C# 更改页面后gridview将消失,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个gridview,它应该分为多个页面,但问题是,当我更改页面时,整个gridview正在消失,我尝试了在internet上找到的所有内容,但没有解决方案,这里是我的代码 <asp:GridView ID="ExistContents" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="true" PageSi

我有一个gridview,它应该分为多个页面,但问题是,当我更改页面时,整个gridview正在消失,我尝试了在internet上找到的所有内容,但没有解决方案,这里是我的代码

<asp:GridView ID="ExistContents" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"
    AllowPaging="true" PageSize="5"    OnPageIndexChanging="ExistContents_PageIndexChanging" >
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:BoundField DataField="ContentID" HeaderText="id" />
        <asp:ImageField DataImageUrlField="TmpFilename" HeaderText="Image">
            <ControlStyle Height="64px" Width="96px" />
        </asp:ImageField>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Description" HeaderText="Description" />
        <asp:BoundField DataField="Type" HeaderText="Type" />
        <asp:BoundField DataField="ContentID" HeaderText="id" Visible="false" ShowHeader="false" />
        <asp:TemplateField HeaderText="Delete">
            <ItemTemplate>
                <asp:UpdatePanel runat="server">
                    <ContentTemplate>
                        <asp:CheckBox runat="server" ID="ChBox1"  OnCheckedChanged="ExistContents_CheckedChanged" AutoPostBack="True"/>
                    </ContentTemplate>
                </asp:UpdatePanel>

            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"  CssClass="header"/>
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

下面是事件处理程序

protected void ExistContents_PageIndexChanging(object sender, GridViewPageEventArgs e)
{        
    ExistContents.PageIndex = e.NewPageIndex;

    List<CONTENT> panier;
    panier = (List<CONTENT>)Session["PANIER"];
    ExistContents.DataSource = panier;
    ExistContents.DataBind();
}
protectedvoid ExistContents\u PageIndexChanging(对象发送方,GridViewPageEventArgs e)
{        
ExistContents.PageIndex=e.NewPageIndex;
列表窗格;
窗格=(列表)会话[“窗格”];
ExistContents.DataSource=panier;
ExistContents.DataBind();
}
您应该处理该事件


设置的目的是为您提供在执行分页之前取消分页的选项。

以下是此问题的答案,事实上,我的数据源不是会话,而是linq查询,因此我对其进行了更改,它工作得非常好

这是我的新代码

        protected void ExistContents_CheckedChanged(object sender, EventArgs e)
    {

        CheckBox chk = (CheckBox)sender;
        GridViewRow gr = (GridViewRow)chk.Parent.Parent.Parent.Parent;
        int id= Convert.ToInt32(gr.Cells[0].Text);
        if (chk.Checked)
            AddToCaddy(id, "DELETE");
        else
            DeleteFromCaddy(id);

        UpdatePanel.DataBind();
        UpdatePanel.Update();
    }
仅供参考,如果您正在页面中对gridview进行数据绑定,请在
中加载如果(!IsPostBack)
则需要重新查找数据源,但如果您没有,则只需要使用这些函数

UpdatePanel.DataBind();
UpdatePanel.Update()


我希望这有助于

在您的GV Showheader中设置属性Henempty=“true”发生了什么,当您更改页面时,它是否有数据?它只显示标题,没有数据行,但我找到了问题的解决方案。事实上,这是我的错误,我应该在eventhandler中进行linq查询,作为我的viewgrid的数据源。谢谢您的帮助。正如我所说,我在我的评论中找到了答案,但无论如何,谢谢您。请将答案发布到你自己的问题,以便其他人也能从中受益。