且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从当前页面的其他页面获取所有服务器端控件

更新时间:2023-12-01 18:29:46





要实现这一目标,您需要加载您需要遍历所有控件的页面。



在您的情况下,您希望找到所有控件页面B中的A.aspx页面。因此,在页面B上,您需要加载页面A.aspx,以便必须呈现页面的所有控件,并且您可以找到控件ID。



代码:



B.aspx.cs

----------

 使用 System.Web.Compilation; 





页面页面=(页面)BuildManager.CreateInstanceFromVirtualPath( 〜/ A .aspx typeof (页面)); 
page.ProcessRequest(HttpContext.Current);







 LoopingControls(页面); 







LoopingControls方法的定义:



声明全局ArrayList对象 - oArrayList;



  public   void  LoopingControls(Control oControl)
{
oArrayList = new ArrayList ();
foreach (控制frmCtrl in oControl.Controls)
{
if (frmCtrl TextBox)
{
oArrayList.Add( new UtilityObj(frmCtrl.ID,((TextBox)frmCtrl).Text));
}
if (frmCtrl.HasControls())
{
LoopingControls(frmCtrl);
}
}
}







我创建了实体要捕获的类 - 控制ID和文本:



  public   class  UtilityObj 
{
private string _name;
private string _value;
public UtilityObj( string 名称, string 价值)
{
_name =姓名;
_value = Value;
}
public string 名称
{
get { return _name; }
set {_ name = Name; }
}
public string 价值
{
get { return (_ value); }
set {_ value = value ; }
}
}









最终完整代码:





 使用系统; 
使用 System.Collections;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Compilation;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 WebApplication3
{
public partial class GetPageControls:System.Web.UI.Page
{
ArrayList oArrayList;
受保护 void Page_Load( object sender,EventArgs e)
{
Page page =(Page)BuildManager.CreateInstanceFromVirtualPath( 〜/ A.aspx typeof (页面));
page.ProcessRequest(HttpContext.Current);

LoopingControls(页);
}

public void LoopingControls(Control oControl)
{
oArrayList = new ArrayList();
foreach (控制frmCtrl in oControl.Controls)
{
if (frmCtrl TextBox)
{
oArrayList.Add( new UtilityObj(frmCtrl.ID,((TextBox)frmCtrl).Text));
}
if (frmCtrl.HasControls())
{
LoopingControls(frmCtrl);
}
}
}
}

public class UtilityObj
{
private string _name;
private string _value;
public UtilityObj( string 名称, string 价值)
{
_name =姓名;
_value = Value;
}
public string 名称
{
get { return _name; }
set {_ name = Name; }
}
public string 价值
{
get { return (_ value); }
set {_ value = value ; }
}
}
}







谢谢,


I want to get all control's ID from a page in another page.

suppose two pages A.aspx and B.aspx. I want want fetch all controls from page B.aspx into A.aspx page


public partial clas PageA:System.Web.UI.Page
{
    //all serverside controls of PageB must be listed here.
}

public partial clas PageB:System.Web.UI.Page
{

}



What I have tried:

I have no clues how can I fix this problem,help needed.

Hi,

To achieve this you need to load that page to whom you need to traverse all the controls.

In your case you want to find all the controls of Page "A.aspx" from Page "B". So, on Page "B" you need to load Page "A.aspx", so that all the controls of the page must be rendered and you can find the control ids.

Code:

B.aspx.cs
----------
using System.Web.Compilation;



Page page = (Page)BuildManager.CreateInstanceFromVirtualPath("~/A.aspx", typeof(Page));
            page.ProcessRequest(HttpContext.Current);




LoopingControls(page);




Definition of LoopingControls method:

Declare global ArrayList object - oArrayList;

public void LoopingControls(Control oControl)
        {
            oArrayList = new ArrayList();
            foreach (Control frmCtrl in oControl.Controls)
            {
                if (frmCtrl is TextBox)
                {
                    oArrayList.Add(new UtilityObj(frmCtrl.ID, ((TextBox)frmCtrl).Text));
                }
                if (frmCtrl.HasControls())
                {
                    LoopingControls(frmCtrl);
                }
            }
        }




I have created Entity class to capture - Control ID and Text:

public class UtilityObj
    {
        private string _name;
        private string _value;
        public UtilityObj(string Name, string Value)
        {
            _name = Name;
            _value = Value;
        }
        public string Name
        {
            get { return _name; }
            set { _name = Name; }
        }
        public string Value
        {
            get { return (_value); }
            set { _value = value; }
        }
    }





Final Full Code:


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class GetPageControls : System.Web.UI.Page
    {
        ArrayList oArrayList;
        protected void Page_Load(object sender, EventArgs e)
        {
            Page page = (Page)BuildManager.CreateInstanceFromVirtualPath("~/A.aspx", typeof(Page));
            page.ProcessRequest(HttpContext.Current);

            LoopingControls(page);
        }

        public void LoopingControls(Control oControl)
        {
            oArrayList = new ArrayList();
            foreach (Control frmCtrl in oControl.Controls)
            {
                if (frmCtrl is TextBox)
                {
                    oArrayList.Add(new UtilityObj(frmCtrl.ID, ((TextBox)frmCtrl).Text));
                }
                if (frmCtrl.HasControls())
                {
                    LoopingControls(frmCtrl);
                }
            }
        }
    }

    public class UtilityObj
    {
        private string _name;
        private string _value;
        public UtilityObj(string Name, string Value)
        {
            _name = Name;
            _value = Value;
        }
        public string Name
        {
            get { return _name; }
            set { _name = Name; }
        }
        public string Value
        {
            get { return (_value); }
            set { _value = value; }
        }
    }
}




Thanks,