无法访问Ajax Accordion Pane的内容

时间:2015-07-07 16:37:47

标签: c# asp.net ajax accordion

我在Accordion中使用了HiddenField,而在下面的窗格中使用了另一个Accordion

<asp:Accordion ID="accFuncPerm" runat="server" HeaderCssClass="accheaderfun"
    ContentCssClass="acccontfun" OnItemDataBound="accFuncPerm_ItemDataBound"
    RequireOpenedPane="false" SelectedIndex="-1">
    <headertemplate>
            <%#DataBinder.Eval(Container.DataItem, "nmFuncionalidade")%>
        </headertemplate>
    <contenttemplate>
            <asp:HiddenField ID="hfIDF" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "idFuncionalidade")%>' />
            <asp:Accordion ID="accPagPerm" runat="server" HeaderCssClass="accheader" ContentCssClass="acccont"
                RequireOpenedPane="false" SelectedIndex="-1" HeaderSelectedCssClass="accheadersel" 
                OnItemCommand="accPagPerm_OnItemCommand" SuppressHeaderPostbacks="false" OnItemDataBound="accPagPerm_ItemDataBound">
                <HeaderTemplate>
                    <asp:LinkButton runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "nmPagina")%>'  
                        CommandName="Select" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "idPagina")%>' Width="100%" />                                    
                </HeaderTemplate>
                <ContentTemplate>
                    <asp:HiddenField ID="hfID" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "idPagina")%>' />
                    <asp:Table ID="tblGrupoAcaoPagina" runat="server" CssClass="gv" GridLines="Both">
                        <asp:TableHeaderRow ID="tblHeader" runat="server">
                        </asp:TableHeaderRow>
                    </asp:Table>
                </ContentTemplate>
            </asp:Accordion>
        </contenttemplate>
</asp:Accordion>

PostBack 上,我访问其选定的窗格:

AccordionPane pane = accFuncPerm.Panes[accFuncPerm.SelectedIndex];

但是,当我试图找到它的控件时,它失败了。

Accordion accPagPerm = (Accordion)pane.FindControl("accPagPerm");
pane = accPagPerm.Panes[int.Parse(txtAcc.Text)];
HiddenField hf = (HiddenField)pane.FindControl("hfID");

我找到的面板中包含空内容, hiddenfield null

有谁知道原因,请给我一个解决方案吗?

1 个答案:

答案 0 :(得分:0)

Accordion在控件中有两个AccordionContentPanel个实例,一个用于标题,另一个用于内容。内容AccordionContentPanel包含您要查找的值:

protected void accFuncPerm_ItemCommand(object sender, CommandEventArgs e)
{
    AccordionContentPanel acpContent;
    AccordionPane aPane = accFuncPerm.Panes[accFuncPerm.SelectedIndex];
    Control[] controls;

    //Find accPagPerm
    controls = new Control[aPane.Controls.Count];
    aPane.Controls.CopyTo(controls, 0);
    acpContent = (AccordionContentPanel)controls.Single(c => c.FindControl("accPagPerm") != null);
    Accordion accPagPerm = (Accordion)acpContent.FindControl("accPagPerm");

    //Find hfID
    aPane = accPagPerm.Panes[int.Parse(txtAcc.Text)];
    controls = new Control[aPane.Controls.Count];
    aPane.Controls.CopyTo(controls, 0);
    acpContent = (AccordionContentPanel)controls.Single(c => c.FindControl("hfID") != null);
    HiddenField hf = (HiddenField)acpContent.FindControl("hfID");
}

您需要检查空值并确保数据类型符合预期,但这样可以帮助您入门。

旁注:你可以删除controls变量,但是你需要计算内容AccordionContentPanel的ID。它似乎遵循&lt; Accordion.ID&gt; _Pane_ &lt; PaneIndex&gt; _content格式,其中&lt; Accordian.ID&gt; 是id拥有该面板的Accordion&lt; PaneIndex&gt; Accordion的{​​{1}}属性中面板的索引。