Page.Controls 母版页无法正常工作
Page.Controls not working correctly with masterpage
我有一个class,用于将所有页面控件的名称保存到数据库中。我们在第一次创建页面时使用它来设置翻译控件。
class 被发送到页面控件集合,然后它循环遍历每个页面,但是由于将所有页面附加到母版页,页面控件集合仅包含存在的四个内容区域并且不拾取任何内容else 在里面。
我已经尝试了几种方法来尝试解决这个问题,例如使用 div 或表格,但没有奏效,有人可以解释如何从母版页继承的页面中获取 Pages.Controls 吗?
我试过这个问题的答案:Loop through all controls on asp.net webpage
我从页面中获取的控件实际上似乎不包含子控件,因此无法将它们添加到列表中:
List<Control> foundsofar = null;
foreach (Control control in page)
{
foreach (Control c in control.Controls)
{
if (c is Control)
{
foundsofar.Add(c);
}
}
}
调用 class:
ArrayList PageObjects = GetPageControlIDs.AddControls(Page.Controls, array, constPageID);
添加控件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;
}
}
}
要从页面本身查找母版页控件,请使用以下代码。
此代码段将在页面代码中。
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;
}
}
}
}
由于 System.Web.UI.MasterPage 是从 UserControl 继承的,它具有 UserControl 可用的所有属性和方法。
要使其成为可重复使用的功能,有很多方法。
希望对您有所帮助!!!
希望这次我理解了你的问题:)
我最终找到了解决这个问题的方法,但无法找到一种简单的方法来获取所有控件。我不得不在其他控件中寻找它们,最终在这里找到了它们:
ArrayList PageObjects = GetPageControlIDs.AddControls(this.Controls[0].Controls[0].Controls[3].Controls[13].Controls[1].Controls[3].Controls, array, constPageID);
我有一个class,用于将所有页面控件的名称保存到数据库中。我们在第一次创建页面时使用它来设置翻译控件。
class 被发送到页面控件集合,然后它循环遍历每个页面,但是由于将所有页面附加到母版页,页面控件集合仅包含存在的四个内容区域并且不拾取任何内容else 在里面。
我已经尝试了几种方法来尝试解决这个问题,例如使用 div 或表格,但没有奏效,有人可以解释如何从母版页继承的页面中获取 Pages.Controls 吗?
我试过这个问题的答案:Loop through all controls on asp.net webpage
我从页面中获取的控件实际上似乎不包含子控件,因此无法将它们添加到列表中:
List<Control> foundsofar = null;
foreach (Control control in page)
{
foreach (Control c in control.Controls)
{
if (c is Control)
{
foundsofar.Add(c);
}
}
}
调用 class:
ArrayList PageObjects = GetPageControlIDs.AddControls(Page.Controls, array, constPageID);
添加控件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;
}
}
}
要从页面本身查找母版页控件,请使用以下代码。
此代码段将在页面代码中。
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;
}
}
}
}
由于 System.Web.UI.MasterPage 是从 UserControl 继承的,它具有 UserControl 可用的所有属性和方法。
要使其成为可重复使用的功能,有很多方法。
希望对您有所帮助!!! 希望这次我理解了你的问题:)
我最终找到了解决这个问题的方法,但无法找到一种简单的方法来获取所有控件。我不得不在其他控件中寻找它们,最终在这里找到了它们:
ArrayList PageObjects = GetPageControlIDs.AddControls(this.Controls[0].Controls[0].Controls[3].Controls[13].Controls[1].Controls[3].Controls, array, constPageID);