C# 使用Usercontrol中的公共属性设置Webcontrol属性
我最近发现,通过usercontrol中的公共属性设置asp:按钮上的visible属性并不总是有效(请参阅)。我习惯于通过公共属性设置我的webcontrol属性,但是否有标准或最佳实践来设置我的usercontrol的webcontrol属性 这是我的代码的简化版本:C# 使用Usercontrol中的公共属性设置Webcontrol属性,c#,asp.net,user-controls,C#,Asp.net,User Controls,我最近发现,通过usercontrol中的公共属性设置asp:按钮上的visible属性并不总是有效(请参阅)。我习惯于通过公共属性设置我的webcontrol属性,但是否有标准或最佳实践来设置我的usercontrol的webcontrol属性 这是我的代码的简化版本: public partial class MyUserControl: System.Web.UI.UserControl { public bool IsVisible { set{MyBut
public partial class MyUserControl: System.Web.UI.UserControl
{
public bool IsVisible
{
set{MyButton.Visible = value;}
}
protected void Page_PreRender(object sender, EventArgs e)
{
if (MyButton.Visible)
{
trButtons.Visible = true;
//do something
//MyButton.Visible is always false, even when it is assigned true thru the
//public property above, when the <tr> element in the form has Visible = "false"
}
}
}
公共部分类MyUserControl:System.Web.UI.UserControl
{
公共图书馆是可见的
{
设置{MyButton.Visible=value;}
}
受保护的无效页面\u预呈现(对象发送方,事件参数e)
{
if(MyButton.Visible)
{
trbutions.Visible=true;
//做点什么
//MyButton.Visible始终为false,即使通过
//当表单中的元素具有Visible=“false”时,上述公共属性
}
}
}
设计师
<table>
<tr runat="server" id="trButtons" visible="true">
<td>
<asp:Button ID="MyButton" runat="server" Text="The Button" />
</td>
</tr>
</table>
我想这是因为页面生命周期。这里有什么问题?Visibility
是否未设置为“True”?动态属性并不总是有效。尝试在生命周期的Page_PreInit方法中的可见性。