Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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# 问题:从静态方法修改控件_C#_Asp.net - Fatal编程技术网

C# 问题:从静态方法修改控件

C# 问题:从静态方法修改控件,c#,asp.net,C#,Asp.net,这里有一些类似的问题,但它们对我不起作用 我在页面上有一个自定义下拉列表,页面名称为WebForm1。在WebForm1上,我有一个对它的程序集引用: 然后我有一个静态方法,如下所示: public static void Populate_Country_DDL(System.Web.UI.Page page) { GroupDropDownList .GroupDropDownList ddlCountry =

这里有一些类似的问题,但它们对我不起作用

我在页面上有一个自定义下拉列表,页面名称为
WebForm1
。在
WebForm1
上,我有一个对它的程序集引用:

然后我有一个静态方法,如下所示:

    public static void Populate_Country_DDL(System.Web.UI.Page page)
        {
            GroupDropDownList
                .GroupDropDownList ddlCountry =
 ((GroupDropDownList.GroupDropDownList)page.FindControl("ddlCountry"));

            using (DataContext db = new DataContext())
            {
                var country = db.Countries.OrderBy(x => x.Text);

                ddlCountry.DataSource = country; //Error here
Utility.Populate_Country_DDL(this.Page);
Utility.Populate_Country_DDL(
                (ContentPlaceHolder)this.Master
                .FindControl("ContentPlaceHolder1"));
WebForm1
上,我试图访问静态方法,如下所示:

    public static void Populate_Country_DDL(System.Web.UI.Page page)
        {
            GroupDropDownList
                .GroupDropDownList ddlCountry =
 ((GroupDropDownList.GroupDropDownList)page.FindControl("ddlCountry"));

            using (DataContext db = new DataContext())
            {
                var country = db.Countries.OrderBy(x => x.Text);

                ddlCountry.DataSource = country; //Error here
Utility.Populate_Country_DDL(this.Page);
Utility.Populate_Country_DDL(
                (ContentPlaceHolder)this.Master
                .FindControl("ContentPlaceHolder1"));
问题是我得到了一个“对象引用未设置为对象实例”错误。我在实用程序类中为
GroupDropDownList

GroupDropDownList
只允许我将选项组放入下拉列表中

更新:为了解决这个问题,我修改了我的实用工具方法,将
ContentPlaceHolder
类改为
Page
类,然后在调用方法中,我在当前页面母版页中找到了ContentPlaceHolder,如下所示:

    public static void Populate_Country_DDL(System.Web.UI.Page page)
        {
            GroupDropDownList
                .GroupDropDownList ddlCountry =
 ((GroupDropDownList.GroupDropDownList)page.FindControl("ddlCountry"));

            using (DataContext db = new DataContext())
            {
                var country = db.Countries.OrderBy(x => x.Text);

                ddlCountry.DataSource = country; //Error here
Utility.Populate_Country_DDL(this.Page);
Utility.Populate_Country_DDL(
                (ContentPlaceHolder)this.Master
                .FindControl("ContentPlaceHolder1"));

我认为问题在于没有找到您在页面上查看的控件。您确定名称正确吗

FindControl
方法不在控件层次结构中,因此您必须编写自己的方法来搜索页面上的所有控件

例如,递归版本:

    public static Control FindControlRecursively(Control parent, string id)
    {
        Control control = parent.FindControl(id);

        if (control != null)
        {
            return control;
        }
        else
        {
            foreach (Control childControl in parent.Controls)
            {
                control = FindControlRecursively(childControl, id);
                if (control != null)
                {
                    return control;
                }
            }
        }

        return null;
    }
或无递归的版本:

    public static Control DeepFindControl(Control parent, string id)
    {
        Queue<Control> queue = new Queue<Control>();

        queue.Enqueue(parent);

        while (queue.Count > 0)
        {
            Control currentParent = queue.Dequeue();

            Control control = currentParent.FindControl(id);

            if (control != null)
            {
                return control;
            }

            foreach (Control childControl in parent.Controls)
            {
                queue.Enqueue(childControl);
            }
        }

        return null;
    }
公共静态控件DeepFindControl(控件父级,字符串id)
{
队列=新队列();
队列。排队(父级);
而(queue.Count>0)
{
Control currentParent=queue.Dequeue();
Control=currentParent.FindControl(id);
if(控件!=null)
{
返回控制;
}
foreach(父控件中的控件childControl)
{
排队(儿童控制);
}
}
返回null;
}

我认为问题在于没有找到您在页面上查看的控件。您确定名称正确吗

FindControl
方法不在控件层次结构中,因此您必须编写自己的方法来搜索页面上的所有控件

例如,递归版本:

    public static Control FindControlRecursively(Control parent, string id)
    {
        Control control = parent.FindControl(id);

        if (control != null)
        {
            return control;
        }
        else
        {
            foreach (Control childControl in parent.Controls)
            {
                control = FindControlRecursively(childControl, id);
                if (control != null)
                {
                    return control;
                }
            }
        }

        return null;
    }
或无递归的版本:

    public static Control DeepFindControl(Control parent, string id)
    {
        Queue<Control> queue = new Queue<Control>();

        queue.Enqueue(parent);

        while (queue.Count > 0)
        {
            Control currentParent = queue.Dequeue();

            Control control = currentParent.FindControl(id);

            if (control != null)
            {
                return control;
            }

            foreach (Control childControl in parent.Controls)
            {
                queue.Enqueue(childControl);
            }
        }

        return null;
    }
公共静态控件DeepFindControl(控件父级,字符串id)
{
队列=新队列();
队列。排队(父级);
而(queue.Count>0)
{
Control currentParent=queue.Dequeue();
Control=currentParent.FindControl(id);
if(控件!=null)
{
返回控制;
}
foreach(父控件中的控件childControl)
{
排队(儿童控制);
}
}
返回null;
}
来自:

FindControl方法可用于 访问ID不为的控件 在设计时可用。方法 仅搜索页面的即时链接,或 顶层,容器它没有 在中递归搜索控件 命名包含在 页在中访问控件 从属命名容器,调用 查找该容器的控制方法。

只能在要查找的控件的直接父控件上使用
FindControl()
,否则它将从以下位置返回
null

FindControl方法可用于 访问ID不为的控件 在设计时可用。方法 仅搜索页面的即时链接,或 顶层,容器它没有 在中递归搜索控件 命名包含在 页在中访问控件 从属命名容器,调用 查找该容器的控制方法。


只能在要查找的控件的直接父控件上使用
FindControl()
,否则它将返回
null

请发布方法的完整版本并指定在哪一行获得异常。请发布方法的完整版本并指定在哪一行获得异常。因此,如果
WebForm1
位于母版页内,则我需要找到该母版页的内容占位符?如果
WebForm1
在母版页中,那么我需要找到该母版页的contentnt占位符?非递归方法很有趣。有什么理由用一个来代替另一个吗?@Nuick:这只是遍历控件树的不同方式。在这种情况下,我会说你选择哪一个没有区别。@Nick:你也可以看到递归与循环的问题:非递归方法很有趣。有什么理由用一个来代替另一个吗?@Nuick:这只是遍历控件树的不同方式。在这种情况下,我会说你选择哪一个没有区别。@Nick:你也可以看到递归和循环的问题: