简体   繁体   English

我如何在后面的代码中获得网站另一页的所有控制权

[英]How can i get all control in another page of site in code behind

How can I get all control in another page of my site in code behind. 我如何在后面的代码中获得网站另一页中的所有控件。
For example I'm in Default.aspx page and I have an ASP Button. 例如,我在Default.aspx页中,并且有一个ASP按钮。 I want, when I click on Button get list of all ASP Control in Dashboard.aspx (Like Button , TextBox , DropDownList , etc) without opening Dashboard.aspx 我想要当我单击Button时获得Dashboard.aspx中所有ASP控件的列表(如ButtonTextBoxDropDownList等),而无需打开Dashboard.aspx
page in Browser. 浏览器中的页面。

Note: 注意:
1. Getting all Control process must be in Code Behind. 1.获取所有控制过程必须在代码隐藏中。
2. I don't want open Dashboard.aspx page in browser. 2.我不想在浏览器中打开Dashboard.aspx页面。

I use HttpWebRequest to solve this problem. 我使用HttpWebRequest来解决此问题。

In Default.aspx page when Clicked on button run this code : Default.aspx页面中, Clicked按钮时,运行以下代码:

byte[] dataArray = Encoding.UTF8.GetBytes("");

//url = "http://localhost:50036/UI/Dashboard.aspx?Action=FindControl"
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";

httpRequest.ContentLength = dataArray.Length;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(dataArray, 0, dataArray.Length);
requestStream.Flush();
requestStream.Close();

HttpWebResponse webResponse = (HttpWebResponse)httpRequest.GetResponse();

if (httpRequest.HaveResponse == true)
{
      Stream responseStream = webResponse.GetResponseStream();
      StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
      String responseString = responseReader.ReadToEnd();
      /*
          In responseString string i have all control and types seperated by `semicolon`(`;`)
      */
}
else
      Console.Write("no response");

In this code url Variable contain url of Dashboard.aspx 在此代码中, url变量包含Dashboard.aspx URL

Note:url must contain http:// otherwise doesn't work 注意:网址必须包含http://否则将不起作用

In Dashboard.aspx page in Page_Load write this code: Page_Load Dashboard.aspx页中编写以下代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["Action"] != null && Request.QueryString["Action"].ToString() == "FindControl")
    {
         HttpContext.Current.Response.Write(ControlsList(this));
         HttpContext.Current.Response.End();
    }
}

public void ControlsList(Control parent)
    {
        string ans = "";
        foreach (Control c in parent.Controls)
        {
            if (c is TextBox || c is Button || c is DropDownList || c is CheckBox || c is RadioButton || c is CheckBoxList || c is RadioButtonList || c is ImageButton || c is LinkButton)
            {
                if(c.ID != null && c.ID != "")
                ans +=c.ID + "," + ((System.Reflection.MemberInfo)(c.GetType().UnderlyingSystemType)).Name + ";";
            }
            ans += ControlsList(c);
        }
        return ans;
    }

In Page_Load check Action=FindControl then find all control with specified Type with recursive function ControlsList and write it to response to use in Default.aspx Page_Load检查Action=FindControl然后使用递归函数ControlsList查找具有指定Type的所有控件,并将其写入响应以在Default.aspx使用

Its completely work for me! 它完全为我工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM