Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.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# 使用FindControl在事件上设置用户控件的属性_C#_Asp.net_User Controls - Fatal编程技术网

C# 使用FindControl在事件上设置用户控件的属性

C# 使用FindControl在事件上设置用户控件的属性,c#,asp.net,user-controls,C#,Asp.net,User Controls,我有一个在页面加载中动态加载的用户控件: protected void Page_Load(object sender, EventArgs e) { MyControl ctl = (MyControl)LoadControl(controlPath); ctl.ID = "mycontrol"; this.MyControlPlaceHolder.Controls.Add(ctl); } 页面前端: <asp:PlaceHolder runat="server

我有一个在页面加载中动态加载的用户控件:

protected void Page_Load(object sender, EventArgs e)
{
    MyControl ctl = (MyControl)LoadControl(controlPath);
    ctl.ID = "mycontrol";
    this.MyControlPlaceHolder.Controls.Add(ctl);
}
页面前端:

<asp:PlaceHolder runat="server" ID="MyControlPlaceHolder"></asp:PlaceHolder>
这是回发后写入失败,因此我在查找控件时似乎做了一些错误的事情。最好的方法是什么

编辑:


这使得它能够找到控件,但是,当控件在回发后加载时,它似乎没有设置属性。

您必须使用递归FindControl实现,因为FindControl只能找到直接子对象。控件插入到较低级别的命名容器中。来自MSDN的通用FindControlRecurrisve:

private Control FindControlRecursive(Control rootControl, string controlID)
{
    if (rootControl.ID == controlID) return rootControl;

    foreach (Control controlToSearch in rootControl.Controls)
    {
        Control controlToReturn = 
            FindControlRecursive(controlToSearch, controlID);
        if (controlToReturn != null) return controlToReturn;
    }
    return null;
}

或者,如果您的样本中只有一个特定的类似conatiner:

MyControl ctl =  this.MyControlPlaceHolder.FindControl("mycontrol");

if (ctl != null){
            ctl.MyProperty = true;
            Response.Write("success");
        }
        else
            Response.Write("fail");
视图状态启用控件

public class MyControl:Control
{
   public bool MyProperty
   {
       get 
       {
           return ViewState["my"] != null? (bool) ViewState["my"]: false; 
       }
       set 
       {
           ViewState["my"]  = value;
       }
   }
}

尝试移动代码以将控件动态添加到Init中,而不是加载。我不能确定,但是在Init和Load之间会发生很多事情,如果您的控件不存在并没有说明原因,则可能会导致类似的问题。

您在占位符的控件集合中添加了该控件。 除了什么控件是动态创建的控件之外,如果您想以这种方式查找动态添加的控件,您必须从根(可能是页面)开始进行递归搜索,因此,如果您在网上冲浪,您可以找到很好的解决方案

就我个人而言,我更喜欢支持泛型并表示为扩展方法的解决方案,因此您可以在任何地方使用该解决方案。这些是一些有用的链接


  • 希望这有帮助

    标题中不需要标签。标签系统解决了这个问题。请阅读以了解更多信息。您应该始终在每篇文章的首页重新加载用户控件。必须在init或preinit期间添加它们,因为只有这样,viewstate才会被正确捕获和还原。我将其切换到底部,它现在正在写入“success”,但是,在回发之后,该属性似乎没有设置。因此,它正在查找控件,但用户控件正在加载,就像未设置属性一样。MyControl需要将其MyProperty值存储在其ViewState中,并且必须在容器中的Page_INIT事件(而不是加载事件)中插入控件。控件的get方法必须读取其
    MyControl ctl =  this.MyControlPlaceHolder.FindControl("mycontrol");
    
    if (ctl != null){
                ctl.MyProperty = true;
                Response.Write("success");
            }
            else
                Response.Write("fail");
    
    public class MyControl:Control
    {
       public bool MyProperty
       {
           get 
           {
               return ViewState["my"] != null? (bool) ViewState["my"]: false; 
           }
           set 
           {
               ViewState["my"]  = value;
           }
       }
    }