在子网格视图中分页不起作用(Paging in child grid view not working)

编程入门 行业动态 更新时间:2024-10-26 06:26:32
在子网格视图中分页不起作用(Paging in child grid view not working)

大家好我正在用嵌套gridview做一个程序,我的设计如下

<asp:Panel CssClass="grid" ID="pnlCust" runat="server"> <asp:UpdatePanel ID="pnlUpdate" runat="server"> <ContentTemplate> <asp:GridView AllowPaging="True" ID="gvCustomers" AutoGenerateColumns="False" DataKeyNames="EmpID" runat="server" ShowHeader="true" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" > <AlternatingRowStyle BackColor="White" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Panel CssClass="group" ID="pnlCustomer" runat="server"> <%--<asp:Image ID="imgCollapsible" CssClass="first" ImageUrl="~/Assets/img/plus.png" Style="margin-right: 5px;" runat="server" /><span class="header"> <%#Eval("EmpID")%> : <%#Eval("empname")%>--%> <asp:RadioButton ID="rdbtn" runat="server" onclick="RadioCheck(this);" OnCheckedChanged="radio_changed" Text='<%# Bind("EmpID") %>' AutoPostBack="true"></asp:RadioButton> </asp:Panel> <asp:Panel Style="margin-left: 20px; margin-right: 20px" ID="pnlOrders" runat="server"> <asp:GridView AutoGenerateColumns="false" CssClass="grid" ID="gvOrders" runat="server" PageSize="1" ShowHeader="true" EnableViewState="false" DataKeyNames="EmpID" AllowPaging="true" OnPageIndexChanging="gvOrders_PageIndexChanging"> <RowStyle CssClass="row" /> <AlternatingRowStyle CssClass="altrow" /> <Columns> <asp:BoundField HeaderText="Employee Id" DataField="EmpID"> <ItemStyle HorizontalAlign="Center" Width="100px" /> </asp:BoundField> <asp:TemplateField ItemStyle-CssClass="rownum"> <ItemTemplate> <%# Container.DataItemIndex + 1 %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderStyle-BackColor="#EFF1F1" ItemStyle-HorizontalAlign="Center" ItemStyle-Height="25" HeaderStyle-Width="50" ItemStyle-Width="50"> <HeaderTemplate> <input id="chkBoxAll" type="checkbox" onclick="checkAllBoxes()" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkBoxChild" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="PayPeriodNumber" HeaderText="PayPeriod" HeaderStyle-BackColor="#EFF1F1" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="80" ItemStyle-Width="80"> </asp:BoundField> <asp:BoundField DataField="PayRollYear" HeaderText="Payroll Year" HeaderStyle-BackColor="#EFF1F1" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="100" ItemStyle-Width="100"> </asp:BoundField> <asp:BoundField DataField="PaymentDate" HeaderText="Payment Date" HeaderStyle-BackColor="#EFF1F1" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="110" ItemStyle-Width="110"> </asp:BoundField> </Columns> </asp:GridView> </asp:Panel> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#CCCC99" /> <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> <RowStyle BackColor="#F7F7DE" /> <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#FBFBF2" /> <SortedAscendingHeaderStyle BackColor="#848384" /> <SortedDescendingCellStyle BackColor="#EAEAD3" /> <SortedDescendingHeaderStyle BackColor="#575357" /> </asp:GridView> </ContentTemplate> </asp:UpdatePanel> </asp:Panel>

页面加载上的图像

在选择其中一个单选按钮时,我将显示相应的员工详细信息,如下所示

我已经为gridview写了如下的分页

if (!Page.IsPostBack) { GridViewChildPageIndex(); } private void GridViewChildPageIndex() { DataTable dtPageIndex = new DataTable(); dtPageIndex.Columns.Add("PageIndex", typeof(int)); for (int i = 0; i < gvCustomers.Rows.Count; i++) { dtPageIndex.Rows.Add("0"); } Session["ChildPageIndex"] = dtPageIndex; }

子gridview的寻呼代码如下

protected void gvOrders_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView gvwChild = (GridView)sender; //GridViewRow gvRowParent = ((System.Web.UI.WebControls.GridView)sender).Parent as GridViewRow; GridViewRow gvRowParent = gvwChild.Parent as GridViewRow; gvwChild.PageIndex = e.NewPageIndex; if (Session["ChildPageIndex"] != null) { DataTable dtPageIndex = (DataTable)Session["ChildPageIndex"]; dtPageIndex.Rows[gvRowParent.RowIndex][1] = e.NewPageIndex; // *I am getting error here as Object reference not set to an Object can any one tell where I went wrong* } BindChildGrdView(gvCustomers.DataKeys[gvRowParent.RowIndex].Value.ToString(), gvwChild); }

Stack Trace

at _Default.gvOrders_PageIndexChanging(Object sender, GridViewPageEventArgs e) in d:\Projects\GridView-Collapsible\GridView-Collapsible\Default.aspx.cs:line 54 at System.Web.UI.WebControls.GridView.OnPageIndexChanging(GridViewPageEventArgs e) at System.Web.UI.WebControls.GridView.HandlePage(Int32 newPage) at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) at System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Hi all I am doing a program with a nested gridview my design is as follows

