Read / loop foreach all controls dynamically from code behind c# asp.net:
protected void Page_Load(object sender, EventArgse)
{
ListControlCollections();
}
{
ListControlCollections();
}
private void ListControlCollections()
{
Dictionary<string, string> diclist = new Dictionary<string, string>();
AddControls(Page.Controls, diclist);
foreach (KeyValuePair<string, string> item in diclist)
{
Response.Write(item.Key +"<--->" + item.Value);
}
}
private void AddControls(ControlCollection page, Dictionary<string, string> diclist)
{
foreach (Control c in page)
{
if (c.ID != null)
{
if (c is TextBox)
{
diclist.Add(c.ID,"text");
};
}
if (c.HasControls())
{
AddControls(c.Controls, diclist);
}
}
}
{
Dictionary<string, string> diclist = new Dictionary<string, string>();
AddControls(Page.Controls, diclist);
foreach (KeyValuePair<string, string> item in diclist)
{
Response.Write(item.Key +"<--->" + item.Value);
}
}
private void AddControls(ControlCollection page, Dictionary<string, string> diclist)
{
foreach (Control c in page)
{
if (c.ID != null)
{
if (c is TextBox)
{
diclist.Add(c.ID,"text");
};
}
if (c.HasControls())
{
AddControls(c.Controls, diclist);
}
}
}