简体   繁体   English

如何从父页面有条件地隐藏usercontrol中的某些控件

[英]How to hide some controls in the usercontrol conditionally from parent page

I have a aspx page which has five panels. 我有一个aspx页面,其中包含五个面板。 Each panel has a usercontrol in it. 每个面板中都有一个用户控件。 The user controls on all the five panels are the same. 所有五个面板上的用户控件均相同。 The user control in the first panel is to gather data for primary members and the rest are used for secondary members. 第一个面板中的用户控件用于收集主要成员的数据,其余用于次要成员。 I use ">" and "<" custom buttons to move from one panel to another. 我使用“>”和“ <”自定义按钮从一个面板移动到另一个面板。 Whenever I move from the primary panel to secondary I wan to hide two textboxes in the usercontrol. 每当我从主面板移动到辅助面板时,我都会在用户控件中隐藏两个文本框。 When they move back to the primary panel, those textboxes should reappear. 当它们移回主面板时,这些文本框应重新出现。 Since the buttons are in the parent page, please let me know how to do it. 由于按钮位于父页面中,请让我知道如何执行此操作。 I believe creating an event in the usercontrol and accessing it from parent page may work. 我相信可以在用户控件中创建事件并从父页面访问它。 But not sure how to do it. 但是不确定如何做。 Or please let me know any other methods. 或者,请让我知道其他方法。

Thanks 谢谢

You need not create an event in the user controls for this. 您无需为此在用户控件中创建事件。

All that you need is to create a public property on the user control, that you can set when you use the user control. 您所需要做的就是在用户控件上创建一个公共属性,您可以在使用用户控件时对其进行设置。

Since you have not provided any code, I will just give a sample. 由于您还没有提供任何代码,因此我将给出一个示例。

public partial class MyWidget: System.Web.UI.UserControl
{
    private bool showPrimary;
    public bool ShowPrimary
    {
        get { return showPrimary; }
        set 
        {
            showPrimary = value;
            txtPK1.Visible = value;
            txtPK2.Visible = value;
        }
    }
}

Then you set it when you call it as follows: 然后,当您按如下方式调用它时进行设置:

Main Panel:
<uc1:MyWidget ID="MyWidget1" ShowPrimary="true" runat="server" />

Secondary Panel:
<uc1:MyWidget ID="MyWidget1" ShowPrimary="false" runat="server" />

I don't understand, the buttons > and < are in the page to advance to the next/previous control. 我不明白, ><按钮位于页面中,可前进至下一个/上一个控件。 Then you just have to handle their click event in the page and switch visibility of the UserControls. 然后,您只需要处理页面中的单击事件并切换UserControls的可见性即可。

If you don't know how to control visibility of controls inside of UserControls from the page: 如果您不知道如何从页面控制UserControls内部控件的可见性:

  • Use properties in the UserControl , for example PrimaryMode . 使用UserControl属性,例如PrimaryMode There you can hide/show the TextBoxes accordingly. 在这里您可以相应地隐藏/显示TextBoxes You can call these properties from the page. 您可以从页面调用这些属性。 PrimaryMode could be of type bool or a custom enum type(if you want to provide multiple display-modes). PrimaryMode可以是bool类型,也可以是自定义枚举类型(如果要提供多种显示模式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM