C# 页面。控件不能正确使用母版页
我有一个类,用于将所有页面控件的名称保存到数据库中。我们在第一次创建页面时使用它来设置翻译控件 该类被发送到pages controls集合,然后在其中循环遍历每一个页面,但由于将所有页面附加到母版页,pages controlcollection只包含存在的四个内容区域,而不拾取其中的任何其他内容 我尝试过几种方法来解决这个问题,比如使用div或tables,但都不起作用,有人能解释一下如何获取页面吗?从母版页继承的页面控件 我试过这个问题的答案: 我从第页获取的控件实际上似乎不包含子控件,因此无法将它们添加到列表中:C# 页面。控件不能正确使用母版页,c#,asp.net,class,webforms,controls,C#,Asp.net,Class,Webforms,Controls,我有一个类,用于将所有页面控件的名称保存到数据库中。我们在第一次创建页面时使用它来设置翻译控件 该类被发送到pages controls集合,然后在其中循环遍历每一个页面,但由于将所有页面附加到母版页,pages controlcollection只包含存在的四个内容区域,而不拾取其中的任何其他内容 我尝试过几种方法来解决这个问题,比如使用div或tables,但都不起作用,有人能解释一下如何获取页面吗?从母版页继承的页面控件 我试过这个问题的答案: 我从第页获取的控件实际上似乎不包含子控件,因
List<Control> foundsofar = null;
foreach (Control control in page)
{
foreach (Control c in control.Controls)
{
if (c is Control)
{
foundsofar.Add(c);
}
}
}
添加控件类:
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);
这个问题是否可能重复?我已经试过了,我已经更新了我的答案来解释。我不是在寻找所有使用母版页的页面。我正在查找从母版页继承的页面中的所有asp.net控件。无论如何,我无法理解这个答案,因为regex是一个用于验证字符串的工具。我以为您需要解决方案中使用特定母版页的所有控件信息。。。。。如果您只想在从母版页继承的页面中使用母版页控件,请使用以下代码。。。。我希望这次我能理解你的问题。this.Master.Controls只接受母版页中使用的控件,这是我已经能够做到的。我需要从使用母版的页面获取控件。这意味着除了内容区域之外,显然还有更多的控件。
ArrayList PageObjects = GetPageControlIDs.AddControls(this.Controls[0].Controls[0].Controls[3].Controls[13].Controls[1].Controls[3].Controls, array, constPageID);