【问题标题】:how to change visible of an asp.net control of master page in content page?如何更改内容页面中母版页的 asp.net 控件的可见性?
【发布时间】:2013-05-01 08:05:50
【问题描述】:

我有一个Master Page,它有一个asp:Panel 控件和在其后面的代码中设置Visible = False 的代码。
现在我想在内容页面之一中更改Visible = True。怎么做?

后面的母版页代码:

AccountUserInfo.Visible = false;  

后面的内容页面代码:

((Panel)Master.FindControl("AccountUserInfo")).Visible = true;

显然内容页面后面的代码不起作用。

【问题讨论】:

  • 您是否收到某种错误?你确定那条线会被调用吗?
  • @Serge,不,一切都很好,我没有任何错误!但它不起作用!

标签: c# asp.net asp.net-controls


【解决方案1】:

将控件设置为Visible = False的母版页代码正在页面上的代码之后执行。

尝试将页面代码放在PreRender 事件上。这是循环中最后的事件之一:

protected override void OnPreRender(EventArgs e)
{
    ((Panel)Master.FindControl("AccountUserInfo")).Visible = true; 
    base.OnPreRender(e);
}

另外,看看这个ASP.NET Page Life Cycle Diagram

【讨论】:

    【解决方案2】:

    在 ASP 网站生命周期中,Page 的代码在 Master 页面的代码之前运行。

    所以你基本上只是在你的母版页中覆盖之前设置为“true”的“可见”设置:AccountUserInfo.Visible=false;

    另请注意,如果 AccountUserInfo 的任何父容器的可见性设置为 false,AccountUserInfo.Visible getter 将返回 false(恕我直言:微软在那里做出的糟糕选择......)。

    【讨论】:

    • 看Page Life Cycle。您还可以在母版页和内容页中放置调试断点,以便查看哪个先执行。
    【解决方案3】:

    试试这个

    protected void Page_PreRender(object sender, EventArgs e)
    {
    
    ((Panel)Master.FindControl("panel")).Visible = true;
    
    }
    

    希望对你有帮助

    【讨论】:

      【解决方案4】:

      试试这个

      在内容页面中

      protected void Page_PreRender(object sender, EventArgs e)
      {
      
          Panel panel = (Panel)Master.FindControl("panel");
          panel.Visible = true;
      
      }
      

      【讨论】:

        【解决方案5】:

        如果母版页与其控件之间的连接是直接的,则以前的答案可能会起作用。在其他情况下,您可能想查看您尝试“查找”和更改的对象的层次结构。 IE。如果从 MasterPage 调用 FindControl 可能会返回 null(这取决于您的内容页面的结构,例如 MasterPage > Menu > MenuItem > control)

        因此,考虑到这一点,您可能希望在后面的内容页面代码中执行类似的操作:

        protected void Page_PreRender(object sender, EventArgs e)
        {
           ParentObj1 = (ParentObj1)Master.FindControl("ParentObj1Id"); 
           ParentObj2 = (ParentObj2)ParentObj1.FindControl("ParentObj1Id"); // or some other function that identifies children objects
           ...
           Control ctrl  = (Control)ParentObjN.FindControl("ParentObjNId"); // or some other function that identifies children objects
           // we change the status of the object
           ctrl.Visible = true;
        }
        

        或者我会给你一个对我有用的实际 sn-p(使带有 id ButtonViewReports 的按钮隐藏在 MasterPage 中,在内容页面上可见):

        protected override void OnPreRender(EventArgs e)
        {
            // find RadMenu first
            RadMenu rm = (RadMenu)this.Master.FindControl("MenuMaster");
            if (rm != null)
            {
                // find that menu item inside RadMenu
                RadMenuItem rmi = (RadMenuItem)rm.FindItemByValue("WorkspaceMenuItems");
                if (rmi != null)
                {
                    // find that button inside that Menitem
                    Button btn = (Button)rmi.FindControl("ButtonViewReports");
                    if (btn != null)
                    {
                        // make it visible
                        btn.Visible = true;
                    }
                }
            }
            base.OnPreRender(e);
        }
        

        【讨论】:

          猜你喜欢
          • 2017-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多