• ASP.NET 采用MasterPage 后的控件组织结构


    你可以在Page指令页设置启用Trace单页面跟踪,或者在Web.config中再启用全局跟踪,就可以看到Control Tree了。

     采用MasterPage后的页面结构如下:

                //Page  --ASP.about_aspx
                //    Master Page   --ASP.site_master
                //        (Master page markup and controls)
                //        ContentPlaceHolder    --HeadContent
                //            Content page markup and server controls
                //            (Master page markup and controls)
                //        ContentPlaceHolder    --MainContent
                //            Content page markup and server controls
                //            (Master page markup and controls)
    无论我们用FindControl查找控件,或者是添加控件,一定是添加到ContenPlaceHolder内部的!

    下面代码实现了动态添加控件以及两种查找控件的方法。

           //BMK FindControlRecursive
            /// <summary>
            
    /// 迭代遍历控件,查找子控件
            
    /// </summary>
            
    /// <param name="rootControl">包含控件的容器</param>
            
    /// <param name="controlIDToBeSEEK">要查找的控件的ID</param>
            
    /// <returns></returns>
            private Control FindControlRecursive(Control rootControl, string controlIDToBeSEEK)
            {
                
    if (rootControl.ID == controlIDToBeSEEK)
                    
    return rootControl;

                
    foreach (Control control in rootControl.Controls)
                {
                    Control controlToReturn 
    = FindControlRecursive(control, controlIDToBeSEEK);
                    
    if (controlToReturn != null)
                        
    return controlToReturn;
                }
                
    return null;
            }



            
    //再有Masterpage 的时候,控件必须加到ContentPlaceHolder内
            
    //要查找控件,也必须ContentPlaceHolder内FindControl,无论层次怎样,总能得到有效结果
            protected void Button1_Click(object sender, EventArgs e)
            {
                
    for (int i = 0; i < 5; i++)
                {
                   TextBox  txtDynamicAdded 
    = new TextBox();
                   txtDynamicAdded.ID 
    = "txt" + i.ToString();
                    txtDynamicAdded.Text 
    = "Hello,World!";
                    
    //可视状态,否则PostBack后动态添加的控件会不见
                    ViewState[txtDynamicAdded.ID] = true;
                    
    this.Master.FindControl("MainContent").Controls.Add(txtDynamicAdded);
                }

                Control ctrlFindbutton 
    = FindControlRecursive(this"Button1");
                
    if (ctrlFindbutton != null)
                {
                    Button btnFind 
    = (Button)ctrlFindbutton;
                    btnFind.Text 
    = "find" + DateTime.Now.ToString("T");
                }

                
    //如何才能访问MainContent呢
                Control cc = this.Master.FindControl("MainContent").FindControl("Button1");


                
    //Page  --ASP.about_aspx
                
    //    Master Page   --ASP.site_master
                
    //        (Master page markup and controls)
                
    //        ContentPlaceHolder    --HeadContent
                
    //            Content page markup and server controls
                
    //            (Master page markup and controls)
                
    //        ContentPlaceHolder    --MainContent
                
    //            Content page markup and server controls
                
    //            (Master page markup and controls)

            }
  • 相关阅读:
    高级映射之事务
    配置tomcat-users.xml文件
    动态SQL之标签
    性能测试
    Service
    添加 aar 或 jar 包依赖 的方式
    安卓设备 以太网代理 问题排查
    剑指offer:面试题15、链表中倒数第 K 个结点
    剑指offer:面试题14、调整数组顺序使奇数位于偶数前面
    剑指offer:面试题13、在O(1)时间删除链表结点
  • 原文地址:https://www.cnblogs.com/flaaash/p/1996639.html
Copyright © 2020-2023  润新知