How can I access a control on an aspx page from the cs file in a programmatic way?
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?
I tried to use FindControl() as shown and it returns null.
aspx page:
<asp:Panel ID="atlanta" runat="server" Visible="false"></asp:Panel>
cs file:
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. In my case its just a simple content page with a content tag with a bunch of panels in it.
FindControl()isn’t recursive, which may be your problem. However, there are many implementations of a recursive version, such as this one.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.