简体   繁体   English

如何以编程方式从CS文件访问ASPX页面上的控件

[英]How to programmatically access a control on a aspx page from the cs file

How can I access a control on an aspx page from the cs file in a programmatic way? 如何以编程方式从cs文件访问aspx页面上的控件?

For instance, if I have a set of asp:Panel controls each with an ID named by a city (id="atlanta", id="chicago", id="pittsburgh", etc.) and then in the cs I grab a value from the database to match up to the control names what would I use? 例如,如果我有一组asp:Panel控件,每个控件都有一个以城市命名的ID(id =“ atlanta”,id =“ chicago”,id =“ pittsburgh”等),然后在CS中抓取数据库中与控件名称匹配的值,我将使用什么?

I tried to use FindControl() as shown and it returns null. 我尝试如图所示使用FindControl(),它返回null。

aspx page: aspx页面:

<asp:Panel ID="atlanta" runat="server" Visible="false"></asp:Panel>

cs file: cs文件:

controlName = storeLocation.City.ToLower();
Panel cityPanel = (Panel)FindControl(controlName);
cityPanel.Visible = true;

I suppose FindControl() is really for use in cases like Repeaters or Grids where you pass in the ItemTemaplate. 我想FindControl()确实适用于在ItemTemaplate中传递的Repeater或Grids之类的情况。 In my case its just a simple content page with a content tag with a bunch of panels in it. 在我的情况下,它只是一个简单的内容页面,其中包含带有一堆面板的内容标签。

Thanks in advanced! 提前致谢! :) :)

FindControl() isn't recursive, which may be your problem. FindControl()不是递归的,这可能是您的问题。 However, there are many implementations of a recursive version, such as this one . 但是,有许多递归版本的实现,例如this

If you are creating the panels dynamically and you want access to them later in the page lifecycle, you can add them all to a Dictionary<string, Panel> where the ID is the key. 如果要动态创建面板,并且希望在页面生命周期的后期访问它们,则可以将它们全部添加到Dictionary<string, Panel> ,其中ID是键。

You can use this extension method to find controls recursively: 您可以使用此扩展方法来递归查找控件:

public static class ControlExtension
{

    public static IEnumerable<Control> GetAllControls(this Control parent)
    {
        foreach (Control control in parent.Controls)
        {
            yield return control;
            foreach (Control child in control.GetAllControls())
            {
                yield return child;
            }
        }
    }
}

And then in your code behind page you could do something like this if you prefix your labels: 然后在页面后面的代码中,如果给标签加上前缀,则可以执行以下操作:

IEnumerable<Control> city_controls = this.GetAllControls().Where(x => x.Id.Contains("city_"))

or a single control: 或单个控件:

var city = this.GetAllControls().Single(x => x.Id = "atlanta");

Where the this is your ASPX code behind page. this是您的ASPX代码背后的页面。

It also works on controls such as panels, so if you want to find all controls inside a panel etc. 它也可以在面板等控件上使用,因此,如果要在面板中找到所有控件等。

If you know the containing control you may be able to use the FindControl() function of that control to find it. 如果您知道包含的控件,则可以使用该控件的FindControl()函数来查找它。 This method will be much faster and safer than using a recursive method. 与使用递归方法相比,此方法将更快,更安全。

Microsoft did not include a recursive FindControl() function for a reason, it will slow your page down over time as more controls get added to the page. Microsoft出于某种原因未包括递归的FindControl()函数,随着更多控件被添加到页面中,它将随着时间的流逝而降低页面的速度。 If that doesn't work use a recursive function like other responses suggested. 如果这样不起作用,请像建议的其他响应一样使用递归函数。

This should work for you since the panels are nested in the asp:Content control. 这应该为您工作,因为面板嵌套在asp:Content控件中。

    Panel cityPanel =
(Panel)Master.FindControl("ContentPlaceHolderId").FindControl(controlName);

Where off course, ContentPlaceHolderId is the Id of the corresponding ContentPlaceHolder in the Master page you are implementing. 当然,ContentPlaceHolderId是要实现的母版页中相应ContentPlaceHolder的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM