使用Usercontrol中的公共属性设置Webcontrol属性

时间:2012-08-09 16:34:47

标签: c# asp.net user-controls

我最近发现在我的usercontrol中的asp:按钮上设置visible属性并不总是通过我的usercontrol中的公共属性设置它(参见Why is the Visible property not correctly updated in a UserControl?)。我习惯通过公共属性设置我的webcontrol属性,但是为我的usercontrol设置webcontrol属性是否有标准或最佳实践?

这是我的代码的简化版本:

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"
        }
    }
}

设计

 <table>
     <tr runat="server" id="trButtons" visible="true">
         <td>                
            <asp:Button ID="MyButton" runat="server" Text="The Button" /> 
         </td>
     </tr>
 </table>

0 个答案:

没有答案