0

我的页面中有一些不同的控件,例如 Button、Textbox 或 Gridview。在页面加载中,我将设置它们的可见性和启用的属性。但我将只有他们的 id 之类的字符串。我会从数据库中获取

   ControlId  Type     Visible   Enabled

   btnSave    Button      0         0

   txtEdit    TextBox     1         0

你知道如何在我的页面加载中做到这一点吗?

4

3 回答 3

2

假设您将控件 ID 加载到字符串 []

尝试 :

protected class ControlDescription
    {
        public string Name { get; set; }
        public bool isVisible { get; set; }
        public bool isEnabled { get; set; }

        public ControlDescription(string _name, int vis, int ena)
        {
            this.Name = _name;
            this.isVisible = (vis == 1);
            this.isEnabled = (ena == 1);
        }
    }

protected void Page_Load(object sender, EventArgs e)
    {
        List<ControlDescription> CDescriptions = new List<ControlDescription>();
        //loop through data from database 
        //{
        //ControlDescription C = new ControlDescription(name,int,ena);
        //CDescriptions.Add(C);
        //}

                foreach (ControlDescription C in CDescriptions)
                {
                    Control Ctrl = this.FindControl(C.Name);
                    if (Ctrl != null)
                    {
                        //Ctrl.Visible = C.isVisible;
                        DisableControls(Ctrl);
                        //if (Ctrl is WebControl)
                        //    ((WebControl)Ctrl).Enabled = C.isEnabled;
                        HideControls(Ctrl);
                    }
                }
    }

编辑:根据您的评论,您可能不知道您的控件类型并不总是 web 控件或不一定是子类,我添加了另外 2 个函数并在上面的代码中使用它们而不是现有代码。

private void DisableControls(System.Web.UI.Control control)
{

    foreach (System.Web.UI.Control c in control.Controls) {

        // Get the Enabled property by reflection.
        Type type = c.GetType;
        PropertyInfo prop = type.GetProperty("Enabled");

        // Set it to False to disable the control.
        if ((prop != null)) {
            prop.SetValue(c, false, null);
        }

        // Recurse into child controls.
        if (c.Controls.Count > 0) {
            this.DisableControls(c);
        }

    }

} 

private void HideControls(System.Web.UI.Control control)
{

    foreach (System.Web.UI.Control c in control.Controls) {

        // Get the Enabled property by reflection.
        Type type = c.GetType;
        PropertyInfo prop = type.GetProperty("Visible");

        // Set it to False to disable the control.
        if ((prop != null)) {
            prop.SetValue(c, false, null);
        }

        // Recurse into child controls.
        if (c.Controls.Count > 0) {
            this.HideControls(c);
        }

    }

} 
于 2013-07-02T10:04:55.323 回答
1

编写一个简单的方法来查找页面控件并设置如下所示的属性。

注意:我没有编译代码。

private void Page_Load(object sender, System.EventArgs e)
{
    SetControl("txtEdit","ASPxTextBox",1,0);
    SetControl("btnSave","ASPxButton",0,0);
}

private void SetControl(string controlId,string controlType, bool visible, bool enabled)
{ 
    foreach(Control control in Page.Controls)
    {
        if(controlType == "ASPxTextBox" && control is ASPxTextBox && control.Id == controlId)
        {
            var tb = ((ASPxTextBox)control);
            tb.Visible = visible;
            tb.Enabled = enabled;
            break;
        }
        if(controlType == "ASPxButton" && control is  ASPxButton && control.Id == controlId)
        {
            var bt = ((ASPxButton)control);
            bt.Visible = visible;
            bt.Enabled = enabled;
            break;
        }
    }
}
于 2013-07-02T09:52:19.957 回答
0

感谢@Siraj Mansour 的回复 https://stackoverflow.com/a/17422753/1685288

首先是我找不到的webcontrol。但是稍后使用您的解决方案它可以工作。我的页面中有面板。因此,使用 this.FindControl 我无法找到面板内的控件。所以我改变了你的发现;

Control Ctrl = FindControlRecursive(Page, c.Name);

我写了一个递归方法;

private Control FindControlRecursive(Control root, string controlId)
    {
        Control ret = null;
        if (root.ID == controlId)
        {
            return root;
        }
        foreach (Control c in root.Controls)
        {
            Control t = FindControlRecursive(c, controlId);
            if (t != null)
            {
                ret = t;
                return ret;
            }

        }
        return ret;
    }

和 DisableControls 方法;

   private void DisableControls(System.Web.UI.Control c)
    {
       Type type = c.GetType();
        PropertyInfo prop = type.GetProperty("Enabled");
        if ((prop != null))
        {
            prop.SetValue(c, false, null);
        }
    }

真的谢谢你。如果你能想到最好的解决方案,我会改变。

于 2013-07-02T11:26:33.343 回答