【问题标题】:CollapsiblePanelExtender in nested Repeater and Gridview setup doesn't function嵌套中继器和 Gridview 设置中的 CollapsiblePanelExtender 不起作用
【发布时间】:2012-01-06 17:33:27
【问题描述】:

我有一个使用三层嵌套的设置:一个中继器,其中的项目使用 CollapsiblePanelExtenders(可以工作),并且每个都包含一个 GridView。然后,它们中的每一个都包含另一个由另一个 CollapsiblePanelExtender 控制的 GridView。仅当我将 clientState 设置为 True 时,这些内部 CollapsiblePanel 才会有效地显示展开或折​​叠状态。但是,它们并没有像预期的那样有效地扩展或崩溃。一切都是动态绑定的。

这是标记...

<asp:Repeater ID="cat_repeater" runat="server">
  <ItemTemplate>
    <asp:CollapsiblePanelExtender id="cat_cpExt" runat="server" TargetControlID="cat_pnl" CollapsedSize="0" Collapsed="false" CollapsedImage="collapsed.png" ExpandedImage="expanded.png" ExpandControlID="cpControl_pnl" CollapseControlID="cpControl_pnl" TextLabelID="cpControl_lbl" ImageControlID="cpControl_img" CollapsedText='<%#configCPText(eval("Title"), False)%>' ExpandedText='<%#configCPText(eval("Title"), True) %>' />
    <asp:Panel ID="cpControl_pnl" runat="server" Visible='<%#itemVisible(eval("ID"), "Recipients", "CategoryID") %>' CssClass="CPanelStyle">
      <asp:Image ID="cpControl_img" runat="server" ImageUrl="expanded.png" />
      <asp:Label ID="cpControl_lbl" runat="server" Text='<%#configCPText(eval("Title"), True) %>' CssClass="CPanelText" />
    </asp:Panel>
    <asp:Panel ID="cat_pnl" runat="server">
      <asp:GridView ID="recipients_gv" runat="server" CssClass="GVStyle" HeaderStyle-CssClass="GVHeaderStyle" RowStyle-CssClass="GVItemStyle" AutoGenerateColumns="false" GridLines="none" AllowPaging="false">
        <Columns>
          <asp:TemplateField HeaderText="Name" SortExpression="Last Name" ItemStyle-CssClass="GVNameStyle">
            <ItemTemplate>
              <asp:Literal id="name_lit" runat="server" text='<%#formatNameText(eval("FirstName"), eval("LastName")) %>' />
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Gifts" ItemStyle-Width="500px">
            <ItemTemplate>
              <asp:CollapsiblePanelExtender id="gifts_cpExt" runat="server" TargetControlID="gifts_pnl" CollapsedSize="0" Collapsed="true" CollapsedImage="collapsed.png" ExpandedImage="expanded.png" ExpandControlID="cpControl_pnl2" CollapseControlID="cpControl_pnl2" TextLabelID="cpControl_lbl2" ImageControlID="cpControl_img2" CollapsedText='<%#configGiftsCPText(eval("ID"), True)%>' ExpandedText='<%#configGiftsCPText(eval("ID"), False) %>' />
              <asp:Panel ID="cpControl_pnl2" runat="server" Visible='<%#itemVisible(eval("ID"), "Gifts", "RecipientID") %>'>
                <asp:Image ID="cpControl_img2" runat="server" ImageUrl="collapsed.png" />
                <asp:Label ID="cpControl_lbl2" runat="server" Text='<%#configGiftsCPText(eval("ID"), False) %>' />
              </asp:Panel>
              <asp:Panel ID="gifts_pnl" runat="server">
                <asp:GridView ID="gifts_gv" runat="server" DataKeyNames="ID" RowStyle-CssClass="GVInnerItemStyle" HeaderStyle-CssClass="GVInnerHeaderStyle" Gridlines="None" AutoGenerateColumns="false" AllowPaging="false" Width="475px">
                  <Columns>
                    <asp:TemplateField ItemStyle-CssClass="GVInnerButtonItemStyle" HeaderText="Description">
                      <ItemTemplate>
                        <asp:LinkButton ID="gift_lBtn" runat="server" Text='<%#eval("Description") %>' CommandName="Select" />
                      </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Complete" ItemStyle-Width="50px">
                      <ItemTemplate>
                        <asp:CheckBox ID="giftComplete_cbx" runat="server" Checked='<%#eval("Complete") %>' />
                      </ItemTemplate>
                    </asp:TemplateField>
                  </Columns>
                </asp:GridView>
              </asp:Panel>
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>
    </asp:Panel>
    <br />
  </ItemTemplate>
</asp:Repeater>

...这是绑定所有内容的代码:

Protected Sub bindCategories()

    Try
        oCmd.Connection = oConn
        oCmd.CommandType = CommandType.Text

        strSQL = "SELECT * FROM Categories"

        oCmd.CommandText = strSQL
        oDA.SelectCommand = oCmd

        oDA.Fill(oDTbl)

        cat_repeater.DataSource = oDTbl
        cat_repeater.DataBind()

        For i As Integer = 0 To oDTbl.Rows.Count - 1


            oCmd.Parameters.Clear()
            inner_dTbl.Clear()

            strSQL = "SELECT *, Title FROM Recipients INNER JOIN Categories on Recipients.CategoryID = Categories.ID WHERE CategoryID = @CategoryID ORDER BY LastName"

            oParam = New SqlParameter
            oParam.ParameterName = "CategoryID"
            oParam.SqlDbType = SqlDbType.Int
            oParam.Value = oDTbl.Rows(i)("ID")
            oCmd.Parameters.Add(oParam)

            oCmd.CommandText = strSQL
            oDA.SelectCommand = oCmd

            oDA.Fill(inner_dTbl)

            Dim gv As GridView = CType(cat_repeater.Items(i).FindControl("recipients_gv"), GridView)
            gv.DataSource = inner_dTbl
            gv.DataBind()

            For j As Integer = 0 To inner_dTbl.Rows.Count - 1

                oCmd.Parameters.Clear()
                gifts_dTbl.Clear()

                strSQL = "SELECT * FROM Gifts WHERE RecipientID = @RecipientID ORDER BY Description"

                oParam = New SqlParameter
                oParam.ParameterName = "RecipientID"
                oParam.SqlDbType = SqlDbType.Int
                oParam.Value = inner_dTbl.Rows(j)("ID")
                oCmd.Parameters.Add(oParam)

                oCmd.CommandText = strSQL
                oDA.SelectCommand = oCmd

                oDA.Fill(gifts_dTbl)

                Dim inner_gv As GridView = CType(gv.Rows(j).FindControl("gifts_gv"), GridView)
                inner_gv.DataSource = gifts_dTbl
                inner_gv.DataBind()

                Dim cpExt As CollapsiblePanelExtender = CType(gv.Rows(j).FindControl("gifts_cpExt"), CollapsiblePanelExtender)
                cpExt.Collapsed = True
                cpExt.ClientState = True

            Next

        Next
    Catch ex As Exception
        Throw ex
    End Try

End Sub

我已经在 2 级嵌套 GridView 设置中成功使用了 CollapsiblePanelExtenders,之前没有问题,并且无需设置 clientState。但是,之前在使用带有中继器的 CollapsiblePanelExtenders 时,我必须指定 clientState。

是否有人对为什么这些扩展器在这个 3 级嵌套设置中不起作用有任何意见?

【问题讨论】:

    标签: asp.net gridview nested repeater collapsiblepanelextender


    【解决方案1】:

    看起来需要它的原因是因为它需要回发才能使所有逻辑正常工作:

    if (this.SupportsClientState) {
      ScriptManager.RegisterHiddenField(this, this.ClientStateFieldID, this.SaveClientState());
      this.Page.RegisterRequiresPostBack(this);
    }
    

    这是来自 AjaxControlToolkit 空间中的public class ScriptControlBase

    您是否将其包装在 UpdatePanel 中?是否会导致回发使用转发器中的 CPE?自从我尝试在中继器中进行 CPE 以来已经有一段时间了,此时我还没有准备好检查代码并构建一个。

    您是否有理由不能只使用完全客户端的东西并使用 javascript 展开/折叠它们?当这些东西扩展时,您是否在服务器端动态加载数据?

    【讨论】:

    • 你知道它当前是否在转发器 CPE 上进行回发吗?
    • 此时我还没有把它放在一个 UpdatePanel 中(我计划至少将内部 GridView 包装在一个中,以便我计划添加更多功能。)在转发器中使用 CPE不会导致回发,但 CPE 具有回发意识。我的所有数据——正如我在我的代码中展示的——被动态加载到使用 CPE 的控件中。我可以尝试使用 CPE 的客户端方法的 JavaScript 方法。我明天试试看。
    • 如果你不这样做,那么只需看看使用一些 jQuery 并切换那些 id,但这可能会更棘手。我明天白天应该上班和下班,如果你想在这里联系我,我可以看看我是否可以在网上见到你(比如在Stack Overflow聊天室?)。我是美国中部时间(所以 GMT - 6 是吗?)。让我知道,很高兴在我可以帮助的地方进行调试。
    • 事实上,我不相信我可以使用 CPE 的客户端方法或 jQuery,因为 CPE 是在绑定外部 Gridview 时动态生成的;可以生成任意数量的它们。我很乐意与您一起调试;不过,我是东部标准时间。
    • 事实上,我确实打算让您尝试用其他东西替换 CPE 功能。让它躺到明天;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多