<asp:Panel CssClass="grid" ID="pnlCust" runat="server"> <asp:UpdatePanel ID="pnlUpdate" runat="server"> <ContentTemplate> <asp:GridView AllowPaging="True" ID="gvCustomers" AutoGenerateColumns="False" DataKeyNames="EmpID" runat="server" ShowHeader="true" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" > <AlternatingRowStyle BackColor="White" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Panel CssClass="group" ID="pnlCustomer" runat="server"> <%--<asp:Image ID="imgCollapsible" CssClass="first" ImageUrl="~/Assets/img/plus.png" Style="margin-right: 5px;" runat="server" /><span class="header"> <%#Eval("EmpID")%> : <%#Eval("empname")%>--%> <asp:RadioButton ID="rdbtn" runat="server" onclick="RadioCheck(this);" OnCheckedChanged="radio_changed" Text='<%# Bind("EmpID") %>' AutoPostBack="true"></asp:RadioButton> </asp:Panel> <asp:Panel Style="margin-left: 20px; margin-right: 20px" ID="pnlOrders" runat="server"> <asp:GridView AutoGenerateColumns="false" CssClass="grid" ID="gvOrders" runat="server" PageSize="1" ShowHeader="true" EnableViewState="false" DataKeyNames="EmpID" AllowPaging="true" OnPageIndexChanging="gvOrders_PageIndexChanging"> <RowStyle CssClass="row" /> <AlternatingRowStyle CssClass="altrow" /> <Columns> <asp:BoundField HeaderText="Employee Id" DataField="EmpID"> <ItemStyle HorizontalAlign="Center" Width="100px" /> </asp:BoundField> <asp:TemplateField ItemStyle-CssClass="rownum"> <ItemTemplate> <%# Container.DataItemIndex + 1 %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderStyle-BackColor="#EFF1F1" ItemStyle-HorizontalAlign="Center" ItemStyle-Height="25" HeaderStyle-Width="50" ItemStyle-Width="50"> <HeaderTemplate> <input id="chkBoxAll" type="checkbox" onclick="checkAllBoxes()" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkBoxChild" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="PayPeriodNumber" HeaderText="PayPeriod" HeaderStyle-BackColor="#EFF1F1" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="80" ItemStyle-Width="80"> </asp:BoundField> <asp:BoundField DataField="PayRollYear" HeaderText="Payroll Year" HeaderStyle-BackColor="#EFF1F1" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="100" ItemStyle-Width="100"> </asp:BoundField> <asp:BoundField DataField="PaymentDate" HeaderText="Payment Date" HeaderStyle-BackColor="#EFF1F1" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="110" ItemStyle-Width="110"> </asp:BoundField> </Columns> </asp:GridView> </asp:Panel> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#CCCC99" /> <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> <RowStyle BackColor="#F7F7DE" /> <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#FBFBF2" /> <SortedAscendingHeaderStyle BackColor="#848384" /> <SortedDescendingCellStyle BackColor="#EAEAD3" /> <SortedDescendingHeaderStyle BackColor="#575357" /> </asp:GridView> </ContentTemplate> </asp:UpdatePanel> </asp:Panel>

Image of that on Page load

On selecting one of the radio buttons I will show the corresponding Employee details which is as follows

I have written paging for gridview as follows

if (!Page.IsPostBack) { GridViewChildPageIndex(); } private void GridViewChildPageIndex() { DataTable dtPageIndex = new DataTable(); dtPageIndex.Columns.Add("PageIndex", typeof(int)); for (int i = 0; i < gvCustomers.Rows.Count; i++) { dtPageIndex.Rows.Add("0"); } Session["ChildPageIndex"] = dtPageIndex; }

Paging code of child gridview is as follows

protected void gvOrders_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView gvwChild = (GridView)sender; //GridViewRow gvRowParent = ((System.Web.UI.WebControls.GridView)sender).Parent as GridViewRow; GridViewRow gvRowParent = gvwChild.Parent as GridViewRow; gvwChild.PageIndex = e.NewPageIndex; if (Session["ChildPageIndex"] != null) { DataTable dtPageIndex = (DataTable)Session["ChildPageIndex"]; dtPageIndex.Rows[gvRowParent.RowIndex][1] = e.NewPageIndex; // *I am getting error here as Object reference not set to an Object can any one tell where I went wrong* } BindChildGrdView(gvCustomers.DataKeys[gvRowParent.RowIndex].Value.ToString(), gvwChild); }

Stack Trace

at _Default.gvOrders_PageIndexChanging(Object sender, GridViewPageEventArgs e) in d:\Projects\GridView-Collapsible\GridView-Collapsible\Default.aspx.cs:line 54 at System.Web.UI.WebControls.GridView.OnPageIndexChanging(GridViewPageEventArgs e) at System.Web.UI.WebControls.GridView.HandlePage(Int32 newPage) at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) at System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

最满意答案

这可能会帮到你:在gvOrders_PageIndexChanging函数中,使用

GridViewRow gvRowParent = gvwChild.Parent.Parent as GridViewRow;

并在if条件内

if (Session["ChildPageIndex"] != null) { DataTable dtPageIndex = (DataTable)Session["ChildPageIndex"]; dtPageIndex.Rows[gvRowParent.RowIndex][0] = e.NewPageIndex; // *I am getting error here as Object reference not set to an Object can any one tell where I went wrong* }

还可以调试并检查gvRowParent是否是此函数第二行中的实例化对象。

This may help you out: In the gvOrders_PageIndexChanging function, use

GridViewRow gvRowParent = gvwChild.Parent.Parent as GridViewRow;

And inside the if condition

if (Session["ChildPageIndex"] != null) { DataTable dtPageIndex = (DataTable)Session["ChildPageIndex"]; dtPageIndex.Rows[gvRowParent.RowIndex][0] = e.NewPageIndex; // *I am getting error here as Object reference not set to an Object can any one tell where I went wrong* }

And also could you debug and check if gvRowParent is an instantiated object in the second line of this function.

更多推荐

本文发布于:2023-07-19 02:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1171517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   网格   视图   不起作用   Paging

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!