我有一个类,用于将所有页面控件的名称保存到数据库中。我们在首次创建页面来设置翻译控件时使用它。
该类被发送到页面控件集合,然后循环遍历每个页面,但由于将所有页面附加到母版页,页面控件集仅包括存在的四个内容区域,并且不会拾取其中的任何其他内容。
我尝试了几个尝试修复此问题,比如使用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);
}
}
}
致电课程:
ArrayList PageObjects = GetPageControlIDs.AddControls(Page.Controls, array, constPageID);
添加控件类:
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;
}
}
}
答案 0 :(得分:0)
我最终找到了解决方案,但无法找到获得所有控件的简单方法。我不得不在其他控件中寻找它们并最终在这里找到它们:
ArrayList PageObjects = GetPageControlIDs.AddControls(this.Controls[0].Controls[0].Controls[3].Controls[13].Controls[1].Controls[3].Controls, array, constPageID);
答案 1 :(得分:-2)
要从页面本身查找母版页控件,请使用以下代码。
此代码段位于代码页。
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提供的所有属性和方法。
要将其作为可重复使用的功能,有很多方法。
希望这会有所帮助!!! 希望这次能理解你的问题:)