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;
}
}
}