正在加载......
仅一羊,已亡,补之已晚;仅一世,而立,不奋而憾!

原创内容页FindControl服务器控件


     一、在用户控件(Footer.ascx)中,使用FindControl可直接查找服务器控件,
        rpCategorys1 表示Footer.ascx中的服务器控件
如:Repeater rpCategorys = (Repeater)FindControl("rpCategorys1");

     二、但是在内容页继承自模版页后,用上面的方法就无法查找到控件了,

摘自 海蓝的 随笔 海蓝 如何使用FindControl查找内容页上的某个控件?

经过一番搜索,在forums.asp.net中找到了答案,以下引用的是bitmask的说法:

...becasue the Content controls themselves dissapear after the master page rearranges the page. You can use the ContentPlaceHolders, or the <form> on the MasterPage if there are no INamingContainers between the form and the control you need.



     所以以上的代码应该改成:

(Repeater)Page.Master.FindControl("MiddlePlaceHolder").FindControl("rpCategorys2");

     MiddlePlaceHolder 表示模版页中的内容占位区域
     <asp:ContentPlaceHolder ID="MiddlePlaceHolder" runat="server"></asp:ContentPlaceHolder>
     rpCategorys2 表示内容页上的服务器控件


     三、参考:如何使用FindControl查找内容页上的某个控件?

   

发表评论

昵称 (必填)

邮箱 (必填且保护)

网址 (选填:http://www.baidu.com)

      

      




Copyright © 2009  伊牛娃   |  沪ICP备09007324号   
点击这里给我发消息