如何列出应用程序/Web解决方案中所有页面上可用的所有控件? [英] How to List All Controls Available on all pages with in an Application / Web Solution?
问题描述
如何基于用户页面选择列出基于Web的应用程序/Web解决方案中每个页面上可用的所有Web服务器控件?
对于实例:从下拉列表中选择任何页面名称. (假设下拉列表中填充了Web解决方案中所有可用的页面名称),它将显示指定页面上所有可用的控件.
页面名称:AboutUs.aspx
页面上可用的控件:AboutUs.aspx
-----------------------
控件ID
-----------------------
文本框:txtUserName
复选框:chkMyStatus
现在,从下拉菜单更改页面名称:
页面名称:Home.aspx
页面上可用的控件:Home.aspx
-----------------------
控件ID
-----------------------
文本框ID:txtAge
Gridview:Gridview1
还应显示指定控件的所有属性.
谢谢.
How to List all the Web Server Controls available on each and every Page with in an Web Based Application / Web Solution based on user Page selection?
For Instance : Select Any Page Name from Dropdown. (Suppose Dropdown is filled with all Page Names available in a web solution) and It will show all the Controls available on specified page.
Page Name : AboutUs.aspx
Controls Available on Page : AboutUs.aspx
-----------------------
Controls ID
-----------------------
Textbox : txtUserName
CheckBox : chkMyStatus
Now Change the Page Name from Dropdown :
Page Name : Home.aspx
Controls Available on Page : Home.aspx
-----------------------
Controls ID
-----------------------
Textbox Id : txtAge
Gridview : Gridview1
Also It Should show all the Properties of Specified Controls.
Thanks in Adv.
推荐答案
步骤:
1.根据所选的PageName,获取该页面
2.遍历该页面的所有控件
Steps:
1. Based on the PageName selected, Get that page
2. Loop through all the controls of that page
foreach (Control c in Page.Controls)
{
// control c
// c.ID;
// c.GetType
// all control properties accessible
// store this in a table
}
3.维护一张桌子.存储所有需要的东西.
4.在Datagrid中显示表格.
您必须设计您的网站,以使页面从某些BasePageClass派生,以便您轻松使用页面引用.
3. Maintain a table. Store all the things needed.
4. display the table in a Datagrid.
You have to design your website such that the pages are derived from some BasePageClass such that having a page reference is easy for you.
步骤1.下拉列表中的所有页面名称
在aspx页上添加ddlPageNames下拉列表
Step 1. All the Page Names in Dropdown
Add ddlPageNames Dropdown on aspx Page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(@"D:\WebSite1\", "*.aspx");
foreach (String item in filePaths)
{
char c = '\\';
String[] Filter = item.Split(c);
ddlPageNames.Items.Add(Filter[2]);
}
}
}
步骤2.创建一个示例页面,您可以在其中使用iFrame对象加载另一个页面,并收集所有控件属性并将其保存到Session中.
Step 2. Create a Sample Page where you can load another page with in using iFrame Object and Collect all the Control Properties and Save them into Session.
<p>
<iframe id="I1" runat="server" name="I1" width="0" height="0" src="Default.aspx" visible="true">
</iframe>
<asp:DropDownList ID="ddlPageNames" runat="server" AutoPostBack="true" onselectedindexchanged="ddlPageNames_SelectedIndexChanged"></asp:DropDownList>
<asp:Button ID="btnListControls" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="TotalControls" runat="server"></asp:Label>
<asp:GridView ID="GridView1" runat="server"
onpageindexchanged="GridView1_PageIndexChanged"
onpageindexchanging="GridView1_PageIndexChanging"
onrowdatabound="GridView1_RowDataBound" >
</asp:GridView>
</p>
protected void btnListControls_Click(object sender, EventArgs e)
{
DataView dt;
dt = (DataView)Session["dtData"];
if (dt != null)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void ddlPageNames_SelectedIndexChanged(object sender, EventArgs e)
{
I1.Attributes["src"] = Convert.ToString(ddlPageNames.SelectedValue);
}
步骤3.现在,将存储的控件名称绑定到Gridview中以列出所有列表.
例如:
假设您要列出Default.aspx页上所有可用的控件,然后按照
Step 3. Now Bind the Stored Control Names into Gridview to List all.
For Instance :
Suppose You Want to List all Controls Available on Default.aspx Page then Follow
DataTable dt = new DataTable();
DataView objDV = new DataView();
protected void Page_Load(object sender, EventArgs e)
{
dt.Columns.Add("Ctrl", typeof(string));
dt.Columns.Add("Name", typeof(string));
foreach (Control ParentMain in Page.Controls)
{
#region //CurrentPage
Page objpage = new Page();
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
//txtControlsList.Text = Convert.ToString(txtControlsList.Text) + sRet + "\n";
//txtControlsList.Text = Convert.ToString(txtControlsList.Text) + ParentMain.ID+"\n";
foreach (Control Child1 in ParentMain.Controls)
{
//txtControlsList.Text = Convert.ToString(txtControlsList.Text) + Child1.ID + "\n";
FillControlList(Child1);
foreach (Control Child2 in Child1.Controls)
{
//txtControlsList.Text = Convert.ToString(txtControlsList.Text) + Child2.ID + "\n";
FillControlList(Child2);
if (Convert.ToString(Child2.ID) == "GridView1")
{
GridView myGridView1 = (GridView)Page.FindControl("GridView1");
}
#region
foreach (Control Child3 in Child2.Controls)
{
//txtControlsList.Text = Convert.ToString(txtControlsList.Text) + Child3.ID + "\n";
FillControlList(Child3);
if (Convert.ToString(Child3.ID) == "Button1")
{
Child3.Visible = false;
}
foreach (Control Child4 in Child3.Controls)
{
FillControlList(Child4);
if (Convert.ToString(Child4.ID) == "Button1")
{
Child4.Visible = false;
}
foreach (Control Child5 in Child4.Controls)
{
FillControlList(Child5);
if (Convert.ToString(Child5.ID) == "Button1")
{
Child5.Visible = false;
}
}
}
}
#endregion
}
}
#endregion
}
objDV = dt.DefaultView;
Session["dtData"] = objDV;
}
Next
public void FillControlList(Control Ctrl)
{
ListItem li = new ListItem();
li.Text = Convert.ToString(Ctrl.ID);
li.Value = Convert.ToString(Ctrl.ID);
if (!string.IsNullOrEmpty(Convert.ToString(li.Text)))
{
DataRow row = dt.NewRow();
row["Ctrl"] = Ctrl.ID;
dt.Rows.Add(row);
dt.Rows[dt.Rows.Count - 1][1] = Ctrl.GetType().Name;
}
}
这篇关于如何列出应用程序/Web解决方案中所有页面上可用的所有控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!