zoukankan      html  css  js  c++  java
  • 在子MasterPage设置UserControl内的Web控件属性

     环境是这样的,一个MasterPage 如MP2(子)嵌套另一个MasterPage 如MP1(父)。一个UserControl 如MyUc,这个UserControl有一个Web控件,如TextBox1它初始状态为 Visible="false" ,它是拉在MP1的MasterPage上。 现在是要在MP2的MasterPage的page_Load控制UserControl内的TextBox显示与否。

    这样的做法,就是所有使用MP1 MasterPage的网页对用户控件的TextBox是不可见,而使用MP2 MasterPage的网页对用户控件的TextBox是可见。

    Insus.NET实现这个问题,也花上不少时间,虽然以前实现很多相似的,但都是有page内,而非是在MasterPage去操作。下面是实现过程:

    写一个Interface(接口) ,设置用户控件内的TextBox是否显示,是让MyUc用户控件实作这个接口。

    IVisible
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// Summary description for IVisible
    /// </summary>
    namespace Insus.NET
    {
        public interface IVisible
        {
            void SetVisible(bool show);
        }
    }

    MyUc用户控件实作上面接口:

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Insus.NET;

    public partial class MyUc : System.Web.UI.UserControl,IVisible
    {      
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public void SetVisible(bool show)
        {
            this.TextBox1.Visible = show;
        }
    }

    接下来,再写一个接口,接口写了一个bool的属性,设法让实作这个接口的物件实作这个属性。:

    ICtrlDisplay
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// Summary description for ICtrlDisplay
    /// </summary>
    namespace Insus.NET
    {
        public interface ICtrlDisplay
        {
            bool ShowControl { set; }
        }
    }

    因为MyUc UserControl 拉入MP1 MasterPage内。因此上面的接口ICtrlDisplay为之准备。

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Insus.NET;

    public partial class MP1 : System.Web.UI.MasterPage,ICtrlDisplay
    {       
        protected void Page_Load(object sender, EventArgs e)
        {
         
        }

        public bool ShowControl
        {
            set 
            {
                ((IVisible)this.MyUc1).SetVisible(value);           
            }
        }
    }

    准备好上面过程,现在我们就可以以MP2的MasterPage的Page_Load事件中,控制到MyUc内的TextBox显示与否。

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Insus.NET;

    public partial class SubDirectory_MP2 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ((ICtrlDisplay)this.Master).ShowControl = true; ;
        }
    }

    看看演示:

     

    演示源程序:

    http://download.cnblogs.com/insus/ASPDOTNET/mstpCtrlUc.rar

  • 相关阅读:
    Spring中配置和读取多个Properties文件
    Eclipse 常用快捷键
    static 静态导包
    CDN 备胎技巧
    org.apache.commons.lang3.ArrayUtils 学习笔记
    深入浅出 妙用Javascript中apply、call、bind
    URL详解与URL编码
    Chrome DevTools good good study day day up
    Java反射机制
    第一篇:《UNIX 环境高级编程》编译环境的搭建
  • 原文地址:https://www.cnblogs.com/insus/p/2362830.html
Copyright © 2011-2022 走看看