zoukankan      html  css  js  c++  java
  • 使用MasterPage后的this.FindControl()函数的问题

    以前我就曾经写过一个关于MasterPage里面使用this.FindControl()函数的问题。

    http://www.cnblogs.com/dlwang2002/archive/2006/07/09/446643.html

    当时通过其他方式解决了,但是今天又遇到了。在google上找到了一篇文章http://west-wind.com/WebLog/posts/5127.aspx说的很对,能够解决这个问题

     

    在没有使用MasterPage之前,页面this访问到的对象就是他自己,所以使用this,FindControl也就没什么问题。

    但是使用MasterPage之后,this.Control.Count=1,就只有一个。跟踪一下:   this.Controls[0]      {ASP.module_mp1_master}       System.Web.UI.Control {ASP.module_mp1_master} .

    也就是说,加入masterPage之后他的控件的顺序就变了。(以前看过一个在asp.net1.1下的MasterPage实现,也是类似的方式,重新组织控件)。这就是问题所在。

     

    解决方法:

    把原来这样的使用:

    protected void AssignControls()

    {

        
    this.lblError = this.FindControl("lblError"as Label;

        
    this.dgItemList = this.FindControl("dgItemList"as DataGrid;

    }


    变成这样调用:

    protected void AssignControls()

    {

        
    this.lblError = this.Master.FindControl("Content").FindControl("lblError"as Label;

        
    this.dgItemList = this.Master.FindControl("Content").FindControl("dgItemList"as DataGrid;

    }



    也就是说,先使用this.Master.FindControl("Content")来找到在MasterPage上相对的位置,然后再按照以前的调用方式就可以了。

     

    或者使用这个函数(这三个函数都来自原作者Rick Strahl

    public static Control FindControlRecursive(Control Root, string Id)

    {

        
    if (Root.ID == Id)

            
    return Root;

     

        
    foreach (Control Ctl in Root.Controls)

        
    {

            Control FoundCtl 
    = FindControlRecursive(Ctl, Id);

            
    if (FoundCtl != null)

                
    return FoundCtl;

        }


     

        
    return null;

    }


    转自:http://www.cnblogs.com/dlwang2002/archive/2006/12/21/599107.html
  • 相关阅读:
    HTML&&CSS
    web概述&HTML快速入门
    JDBC连接池&JDBCTemplate
    基于Breast Cancer dataset的决策树分类及可视化
    三维数组按行优先存储求某位置的地址
    2019年复旦计算机专硕考研经验总结
    1013 Battle Over Cities (25 分)
    1009 Product of Polynomials (25 分)
    1004 Counting Leaves (30 分)
    1090 危险品装箱 (25 分)
  • 原文地址:https://www.cnblogs.com/footleg/p/1298523.html
Copyright © 2011-2022 走看看