动态嵌套列表视图用户控件尝试在绑定之前重新填充回发

时间:2013-11-22 15:19:14

标签: c# asp.net listview user-controls collapsiblepanelextender

有很多问题。本质上是试图提高非工作片的性能。该部分由我继承的嵌套动态列表视图设置组成。它结合了许多级别的用户控件(当前不是动态的),每个级别都显示一个嵌套的Listview。

此图像提供了有关结构的概念,但此处显示的级别下方还有更多用户控件。 (敏感数据)这个问题非常缓慢的原因是原始代码是在每个用户控件创建时使用每个级别的相当复杂的查询查询数据库而创建的。因此,在页面加载时,会发出大量查询。

我正在尝试在初始展开CollapsiblePanelExtender标头时动态加载每个用户控件,但是对于较低级别的更新面板和ViewState存在一些问题。

当我尝试从会话中重新生成动态添加的控件时,数据绑定问题发生,并且正在通过级别传递的字段尚未绑定,或者未通过viewstate重新填充,或者OnInit方法无法找到列出要查看的项目,因为它们尚未绑定!

帮助!

顶级.ascx代码

 <asp:ListView ID="fundHoldingListView" runat="server" OnDataBinding="fundHoldingListView_OnDataBinding" OnItemDataBound="fundHoldingListView_OnItemDataBound" OnItemCreated="OnItemCreated">
        <LayoutTemplate>
            <ul class="li-blank collapsible-panel-container">
                <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
            </ul>
        </LayoutTemplate>
        <ItemTemplate>
            <ajaxToolkit:CollapsiblePanelExtender
                ID="yearlyCollapsableExtender"
                runat="server"
                TargetControlID="yearlyCollapsablePanel"
                ExpandControlID="yearlyPanel"
                CollapseControlID="yearlyPanel"
                Collapsed="True" />
            <li class="li-selectable collapsible-panel">
                <asp:Panel runat="server" ID="yearlyPanel">
                    <h3>
                        <asp:LinkButton ID="linkButton1" runat="server" OnClick="ButtonOnClick" Text='<%# Eval("Value") %>'></asp:LinkButton>
                    </h3>
                </asp:Panel>
            </li>
            <asp:HiddenField ID="yearHiddenField" runat="server" Value='<%# Eval("Value") %>' />
            <asp:Panel runat="server" ID="yearlyCollapsablePanel">
                <asp:UpdatePanel ID="UpdatePanelTimeLineitem" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server"  EnableViewState="True">
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="linkButton1" />
                    </Triggers>
                    <ContentTemplate>
                        <asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
                    </ContentTemplate>
                </asp:UpdatePanel>
                <%--   <custom:MonthlyHoldings ID="yearUserControl" runat="server" Year='<%# Eval("Key") %>' MonthlyClientId="<%# ClientIdAutoCompleteLabel.Text %>">
                        </custom:MonthlyHoldings>--%>
            </asp:Panel>
        </ItemTemplate>
    </asp:ListView>

背后的代码

/// <summary>
    /// The time line.
    /// </summary>
    public partial class TimeLine : UserControl
    {
        /// <summary>
        /// The control list id.
        /// </summary>
        private const string ControlListId = "CONTROL_LIST";

        /// <summary>
        /// Gets or sets the dynamic controls.
        /// </summary>
        public Dictionary<string, string> DynamicControls
        {
            get
            {
                return (Dictionary<string, string>)Session[ControlListId];
            }

            set
            {
                this.Session[ControlListId] = value;
            }
        }

        /// <summary>
        /// The on init.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!this.IsPostBack)
            {
                this.DynamicControls = new Dictionary<string, string>();
            }
            else
            {
                var controls = this.DynamicControls;

                foreach (var listViewDataItem in this.fundHoldingListView.Items)
                {
                    foreach (var control in controls)
                    {
                        if (listViewDataItem.ID == control.Key)
                        {
                            var controlToAdd = (MonthlyHoldings)LoadControl("/UserControls/MonthlyHoldings.ascx");
                            controlToAdd.ID = control.Value;

                            var placeholder = (PlaceHolder)listViewDataItem.FindControl("PlaceHolder1");

                            placeholder.Controls.Add(controlToAdd);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// The page_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void Page_Load(object sender, EventArgs e)
        {

            this.ClientIdAutoCompleteLabel.Text = "17"; // hard coded value to test

            this.fundHoldingListView.DataBind();
        }

        /// <summary>
        /// The fund holding list view_ on data binding.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void fundHoldingListView_OnDataBinding(object sender, EventArgs e)
        {
            using (var ds = new DataContext())
            {
                this.fundHoldingListView.DataSource =
                    ds.HoldingValueByDates
                        .Where(x => x.ClientPortfolioHolding.ClientPortfolio.ClientId == Convert.ToInt32(this.ClientIdAutoCompleteLabel.Text))
                        .GroupBy(x => x.HoldingDate.Year)
                        .Select(x => new Year { Value = x.Key })
                    .Union(ds.ClientPortfolioHoldings
                        .Where(x => x.ClientPortfolio.ClientId == Convert.ToInt32(this.ClientIdAutoCompleteLabel.Text))
                        .Where(x => x.MaturityDate <= DateTime.Now.Date)
                        .Where(x => x.DisposedOn == null || x.DisposedOn > DateTime.Now.Date)
                        .GroupBy(x => x.MaturityDate.Value.Year)
                        .Select(x => new Year { Value = x.Key }))
                        .OrderByDescending(x => x.Value).ToList();
            }
        }

        /// <summary>
        /// The button on click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        protected void ButtonOnClick(object sender, EventArgs e)
        {
            var linkButton = (LinkButton)sender;
            var listItem = (ListViewItem)linkButton.NamingContainer;

            if (this.ViewState[listItem.ID] == null)
            {
                this.AddMonthlyHoldings(listItem);
            }
        }

        /// <summary>
        /// The add monthly holdings.
        /// </summary>
        /// <param name="listItem">
        ///     The list item.
        /// </param>
        private void AddMonthlyHoldings(ListViewItem listItem)
        {
            var updatePanel = (UpdatePanel)listItem.FindControl("UpdatePanelTimeLineitem");
            var placeholder = (PlaceHolder)listItem.FindControl("PlaceHolder1");
            var hiddenFieldYear = (HiddenField)listItem.FindControl("yearHiddenField");
            var control = (MonthlyHoldings)LoadControl("/UserControls/MonthlyHoldings.ascx");

            control.ID = Guid.NewGuid().ToString();

            control.MonthlyClientId = this.ClientIdAutoCompleteLabel.Text;
            control.Year = hiddenFieldYear.Value;

            placeholder.Controls.Add(control);

            control.DataBind();
            updatePanel.Update();

            this.DynamicControls.Add(listItem.ID, control.ID);
        }
    }

第二级.ascx

<asp:ListView runat="server" ID="monthlyListView" OnDataBinding="monthlyListView_OnDataBinding" OnItemDataBound="monthlyListView_OnItemDataBound">
    <LayoutTemplate>
        <ul class="li-blank collapsible-subpanel-container">
            <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
        </ul>
    </LayoutTemplate>
    <ItemTemplate>
        <ajaxToolkit:CollapsiblePanelExtender
            ID="monthlyCollapsableExtender"
            runat="server"
            TargetControlID="monthlyCollapsablePanel"
            ExpandControlID="monthlyPanel"
            CollapseControlID="monthlyPanel"
            Collapsed="True" />
        <li class="li-selectable collapsible-subpanel">
            <asp:Panel runat="server" ID="monthlyPanel">
                <h4>
                    <asp:LinkButton ID="linkButton2" runat="server" OnClick="ButtonOnClick" Text='<%# CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(Convert.ToInt32(Eval("Key")))%>'></asp:LinkButton>
                </h4>
            </asp:Panel>
        </li>

        <asp:HiddenField ID="MonthHiddenField" runat="server" Value='<%# Eval("Key") %>' />
        <asp:HiddenField ID="YearHiddenField" runat="server" Value='<%# Year %>' />
        <asp:HiddenField ID="ClientIdHiddenField" runat="server" Value='<%# MonthlyClientId %>' />

        <asp:Panel runat="server" ID="monthlyCollapsablePanel">
            <asp:UpdatePanel ID="UpdatePanelMonthlyHoldings" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server" EnableViewState="True">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="linkButton2" />
                </Triggers>
                <ContentTemplate>
                    <asp:PlaceHolder runat="server" ID="Placeholder2"></asp:PlaceHolder>
                </ContentTemplate>
            </asp:UpdatePanel>
            <%--<uc:HoldingDates ID="holdingsUserControl" runat="server" Month='<%# Eval("Key") %>' Year='<%# Year %>' HoldingDatesClientId='<%# MonthlyClientId %>'></uc:HoldingDates>--%>
        </asp:Panel>
    </ItemTemplate>
</asp:ListView>

0 个答案:

没有答案