Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 标签的可见属性充当只读属性_C#_Asp.net - Fatal编程技术网

C# 标签的可见属性充当只读属性

C# 标签的可见属性充当只读属性,c#,asp.net,C#,Asp.net,我在用户控件中有一个标签: <asp:Label runat="server" ID="lblRemainingPlacesMessage" Visible="false" /> 但是,标签仍然是隐藏的 让我困惑的是,即使在即时窗口或调试器本地监视中,属性也无法更改: (即时窗口) 什么可以解释我的可见属性不能更改 我也不例外。只是一个像NOOP一样的手术 我的应用程序已启用viewstate。最重要的是,我有其他标签在页面上,这是完美的工作 不知道这是否重要,但我在我的所有者页面

我在用户控件中有一个标签:

<asp:Label runat="server" ID="lblRemainingPlacesMessage" Visible="false" />
但是,标签仍然是隐藏的

让我困惑的是,即使在即时窗口或调试器本地监视中,属性也无法更改:

(即时窗口)

什么可以解释我的可见属性不能更改

我也不例外。只是一个像NOOP一样的手术

我的应用程序已启用viewstate。最重要的是,我有其他标签在页面上,这是完美的工作

不知道这是否重要,但我在我的所有者页面中使用以下方法动态实例化我的用户控件:

    protected override void CreateChildControls()
    {
        m_VisualControl = (MyUserControl)Page.LoadControl(_ascxPath);
        Controls.Add(m_VisualControl);
    }
该应用程序使用ASP.Net WebForms和.Net 3.5 SP1,我使用Visual Studio 2012 Premium。

1)您应该在OnInit上创建任何动态用户控件,否则您将无法使用
ViewState

2) 将您不想更改的label属性作为
UserControl的属性公开

public bool HiddeMyLabel
{
   set { lblRemainingPlacesMessage.Visible = value; }
   get { lblRemainingPlacesMessage.Visible; }
}
3) 您应该在页面中使用OnPreRender来更改任何控件属性

protected override void OnPreRender(EventArgs e)
{
    MyUserControl.HiddeMyLabel = false; 
}
4) 如果仍然存在问题,请手动删除隐藏属性:

public bool HiddeMyLabel
{
   set 
   { 
     if(value)
       lblRemainingPlacesMessage.Attributes.Add("style", "display:none");
     else
       lblRemainingPlacesMessage.Attributes.Add("style", "display:block");
   }
}

尤里·加兰特的评论让我走上了正确的道路

简单地说,我的直系父母是不可见的。我假设控件的visible属性结合了控件的visibility


有时简单的问题有简单的解决方法:)

您能发布控件的完整aspx标记吗?或者如果它太大——至少是直系标签的父母
protected override void OnPreRender(EventArgs e)
{
    MyUserControl.HiddeMyLabel = false; 
}
public bool HiddeMyLabel
{
   set 
   { 
     if(value)
       lblRemainingPlacesMessage.Attributes.Add("style", "display:none");
     else
       lblRemainingPlacesMessage.Attributes.Add("style", "display:block");
   }
}