Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用字符串ControlName更改页面加载中Asp.net控件的可见性_C#_Asp.net - Fatal编程技术网

C# 使用字符串ControlName更改页面加载中Asp.net控件的可见性

C# 使用字符串ControlName更改页面加载中Asp.net控件的可见性,c#,asp.net,C#,Asp.net,我的页面中有一些不同的控件,比如按钮、文本框或者Gridview。。 在页面加载中,我将设置它们的可见性和启用属性。但我只有他们的id像字符串一样。 从数据库中我要 ControlId Type Visible Enabled btnSave Button 0 0 txtEdit TextBox 1 0 你知道如何在我的页面加载中做到这一点吗?写一个简单的方法来查找页面控件并设置如下属性 注意:我

我的页面中有一些不同的控件,比如按钮、文本框或者Gridview。。 在页面加载中,我将设置它们的可见性和启用属性。但我只有他们的id像字符串一样。 从数据库中我要

   ControlId  Type     Visible   Enabled

   btnSave    Button      0         0

   txtEdit    TextBox     1         0

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

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

注意:我没有编译代码

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

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

注意:我没有编译代码

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

假设您已将控件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);
                    }
                }
    }

假设您已将控件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);
                    }
                }
    }

谢谢你的回复

首先,我找不到网络控制。但在以后的解决方案中,它会起作用。我的页面中有一个面板。因此,使用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;
    }
和禁用控制方法

   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);
        }
    }

真的谢谢你。如果你能想出最好的解决办法,我会改变的。

谢谢你@Siraj Mansour的回复

首先,我找不到网络控制。但在以后的解决方案中,它会起作用。我的页面中有一个面板。因此,使用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;
    }
和禁用控制方法

   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);
        }
    }

真的谢谢你。如果你能想出最好的解决办法,我会改变。

你试过FindControl(id)吗?控件是动态创建的吗?在哪里?您可以通过表单控件循环设置相应的属性。@amartynov当使用“查找控件”时,我无法更改“已启用”属性。但对于可见的,它可以工作。@CocLn检查我的解决方案您尝试过FindControl(id)吗?控件是动态创建的吗?在哪里?您可以通过表单控件循环设置相应的属性。@amartynov当使用“查找控件”时,我无法更改“已启用”属性。但是,为了让人看得见,它是有效的。@CocLn检查我的解决方案谢谢你Deepu。但是我的控件可以是Devexpress控件。ASPxButton、ASPxTextBox、ASPxCheckBox。。。我将在我的所有项目中使用它。所以我不能这样使用。@CocLn在这种情况下,您可以修改if条件,比如ASPxTextBox而不是TextBox。你能调试代码吗?谢谢你。但是我的控件可以是Devexpress控件。ASPxButton、ASPxTextBox、ASPxCheckBox。。。我将在我的所有项目中使用它。所以我不能这样使用。@CocLn在这种情况下,您可以修改if条件,比如ASPxTextBox而不是TextBox。你能调试代码并找到将任务分成简单部分的好方法吗?最好的可重用性将任务分成简单部分的好方法,最好的可重用性这是我能想到的最简洁的方法:)快乐的编码!这是我能想到的最简洁的:)快乐的编码!