ASP。NET访问只使用一个母版实例的控件

时间:2021-02-25 15:52:51

I have a set of three MasterPages with different filters on them consisting of dropdownlists. When the page inits I want to fill these dropdowns with options using a helping class in the App_Code folder to decrease repeated code. The problem I have is that I cannot find these dropdowns in the MasterPage the method below receives. Can this be done or should I explore other options?

我有一组三个不同过滤器的MasterPages,它们由下拉列表组成。当页面inits时,我希望使用App_Code文件夹中的help类填充这些下拉列表,以减少重复代码。我遇到的问题是,我无法在下面的方法接收的主页中找到这些下拉列表。这是可以做到的,还是我应该探索其他的选择?

I have tried to first find the ContentPlaceHolder and then searching but cannot find the ContentPlaceHolder the dropdowns are in.
I have also tried to access the parent and from there find the ContentPlaceHolder but no dice.

我尝试先找到ContentPlaceHolder,然后再搜索,但无法找到下拉列表中的ContentPlaceHolder。我还尝试访问父进程,从那里找到ContentPlaceHolder,但没有找到骰子。

Other options being sending the dropdowns to this class and filling them, which is the easier solution. But I want to explore this very general solution first.

其他选项正在将下拉列表发送到这个类并填充它们,这是更简单的解决方案。但我想先探讨这个通解。

public static void FillFilters(MasterPage page) {

    DropDownList[] dropdowns = new DropDownList[3];
    dropdowns[0] = (DropDownList)page.FindControl("StatusDropDown");
    dropdowns[1] = (DropDownList)page.FindControl("DepartmentDropDown");
    dropdowns[2] = (DropDownList)page.FindControl("EmployeeDropDown");
    ...code filling the dropdowns etc. etc.

2 个解决方案

#1


2  

Try this extension method. You don't need the MasterPage, just that the control should parent of your current control

试试这个扩展方法。您不需要MasterPage,只需控制您当前控件的父控件。

public static class ControlsExtensionMethods
{
    public static Control FindControlRecursive(this Control root, string id)
    {
        if (id == string.Empty)
        {
            return null;
        }


        if (root.ID == id)
        {
            return root;
        }

        foreach (Control nestedControl in root.Controls)
        {
            Control foundedControlInNested = FindControlRecursive(nestedControl, id);
            if (foundedControlInNested != null)
            {
                return foundedControlInNested;
            }
        }
        return null;
    }
}

#2


0  

To add on to Juan answer (maybe this should be an edit?) I decided to use the following in my final code:

补充胡安回答(也许这应该是一个编辑?)我决定在我的最终代码中使用以下内容:

public static void FillFilters(MasterPage page) {
    Control parent = page.Controls[0];

    DropDownList[] dropdowns = new DropDownList[3];
    dropdowns[0] = (DropDownList)parent.FindControlRecursive("StatusDropDown");
    dropdowns[1] = (DropDownList)parent.FindControlRecursive("DepartmentDropDown");
    dropdowns[2] = (DropDownList)parent.FindControlRecursive("EmployeeDropDown");

Since I use the MasterPage later in the method.

因为稍后我在方法中使用母版。

#1


2  

Try this extension method. You don't need the MasterPage, just that the control should parent of your current control

试试这个扩展方法。您不需要MasterPage,只需控制您当前控件的父控件。

public static class ControlsExtensionMethods
{
    public static Control FindControlRecursive(this Control root, string id)
    {
        if (id == string.Empty)
        {
            return null;
        }


        if (root.ID == id)
        {
            return root;
        }

        foreach (Control nestedControl in root.Controls)
        {
            Control foundedControlInNested = FindControlRecursive(nestedControl, id);
            if (foundedControlInNested != null)
            {
                return foundedControlInNested;
            }
        }
        return null;
    }
}

#2


0  

To add on to Juan answer (maybe this should be an edit?) I decided to use the following in my final code:

补充胡安回答(也许这应该是一个编辑?)我决定在我的最终代码中使用以下内容:

public static void FillFilters(MasterPage page) {
    Control parent = page.Controls[0];

    DropDownList[] dropdowns = new DropDownList[3];
    dropdowns[0] = (DropDownList)parent.FindControlRecursive("StatusDropDown");
    dropdowns[1] = (DropDownList)parent.FindControlRecursive("DepartmentDropDown");
    dropdowns[2] = (DropDownList)parent.FindControlRecursive("EmployeeDropDown");

Since I use the MasterPage later in the method.

因为稍后我在方法中使用母版。