ASP.Net 3.5 AjaxControlToolkit主要在UpdatePanel中的CollapsiblePanelExpander部分回发困境

时间:2011-08-08 19:46:41

标签: asp.net vb.net collapsiblepanelextender

长时间读者第一次海报

我有一个链接到母版页的内容页面。主人有toolkitscriptmanager。在内容页面上,我在父转发器的项模板中有一个嵌套转发器,我有一个用于子转发器控件的CollapsiblePanelExtender。在部分回发时,如果未设置折叠属性,则展开所有面板。如果collapsed属性设置为true,则部分回发时将折叠所有面板。

转发器的数据绑定发生在后面代码的page_init部分,我通过迭代page_load事件中的项来检查面板的状态。似乎没有任何工作,在调试代码时,我确实看到属性被适当设置,但是当在浏览器中呈现时,所有面板都是打开的。触发部分回发的编辑事件在modalpopup中执行。

我试图将面板移动到他们自己的更新面板中,删除更新面板,将模式放在更新面板之外无济于事。不维护ViewState。

页码:

<asp:Repeater ID="rptrConsultants" runat="server" OnItemCommand="rptrConsultants_ItemCommand" OnItemDataBound="rptrConsultants_ItemDataBound">
                            <HeaderTemplate>
                                <div class="pageTitle">
                                    Your Consultants (Click on the image beside the consultant to view their group memberships)
                                    <br />
                                    Official names are listed. Expanding the consultant will show the name assigned and the related groups.
                                </div>
                                <br />
                                <table>
                                    <tr>
                                        <td style="width: 60px; font-weight: bold; text-align: center;">
                                            Actions
                                        </td>
                                        <td style="font-weight: bold;">
                                            Consultant information
                                        </td>
                                    </tr>
                            </HeaderTemplate>
                            <FooterTemplate>
                                </table>
                                <asp:Label ID="lblNoConsultant" runat="server" Text="You have not created any consultants" Visible="false"></asp:Label>
                            </FooterTemplate>
                            <ItemTemplate>
                                <tr>
                                    <td style="width: 60px; text-align: center;">
                                        <asp:Image ID="imgConsultantExpander" runat="server" ToolTip="Click here to toggle consultant details" AlternateText="Toggle Consultant Details" />
                                        <asp:ImageButton ID="btnDeleteConsultant" AlternateText="Delete Consultant" runat="server" CausesValidation="false" CommandName="DeleteConsultant" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "IndividualsID")%>' ImageUrl="~/Styles/Images/Icons/remove_consultant.png" ToolTip="Delete consultant" />
                                        <asp:ImageButton ID="btnAddConsultantToNewGroup" AlternateText="Add consultant to a new group" runat="server" CommandName="AddConsultantToGroup" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "IndividualsID")%>' ImageUrl="~/Styles/Images/Icons/add_user_to_group.png" ToolTip="Add consultant to a new group" />
                                    </td>
                                    <td>
                                        <%#DataBinder.Eval(Container.DataItem, "Name")%>
                                        (<%#DataBinder.Eval(Container.DataItem, "UserID") %>)
                                        <asp:Label ID="lblGroupCount" runat="server" />
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <asp:Panel ID="pnlConsultantExpander" runat="server">
                                            <asp:Repeater ID="rptrConsultantGroups" runat="server" DataSource='<%#Container.DataItem.Row.GetChildRows("ConsultantGroupRelation") %>' OnItemCommand="rptrConsultantGroups_ItemCommand">
                                                <HeaderTemplate>
                                                    <table style="margin-left: 50px;">
                                                        <tr>
                                                            <td style="width: 40px; text-align: center; font-weight: bold;">
                                                                Actions
                                                            </td>
                                                            <td style="font-weight: bold;">
                                                                Membership
                                                            </td>
                                                            <td style="font-weight: bold;">
                                                                Nick Name
                                                            </td>
                                                        </tr>
                                                </HeaderTemplate>
                                                <ItemTemplate>
                                                    <tr>
                                                        <td style="width: 40px;">
                                                            <asp:ImageButton ID="btnEditConsultant" AlternateText="Edit Consultant" runat="server" CommandName="EditConsultantInGroup" CommandArgument='<%#Container.DataItem("GroupID") & "|" & Container.DataItem("GroupMembershipID")%>' ImageUrl="~/Styles/Images/Icons/edit_consultant.png" />
                                                            <asp:ImageButton ID="btnDeleteConsultant" AlternateText="Remove from group" runat="server" CommandName="DeleteConsultantFromGroup" CommandArgument='<%#Container.DataItem("GroupID") & "|" & Container.DataItem("GroupMembershipID")%>' ImageUrl="~/Styles/Images/Icons/delete_fromgroup.png" ToolTip="Remove from group" />
                                                        </td>
                                                        <td class="textbold">
                                                            <%#Container.DataItem("GroupName")%>
                                                        </td>
                                                        <td class="textitalic">
                                                            <%#Container.DataItem("nickname")%>
                                                        </td>
                                                    </tr>
                                                </ItemTemplate>
                                                <FooterTemplate>
                                                    </table>
                                                </FooterTemplate>
                                            </asp:Repeater>
                                        </asp:Panel>
                                        <asp:CollapsiblePanelExtender ID="cpepnlConsultantExpander" runat="server" TargetControlID="pnlConsultantExpander" CollapseControlID="imgConsultantExpander" ExpandControlID="imgConsultantExpander" CollapsedImage="~/Styles/Images/Icons/user_info.png" ExpandedImage="~/Styles/Images/Icons/user_open.png" ImageControlID="imgConsultantExpander" EnableViewState="true" CollapsedSize="0" />
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:Repeater>

然后是page_load中出现的代码隐藏方法:

Private Sub setCollapsiblePanelsInRepeater(ByVal rptr As Repeater)
    For Each item As RepeaterItem In rptr.Items
        If ((item.ItemType = ListItemType.AlternatingItem) Or (item.ItemType = ListItemType.Item)) Then
            For Each ctl As Control In item.Controls
                If (TypeOf ctl Is AjaxControlToolkit.CollapsiblePanelExtender) Then
                    Dim cpe As AjaxControlToolkit.CollapsiblePanelExtender = DirectCast(ctl, AjaxControlToolkit.CollapsiblePanelExtender)
                    If (Not IsPostBack()) Then
                        cpe.Collapsed = True
                        cpe.ClientState = "true"
                    Else
                        Dim isCollapsed As Boolean
                        If (Request.Form(cpe.UniqueID & "_ClientState") IsNot Nothing) Then
                            isCollapsed = (Request.Form(cpe.UniqueID & "_ClientState").ToString() = "true")
                        Else
                            isCollapsed = True
                        End If
                        If (isCollapsed) Then
                            cpe.ClientState = "true"
                            cpe.Collapsed = True
                        Else
                            cpe.ClientState = "false"
                            cpe.Collapsed = False
                        End If
                    End If
                End If
            Next
        End If
    Next
End Sub

请注意,初始加载事件有效!这些面板在页面加载时都已折叠

另请注意,转发器嵌套在tabPanel

提前感谢您的回答!

抱歉长篇大论的解释

编辑:

更新

尝试了Javascript路线 - 仍无济于事

    pageLoad = function()
    {
            CheckStatusOfPanels();
    };

    function CheckStatusOfPanels()
    {
        var allBehaviors = Sys.Application.getComponents();
        for (var loopIndex = 0; loopIndex < allBehaviors.length; loopIndex++)
        {
            currentBehavior = allBehaviors[loopIndex];
            if (currentBehavior._name && currentBehavior.get_name() == 'CollapsiblePanelBehavior')
            {
                var myID = currentBehavior.get_id() + '_ClientState';
                var isCollapsed = document.getElementById(myID).value
                if (isCollapsed == 'true')
                {
                    currentBehavior.expandPanel();
                    currentBehavior._ClientState = isCollapsed;
                }
                else
                {
                    currentBehavior.collapsePanel();
                    currentBehavior._ClientState = isCollapsed;
                }
            }
        }
    }

**更新#2 **

My Rep低于100,所以我必须等待8小时才能回答我自己的问题:

这是我几乎整天工作后的“修复”:

好的,终于找到了解决方案。在这里:

使用我原始帖子中发布的SetCollapsiblePanelsInRepeater方法,我在updatePanel预呈现方法中添加了对此方法的调用,而现在这些面板保留了状态....

Protected Sub updtpnlMain_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles updtpnlMain.PreRender
    setCollapsiblePanelsInRepeater(rptrGroups)
    setCollapsiblePanelsInRepeater(rptrConsultants)

End Sub

对于其他人的清晰度谷歌搜索此问题,设置面板事件的方法是:

Private Sub setCollapsiblePanelsInRepeater(ByVal rptr As Repeater)
    For Each item As RepeaterItem In rptr.Items
        If ((item.ItemType = ListItemType.AlternatingItem) Or (item.ItemType = ListItemType.Item)) Then
            For Each ctl As Control In item.Controls
                If (TypeOf ctl Is AjaxControlToolkit.CollapsiblePanelExtender) Then
                    Dim cpe As AjaxControlToolkit.CollapsiblePanelExtender = DirectCast(ctl, AjaxControlToolkit.CollapsiblePanelExtender)
                    If (Not IsPostBack()) Then
                        cpe.Collapsed = True
                        cpe.ClientState = "true"
                    Else
                        Dim isCollapsed As Boolean
                        If (Request.Form(cpe.UniqueID & "_ClientState") IsNot Nothing) Then
                            isCollapsed = (Request.Form(cpe.UniqueID & "_ClientState").ToString() = "true")
                        Else
                            isCollapsed = True
                        End If
                        If (isCollapsed) Then
                            cpe.ClientState = "true"
                            cpe.Collapsed = True
                        Else
                            cpe.ClientState = "false"
                            cpe.Collapsed = False
                        End If
                    End If
                End If
            Next
        End If
    Next
End Sub

2 个答案:

答案 0 :(得分:1)

母版页上的Toolkitscript管理器会导致实际问题。您可以在单个内容页面上找到您的解决方案。

您还确定已在此类内容页面中启用了viewstate

<%@ Page EnableViewState="true" Language="C#" AutoEventWireup="true" CodeFile="myfile.aspx.cs"
    Inherits="myfile" %>

这适用于C#

答案 1 :(得分:0)

好的,终于找到了解决方案。在这里:

使用我原始帖子中发布的SetCollapsiblePanelsInRepeater方法,我在updatePanel预呈现方法中添加了对此方法的调用,而现在这些面板保留了状态....

Protected Sub updtpnlMain_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles updtpnlMain.PreRender
    setCollapsiblePanelsInRepeater(rptrGroups)
    setCollapsiblePanelsInRepeater(rptrConsultants)

End Sub

对于其他人的清晰度谷歌搜索此问题,检查面板状态并折叠/展开它们的方法是:

Private Sub setCollapsiblePanelsInRepeater(ByVal rptr As Repeater)     对于每个项目作为RepeaterItem在rptr.Items中         If((item.ItemType = ListItemType.AlternatingItem)或(item.ItemType = ListItemType.Item))然后             对于每个ctl As Control in item.Controls                 如果(TypeOf ctl是AjaxControlToolkit.CollapsiblePanelExtender)那么                     Dim cpe As AjaxControlToolkit.CollapsiblePanelExtender = DirectCast(ctl,AjaxControlToolkit.CollapsiblePanelExtender)                     如果(Not IsPostBack())那么                         cpe.Collapsed = True                         cpe.ClientState =“true”                     其他                         Dim isCollapsed As Boolean                         如果(Request.Form(cpe.UniqueID&amp;“_ClientState”)IsNot Nothing)那么                             isCollapsed =(Request.Form(cpe.UniqueID&amp;“_ClientState”)。ToString()=“true”)                         其他                             isCollapsed = True                         万一                         如果(isCollapsed)那么                             cpe.ClientState =“true”                             cpe.Collapsed = True                         其他                             cpe.ClientState =“false”                             cpe.Collapsed = False                         万一                     万一                 万一             下一个         万一     下一个 结束子