zoukankan      html  css  js  c++  java
  • 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)

            }
  • 相关阅读:
    WinForm窗口间传值
    如何自定义标签
    oracle数据库开启的时候 是先开监听还是先开主服务,关数据库的时候呢???
    oracle 10g 安装时字符集的选择,和后边的修改
    Oracle数据库安装及配置(一)
    Win7下完全卸载Oracle 11g的步骤
    Oracle创建表空间、创建用户以及授权
    ORACLE创建表空间、创建用户、更改用户默认表空间以及授权、查看权限
    Linux 常用命令集合
    Java之JSP和Servlet基础知识
  • 原文地址:https://www.cnblogs.com/flaaash/p/1996639.html
Copyright © 2011-2022 走看看