0

I have a class which is used to save the name of all page controls into a database. We use it when we first create a page to setup the controls for translations.

The class is sent the pages controls collection, where it then loops through each one but since attaching all pages to a masterpage, the pages controlcollection only includes the four content areas that exist and does not pickup anything else inside them.

I have tried several things to try and fix this like using divs or tables but it has not worked, can someone explain how to get the Pages.Controls from a page inherited from a master page?

I have tried the answer from this question: Loop through all controls on asp.net webpage

The controls that I took from page, seemed to contain no child controls in practice so was unable to add them to a list:

List<Control> foundsofar = null;
                    foreach (Control control in page)
                    {


                        foreach (Control c in control.Controls)
                        {
                            if (c is Control)
                            {
                                foundsofar.Add(c);
                            }

                        }
                    }

Call to the class:

ArrayList PageObjects = GetPageControlIDs.AddControls(Page.Controls, array, constPageID);

Add controls class:

 static public ArrayList AddControls(ControlCollection page, ArrayList controlList, int PageID)
        {
            if (ObjectSetupSwitch == 1)
            {

                {

                    foreach (Control control in page)
                    {
                        if (control is Button || control is TextBox || control is Label)
                        {// This is cleaner
                            string ControlText = "";
                            string DescText = "";
                            if (control is Button)
                            {
                                Button btnNew = (Button)control;
                                ControlText = btnNew.Text;
                                DescText = btnNew.Text + " Button";
                            }
                            else if (control is TextBox)
                            {
                                TextBox txtNew = (TextBox)control;
                                ControlText = txtNew.Text;
                                DescText = txtNew.Text + " Textbox";
                            }
                            else if (control is Label)
                            {
                                Label lblNew = (Label)control;
                                ControlText = lblNew.Text;
                                DescText = lblNew.Text + " Label";
                            }
                            controlList.Add(control);

                            if (control.ID != null && control.ID != " ")
                            {
                                using (SqlConnection conn = new SqlConnection(GetConnection.GetConnectionString()))
                                {
                                    DataSet ds = new DataSet();
                                    SqlCommand sqlComm = new SqlCommand("PL_Objects_Insert", conn);
                                    sqlComm.Parameters.AddWithValue("@ControlID", control.ID.ToString());
                                    sqlComm.Parameters.AddWithValue("@PageID", PageID);
                                    sqlComm.Parameters.AddWithValue("@Text", ControlText);
                                    sqlComm.Parameters.AddWithValue("@DescText", DescText);


                                    sqlComm.CommandType = CommandType.StoredProcedure;

                                    SqlDataAdapter da = new SqlDataAdapter();
                                    da.SelectCommand = sqlComm;

                                    da.Fill(ds);
                                }
                            }
                        }



                        if (control.HasControls())
                            AddControls(control.Controls, controlList, PageID);
                    }


                    //Do it again for the tooltips
                    foreach (Control control in page)
                    {
                        if (control is Button || control is TextBox || control is ImageButton)
                        {// This is cleaner
                            string ControlText = "";
                            string DescText = "";
                            if (control is Button)
                            {
                                Button btnNew = (Button)control;
                                ControlText = btnNew.ToolTip;
                                DescText = btnNew.ToolTip + " Button Tooltip";
                            }
                            else if (control is ImageButton)
                            {
                                ImageButton btnNew = (ImageButton)control;
                                ControlText = btnNew.ToolTip;
                                DescText = btnNew.ToolTip + " ImageButton Tooltip";
                            }
                            else if (control is TextBox)
                            {
                                TextBox txtNew = (TextBox)control;
                                ControlText = txtNew.ToolTip;
                                DescText = txtNew.ToolTip + " Textbox Tooltip";
                            }
                            controlList.Add(control);

                            if (control.ID != null && control.ID != " ")
                            {
                                using (SqlConnection conn = new SqlConnection(GetConnection.GetConnectionString()))
                                {
                                    DataSet ds = new DataSet();
                                    SqlCommand sqlComm = new SqlCommand("PL_Objects_Insert", conn);
                                    sqlComm.Parameters.AddWithValue("@ControlID", control.ID.ToString() + ".Tooltip");
                                    sqlComm.Parameters.AddWithValue("@PageID", PageID);
                                    sqlComm.Parameters.AddWithValue("@Text", ControlText);
                                    sqlComm.Parameters.AddWithValue("@DescText", DescText);


                                    sqlComm.CommandType = CommandType.StoredProcedure;

                                    SqlDataAdapter da = new SqlDataAdapter();
                                    da.SelectCommand = sqlComm;

                                    da.Fill(ds);
                                }
                            }
                        }



                        if (control.HasControls())
                            AddControls(control.Controls, controlList, PageID);
                    }

                    //Do it again for the RE validators
                    foreach (Control control in page)
                    {
                        if (control is TextBox)
                        {// This is cleaner
                            string ControlText = "";
                            string DescText = "";
                            if (control is TextBox)
                            {
                                TextBox txtNew = (TextBox)control;
                                ControlText = txtNew.ToolTip;
                                DescText = txtNew.ToolTip + " Textbox Tooltip";
                            }
                            controlList.Add(control);

                            if (control.ID != null && control.ID != " ")
                            {
                                using (SqlConnection conn = new SqlConnection(GetConnection.GetConnectionString()))
                                {
                                    DataSet ds = new DataSet();
                                    SqlCommand sqlComm = new SqlCommand("PL_Objects_Validator_Insert", conn);
                                    sqlComm.Parameters.AddWithValue("@ControlID", "REV" + control.ID.ToString());
                                    sqlComm.Parameters.AddWithValue("@ControlToValidate", control.ID.ToString());
                                    sqlComm.Parameters.AddWithValue("@PageID", PageID);
                                    sqlComm.Parameters.AddWithValue("@DescText", "RE Validator");


                                    sqlComm.CommandType = CommandType.StoredProcedure;

                                    SqlDataAdapter da = new SqlDataAdapter();
                                    da.SelectCommand = sqlComm;

                                    da.Fill(ds);
                                }
                            }
                        }

                        if (control.HasControls())
                            AddControls(control.Controls, controlList, PageID);
                    }

                    return controlList;
                }

            }
            else
            {
                return controlList;
            }
        }
    } 
Community
  • 1
  • 1
connersz
  • 1,153
  • 3
  • 23
  • 64

2 Answers2

0

I found a solution to this in the end but was unable to find an easy means to getting all of the controls. I had to go looking for them within the other controls and eventually found them here:

ArrayList PageObjects = GetPageControlIDs.AddControls(this.Controls[0].Controls[0].Controls[3].Controls[13].Controls[1].Controls[3].Controls, array, constPageID);
connersz
  • 1,153
  • 3
  • 23
  • 64
-2

To find master page controls from page itself use below code.

This snippet will be in page Code.

    protected void Page_Load(object sender, EventArgs e)
    {
        GetControl(this.Master.Controls);
    }

    private void GetControl(ControlCollection cc)
    {
        foreach (Control v in cc)
        {
            if (v.HasControls())
            {
                GetControl(v.Controls);
            }
            else
            {
                if (v is TextBox)
                {
                    string s = (v as TextBox).ID;
                }
            }
        }
    }

Since System.Web.UI.MasterPage is inherited from UserControl it has all properties and methods available from UserControl.

To make this as reusable functionality there are many ways.

Hope this helps!!! Hope I understood your question this time :)

Mitesh
  • 74
  • 1
  • 7
  • I'm not looking for all pages that use a master page. I'm looking for all asp.net controls within a page which is inherited from a master page. I couldn't make much sense out of this answer anyway as regex is a tool used to validate strings. – connersz Jan 21 '15 at 09:07
  • I thought you want all controls information from solution which are using particular master page..... If you want controls from master page only in the page which is inherited from master page please use below code.... I hope this time I understand your question. – Mitesh Jan 23 '15 at 22:22
  • This.Master.Controls Just takes the controls used in the masterpage, which is what I am already able to do. I need to take the controls from the page which uses the master. That means there will obviously be many more controls, other than the content areas. – connersz Jan 26 '15 at 09:11