Tech Off Thread

13 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Get controls on a page, that is using a masterpage

Back to Forum: Tech Off
  • User profile image
    Chadk

    I used to be able to do get all controls on a page, by looping trough Page.Controls[3].Controls (I think it was 3, not sure tho).

    But i tried to make it use a masterpage. But now the Page controls layout is completely different. I have been searching in the controls, and i havent been able to find my controls.

    Anyone that can help me? Where do i find my controls on the page?

  • User profile image
    blowdart

    Chadk wrote:
    I used to be able to do get all controls on a page, by looping trough Page.Controls[3].Controls (I think it was 3, not sure tho).

    But i tried to make it use a masterpage. But now the Page controls layout is completely different. I have been searching in the controls, and i havent been able to find my controls.

    Anyone that can help me? Where do i find my controls on the page?


    3?

    Wouldn't it be safest to do something like

    foreach controlsArray in Page.Controls
      foreach subArray in controlsArray ....

    etc. ?

  • User profile image
    Human​Compiler

    Page.FindControl ???  I wouldn't hardcode an index like that as you've found, layout can change and now your code is broken.

  • User profile image
    W3bbo

    HumanCompiler wrote:
    Page.FindControl ???  I wouldn't hardcode an index like that as you've found, layout can change and now your code is broken.


    Why use Page.FindControl when you can use Protected fields?

  • User profile image
    Human​Compiler

    W3bbo wrote:
    
    HumanCompiler wrote: Page.FindControl ???  I wouldn't hardcode an index like that as you've found, layout can change and now your code is broken.


    Why use Page.FindControl when you can use Protected fields?


    If you know that you're looking for, like a control inside of the controls collection of a control you've defined on the page, yes you'd use that field instead of Page, but I don't know their scenario, so that might not work for them.  And what about dynamically added controls?  There are no protected fields for those.

  • User profile image
    W3bbo

    HumanCompiler wrote:
    
    W3bbo wrote:
    HumanCompiler wrote: Page.FindControl ???  I wouldn't hardcode an index like that as you've found, layout can change and now your code is broken.


    Why use Page.FindControl when you can use Protected fields?


    If you know that you're looking for, like a control inside of the controls collection of a control you've defined on the page, yes you'd use that field instead of Page, but I don't know their scenario, so that might not work for them.  And what about dynamically added controls?  There are no protected fields for those.


    True, then there's ITemplate controls that don't work with Protected fields, period.

  • User profile image
    Chadk

    HumanCompiler wrote:
    Page.FindControl ???  I wouldn't hardcode an index like that as you've found, layout can change and now your code is broken.

    Yes, you are right. Maserpages did change the layout.

    I found a better solution. I put all the controls i wanted into a panel, and just take MyPanel.GetControls, which returns all of them.

  • User profile image
    MrFoote

    You still can loop through all the controls to find a specific one.

    Remember that the Master Page simple takes all the controls and inserts them inside the Content control inside of each page. Then you can find controls from the master page on your implemented page by looking inside the controls collection of the content pane.

    Alternatively, you can loop through ALL of the controls in the Page.Controls collection recursively. Here is a routine to do this:

     

        /// <summary>
        /// Returns control with ID = ControlName. Returns null when not found
        /// </summary>
        /// <param name="C"></param>
        /// <param name="ControlName"></param>
        /// <returns></returns>
        public static Control Find(Control C, String ControlName)
        {
            if (C.ID == ControlName) return C;
            foreach (Control c in C.Controls)
            {
                Control cntrl = Find(c, ControlName);
                if (cntrl != null) return cntrl;
            }
            return null;
        }

     

  • User profile image
    MrFoote

    P.S. Sample call might be

    TextBox someTextBox = (TextBox)Find(Page, "someTextBox");

  • User profile image
    Human​Compiler

    MrFoote, doesn't FindControl already do that?

  • User profile image
    MrFoote

    FindControl doesn't work with a master page.

    example:

    Create a master page:

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="TestMasterPage.master.cs" Inherits="TestMasterPage" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

    </asp:contentplaceholder>

    </div>

    </form>

    </body>

    </html>

    Then create a webpage which uses the master page (aspx) that includes a textbox and a label:

    <%@ Page Language="C#" MasterPageFile="~/TestMasterPage.master" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <asp:TextBox runat="server" ID="tb1"></asp:TextBox>

    <asp:Label ID="lbl1" runat="server"></asp:Label>

    </asp:Content>

    Then add the following code behind:


    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    public partial class test : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    Control c1 = FindControl("tb1");

    string s = "";

    if (c1 == null) { s = "FindControl couldn't find tb1!"; } else { s = "FindControl Found tb1!"; }

    Control c2 = Find(Page, "tb1");

    if (c2 == null) { s += "Find couldn't find tb1!"; } else { s += "Find Found tb1!";}

    lbl1.Text = s;

    }

    public static Control Find(Control C, String ControlName)

    {

    if (C.ID == ControlName) return C;

    foreach (Control c in C.Controls)

    {

    Control cntrl = Find(c, ControlName);

    if (cntrl != null) return cntrl;

    }

    return null;

    }

    }


    Output is:

    [A Textbox control] FindControl couldn't find tb1!Find Found tb1!
    Try it and see for yourself!



  • User profile image
    wyattwong

    I would like to find ALL controls in a page within a master page instead of providing a control ID to find in the page, how can I do so ?

  • User profile image
    JChung2006

    MrFoote wrote:
    FindControl doesn't work with a master page.

    Sure, it does.

    Get a reference to a page's master page via its Master property and then do FindControl recursively from the MasterPage's Controls collection, or if you have a general idea of your page's control hierarchy, you can navigate it with FindControl's yourself.

    Example:

    <% Master ... %>
    <html>
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" />
        </form>
    </body>
    </html>
    

    <% Page ... %>
    
    <asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1" />
         <asp:Label ID="Label1" runat="server" />
    </asp:Content>
    

    // C# code-behind for Page
    protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Control form = Master.FindControl("form1"); Control content1 = form.FindControl("ContentPlaceHolder1"); Label label = (Label)content1.FindControl("Label1"); label.Text = "Hello, world!"; // Or use $ syntax between control ID's Label sameLabel = (Label)Master.FindControl("form1$ContentPlaceHolder1$Label1"); } }

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.