通过分页嵌套gridview保留可见的控制属性

时间:2015-04-05 13:37:08

标签: c# asp.net gridview

我在网格视图中有一个网格视图。当我单击父网格视图的编辑时,子网格视图显示一个复选框。当我点击子网格视图的下一页按钮时,可见的控件再次变为不可见。这不是我要求的行为。我想通过在父网格视图处于编辑模式时通过分页子网格视图来单击编辑以保持可见的控件。

我的标记:

<asp:GridView 
    ID="grdImages" 
    runat="server"
    AllowPaging="true"
    ShowFooter="true"
    PageSize="5"
    AutoGenerateColumns="false"
    OnPageIndexChanging="grdImages_PageIndexChanging"
    OnRowCancelingEdit="grdImages_RowCancelingEdit"
    OnRowCommand="grdImages_RowCommand"
    OnRowEditing="grdImages_RowEditing"
    OnRowUpdating="grdImages_RowUpdating"
    OnRowDeleting="grdImages_RowDeleting" 
    EmptyDataText="No Data Available at this Time" 
    OnRowDataBound="grdImages_RowDataBound"  
                DataKeyNames="productID" RowStyle-VerticalAlign="Top" RowStyle-HorizontalAlign="Center">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775"></AlternatingRowStyle>        


    <Columns>

        <asp:TemplateField AccessibleHeaderText="Product ID" HeaderText="Product ID" FooterText="Product ID">
            <ItemTemplate>
                <asp:Label ID="lblProdId" runat="server" Text='<%# Eval("ProductId") %>' ></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="lstAddProdId" runat="server"  AppendDataBoundItems="true" >
                    <asp:ListItem>Select a product</asp:ListItem>
                </asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField AccessibleHeaderText="Product Main Image" FooterText="Product Main Image" HeaderText="Product Main Image">
            <ItemTemplate>
                <asp:Label ID="lblMainImgId" runat="server" Text='<%# Eval("ImageId") %>' ></asp:Label> &nbsp;
                <asp:Label ID="lblMainImgName" runat="server" Text='<%# Eval("ImageName") %>' ></asp:Label> <br />
                <asp:Image ID="imgMain" runat="server" Height="250" Width="250" ImageUrl='<%# Eval("ImagePath") %>' />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label runat="server" Font-Bold="true" Text="Current main image" ></asp:Label> <br />
                <asp:Image ID="imgMain" runat="server" Height="250" Width="250" ImageUrl='<%# Eval("ImagePath") %>' /> <br />
                <asp:Label runat="server" Font-Bold="true" Text="Upload a new image to replace the current main image." ></asp:Label> <br />
                <asp:FileUpload ID="flupEditMain" runat="server" />
            </EditItemTemplate>
            <FooterTemplate>
                <asp:FileUpload ID="flupMain" runat="server" AllowMultiple="false" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField AccessibleHeaderText="Supporting Images" FooterText="Supporting Images" HeaderText="Supporting Images">
            <ItemTemplate>
                <asp:Label ID="lblSupImages" runat="server" Visible="false" Font-Bold="true" Text="Select images to delete"></asp:Label><br />
                <asp:GridView 
                    ID="grdSupImages" 
                    runat="server" 
                    ShowHeader="false" 
                    CellPadding="4" 
                    ForeColor="#333333" 
                    GridLines="None" 
                    AutoGenerateColumns="False"                         
                    AllowPaging="true" 
                    PageSize="4"
                    OnPageIndexChanging="grdSupImages_PageIndexChanging"
                    OnRowEditing="grdSupImages_RowEditing"
                    EnableViewState="true"
                    DataKeyNames="productID" 
                    RowStyle-VerticalAlign="Top" 
                    RowStyle-HorizontalAlign="Center" 
                    EmptyDataText="No Supporting Images Found">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775"></AlternatingRowStyle>
                    <Columns>
                        <asp:TemplateField >
                            <ItemTemplate>                                    
                                <asp:Image ID="imgSup" runat="server" ToolTip='<%# Eval("ImgId") %>' AlternateText='<%# Eval("ImageName") %>' ImageUrl='<%# Eval("ImagePath") %>' Height="125" Width="125" />
                                <asp:Label ID="imgSupName" runat="server" Text='<%# Eval("ImageName") %>' AssociatedControlID="imgSup"></asp:Label>
                                <asp:CheckBox ID="chkSupImages" runat="server" Visible="false" Text="Select Image" CommandName="Select" />
                            </ItemTemplate>

                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#999999"></EditRowStyle>

                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></FooterStyle>

                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></HeaderStyle>

                    <PagerStyle HorizontalAlign="Center" BackColor="#284775" ForeColor="White"></PagerStyle>

                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>

                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></SelectedRowStyle>

                    <SortedAscendingCellStyle BackColor="#E9E7E2"></SortedAscendingCellStyle>

                    <SortedAscendingHeaderStyle BackColor="#506C8C"></SortedAscendingHeaderStyle>

                    <SortedDescendingCellStyle BackColor="#FFFDF8"></SortedDescendingCellStyle>

                    <SortedDescendingHeaderStyle BackColor="#6F8DAE"></SortedDescendingHeaderStyle>
                </asp:GridView>
                <div style="clear:both;">&nbsp;</div>
                <asp:Label ID="lblFlupSupImages" runat="server" Font-Bold="true" Text="Add extra images" Visible="false" />
                <br />
                <asp:FileUpload ID="flupSupImages" runat="server" AllowMultiple="true" Visible="false" />
            </ItemTemplate>

            <FooterTemplate>
                <asp:FileUpload ID="flupExtra" runat="server" AllowMultiple="true" />
            </FooterTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Edit">
            <ItemTemplate>
                <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
                <br />
                <span onclick="return confirm('Are you sure you want to delete these images?')">
                    <asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
                </span>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
                <br />
                <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
            </EditItemTemplate>
            <FooterTemplate>
                <asp:Button ID="btnAddRecord" runat="server" Text="Add" CommandName="Add"></asp:Button>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>

    <EditRowStyle BackColor="#999999"></EditRowStyle>

    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></FooterStyle>

    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"></HeaderStyle>

    <PagerStyle HorizontalAlign="Center" BackColor="#284775" ForeColor="White"></PagerStyle>

    <RowStyle BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>

    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"></SelectedRowStyle>

    <SortedAscendingCellStyle BackColor="#E9E7E2"></SortedAscendingCellStyle>

    <SortedAscendingHeaderStyle BackColor="#506C8C"></SortedAscendingHeaderStyle>

    <SortedDescendingCellStyle BackColor="#FFFDF8"></SortedDescendingCellStyle>

    <SortedDescendingHeaderStyle BackColor="#6F8DAE"></SortedDescendingHeaderStyle>
</asp:GridView>

我的代码背后:

protected void grdImages_RowDataBound(object sender, GridViewRowEventArgs e)
{



        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            GridView grd = (GridView)e.Row.FindControl("grdSupImages"); // find controls
            Label prodId = (Label)e.Row.FindControl("lblProdId");
            grd.ToolTip = prodId.Text;
            int product = Convert.ToInt32(prodId.Text);        // assign values to variables.
            BindNestedGrid(product, grd);      // call the function.
        }

        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex == grdImages.EditIndex)
        {
            //Find Child GridView
            GridView supImagesGrd = (GridView)e.Row.Cells[2].FindControl("grdSupImages");
            if (supImagesGrd != null)
            {
                // find grid header label and make visible
                Label gridHead = (Label)e.Row.Cells[2].FindControl("lblSupImages");
                gridHead.Visible = true;
                // find fileupload header label and make visible.
                Label fileUpHead = (Label)e.Row.Cells[2].FindControl("lblFlupSupImages");
                fileUpHead.Visible = true;
                // find fileupload control and make it visible.
                FileUpload flup = (FileUpload)e.Row.Cells[2].FindControl("flupSupImages");
                flup.Visible = true;
                //Loop through the GridView 
                foreach (GridViewRow row in supImagesGrd.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        //Find the CheckBox
                        CheckBox chb = (CheckBox)row.Cells[0].FindControl("chkSupImages");
                        if (chb != null)
                        {
                            chb.Visible = true;
                        }
                    }
                }
            }
        }


}
protected void grdSupImages_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

    GridView gv = (GridView)sender;        
    gv.PageIndex = e.NewPageIndex;        
    BindNestedGrid(Convert.ToInt32(gv.ToolTip), gv);        

}

如果需要更多信息或代码,请告知我们。

1 个答案:

答案 0 :(得分:0)

对于任何想要了解的人,我在单击编辑按钮时禁用了子网格的分页,因此所有复选框现在都显示在子网格的所有行上。