如何通过属性设置按钮的启用状态

时间:2016-06-05 16:29:38

标签: c# asp.net webforms webusercontrol

我有一个充满按钮的用户控件,我希望能够做的是依次告诉每个按钮是否已禁用,所以我使用公共enumProperty来允许我dertmin哪个按钮。

public enum iandENUM
    {
        Personal =0,
        Property = 1,
        iande = 2,
        Vechiles = 3
    }

public iandENUM actvieButton { get; set; }



<telerik:RadButton ID="rbPersonalDetails" runat="server" CssClass="btn btn-info" Text="Property Details">

                <ContentTemplate>
                    <i class="fa fa-user" style="color: #5bc0de;"></i>Personal Details
                </ContentTemplate>

            </telerik:RadButton>
            <telerik:RadButton ID="rbPropertyDetails" runat="server" CssClass="btn btn-info" Text="Property Details" DisabledButtonCssClass="btn btn-default">

                <ContentTemplate>
                    <i class="fa fa-home" style="color: #5bc0de;"></i>Property Details
                </ContentTemplate>

            </telerik:RadButton>
            <telerik:RadButton ID="rbVechicleDetails" runat="server" CssClass="btn btn-info"   DisabledButtonCssClass="btn btn-default" btn-defaultText="Vechicle Details">

                <ContentTemplate>
                    <i class="fa fa-car" style="color: #5bc0de;"></i>Vechicle Details
                </ContentTemplate>

            </telerik:RadButton>

            <telerik:RadButton ID="rbincomee"  runat="server" CssClass="btn btn-info" DisabledButtonCssClass="btn btn-default">

                <ContentTemplate>
                    <i class="fa fa-gbp" style="color: #5bc0de;"></i>Income & Expenditures 
                </ContentTemplate>

            </telerik:RadButton>



            <telerik:RadButton ID="rbPensions" runat="server" CssClass="btn btn-info"  DisabledButtonCssClass="btn btn-default">

                <ContentTemplate>
                    <i class="fa fa-bank" style="color: #5bc0de;"></i>Pensions
                </ContentTemplate>

            </telerik:RadButton>

            <telerik:RadButton ID="rbMeetings" runat="server" CssClass="btn btn-info"  DisabledButtonCssClass="btn btn-default">

                <ContentTemplate>
                    <i class="fa fa-calendar" style="color: #5bc0de;"></i>Meetings
                </ContentTemplate>

            </telerik:RadButton>

            <telerik:RadButton ID="rbOtherAssets" runat="server" CssClass="btn btn-info" DisabledButtonCssClass="btn btn-default">

                <ContentTemplate>
                    <i class="fa fa-share" style="color: #5bc0de;"></i>Other Assets
                </ContentTemplate>

            </telerik:RadButton>

这是我的主要问题背后的类代码,我将如何给出属性值,以便我可以根据iandeEnum设置活动按钮

  protected void Page_Load(object sender, EventArgs e)
    {
        if(actvieButton==casesToolbar.iandENUM.Personal)
        {




        }
    }

    private bool _enabledState;
    public bool EnabledState
    {
        get { return _enabledState; }
        set {

            rbMeetings.Enabled = _enabledState;
            rbOtherAssets.Enabled = _enabledState;
            rbPensions.Enabled = _enabledState;
            rbPropertyDetails.Enabled = _enabledState;
            rbVechicleDetails.Enabled = _enabledState;
            rbincomee.Enabled = _enabledState;
             }
    }

    public enum iandENUM
    {
        Personal =0,
        Property = 1,
        iande = 2,
        Vechiles = 3
    }

    public iandENUM actvieButton { get; set; }
    public void disableSectionsTillSave(Boolean isEnabled)
    {

        try
        {
            rbMeetings.Enabled = isEnabled;
            rbOtherAssets.Enabled = isEnabled;
            rbPensions.Enabled = isEnabled;
            rbPropertyDetails.Enabled = isEnabled;
            rbVechicleDetails.Enabled = isEnabled;
            rbincomee.Enabled = isEnabled;
        }
        catch (Exception ex)

        {
            string inner = string.Empty;
            if (ex.InnerException != null)
            {
                inner = ex.InnerException.ToString();
            }
            logger.Error("Error in disableSectionsTillSave function edit modules cases " + ex.ToString() + " " + inner);

        }


    }

2 个答案:

答案 0 :(得分:0)

也许只是我,但我看到了对财产使用的误解。这里的错误是你没有在它的setter中设置属性值。如果您需要访问某个属性的字段,请按以下方式编写您的属性:

private bool _enabledState;
public bool EnabledState
{
    get { return _enabledState; }
    set { _enabledState = value; }
}

但是,现在这是一种声明和使用属性的过时方式。更好的方法是通过所谓的自动财产:

public bool EnabledState { get; set; }

详细了解此MSDN article

中的属性

稍后您可以通过访问属性本身来访问属性值 - 即EnabledState - 而不是属性支持字段 - 在这种情况下为_enabledProperty

通过将EnabledState分配到Enabled媒体资源,您可以在代码中的其他位置设置按钮的启用状态:

        rbMeetings.Enabled = EnabledState;
        rbOtherAssets.Enabled = EnabledState;
        rbPensions.Enabled = EnabledState;
        rbPropertyDetails.Enabled = EnabledState;
        rbVechicleDetails.Enabled = EnabledState;
        rbincomee.Enabled = EnabledState;

不幸的是,如果没有关于业务逻辑的更多详细信息,我无法就何时以及如何确定EnabledState向您提供建议。显然它应该在你设置按钮之前发生。一种方法是运行逻辑以确定EnabledState中的Page_Load,然后在Page_PreRender中设置按钮。

答案 1 :(得分:0)

您可以在修改ActiveButton属性的值时启用相应的按钮:

public enum MyEnum
{
    Val1,
    Val2,
    Val3
}

private MyEnum m_ActiveButton = MyEnum.Val1;

public MyEnum ActiveButton
{
    get
    {
        return m_ActiveButton;
    }

    set
    {
        m_ActiveButton= value;

        btn1.Enabled = false;
        btn2.Enabled = false;
        btn3.Enabled = false;

        switch (value)
        {
            case MyEnum.Val1:
            {
                btn1.Enabled = true;
                break;
            }

            case MyEnum.Val2:
            {
                btn2.Enabled = true;
                break;
            }

            case MyEnum.Val3:
            {
                btn3.Enabled = true;
                break;
            }
        }
    }
}

该属性也可以在标记中使用,以设置初始状态:

<userCtl:WebCtl1 runat="server" ActiveButton="Val3" />