當前位置: 首頁>>代碼示例>>C#>>正文


C# Control.ChildControlsCreated屬性代碼示例

本文整理匯總了C#中System.Web.UI.Control.ChildControlsCreated屬性的典型用法代碼示例。如果您正苦於以下問題:C# Control.ChildControlsCreated屬性的具體用法?C# Control.ChildControlsCreated怎麽用?C# Control.ChildControlsCreated使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在System.Web.UI.Control的用法示例。


在下文中一共展示了Control.ChildControlsCreated屬性的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: AddParsedSubObject

//引入命名空間
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TemplateControlSamples
{
    [ParseChildren(true)]
    public class CustomRepeater : Repeater
    {

        // Override to prevent LiteralControls from being added as children.
        protected override void AddParsedSubObject(object o)
        {
        }

        // Override to create repeated items.
        protected override void CreateChildControls()
        {
            object o = ViewState["NumItems"];
            if (o != null)
            {
                // Clear any existing child controls.
                Controls.Clear();

                int numItems = (int)o;
                for (int i = 0; i < numItems; i++)
                {
                    // Create an item.
                    RepeaterItem item = new RepeaterItem(i, ListItemType.Item);
                    // Initialize the item from the template.
                    ItemTemplate.InstantiateIn(item);
                    // Add the item to the ControlCollection.
                    Controls.Add(item);
                }
            }
        }

        // Override to create the repeated items from the DataSource.
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            if (DataSource != null)
            {
                // Iterate over an ICollection DataSource, creating a new item for each data item.
                IEnumerator dataEnum = ((ICollection)base.DataSource).GetEnumerator();
                int i = 0;
                while (dataEnum.MoveNext())
                {
                    // Create an item.
                    RepeaterItem item = new RepeaterItem(i, ListItemType.Item);
                    item.DataItem = dataEnum.Current;
                    // Initialize the item from the template.
                    ItemTemplate.InstantiateIn(this);
                    // Add the item to the ControlCollection.
                    Controls.Add(item);

                    i++;
                }

                // Prevent child controls from being created again.
                ChildControlsCreated = true;
                // Store the number of items created in view state for postback scenarios.
                ViewState["NumItems"] = i;
            }
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.Web.UI,代碼行數:70,代碼來源:Control.ChildControlsCreated


注:本文中的System.Web.UI.Control.ChildControlsCreated屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。