Hi Everyone can u help for finding the events in particular page by passing page name as parameter.here i tried using delegates but i am able to find events only for that particular page the code has written .but my problem is to find only System.Web.UI.WebControls events that are fired and assign it to listbox here iam attaching the code
public partial class _Default : System.Web.UI.Page
{
List<control> controls = new List<control>();
protected void Page_Load(object sender, EventArgs e)
{
var allTextBoxesOnThePage = Page.GetAllControlsOfType<textbox>();
var allHyperLinksInControl = Page.GetAllControlsOfType<hyperlink>();
var allButtonsInControl = Page.GetAllControlsOfType<Button>();
var allCheckboXeOnThePage = Page.GetAllControlsOfType<checkbox>();
controls.AddRange(allButtonsInControl);
controls.AddRange(allCheckboXeOnThePage);
controls.AddRange(allTextBoxesOnThePage);
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (var item in controls)
{
if (item is Button)
{
Delegate[] a = GetEventSubscribers(item, "Click");
TextBox1.Text += a[0].Method.ToString();
}
if (item is CheckBox)
{
Delegate[] a = GetEventSubscribers(item, "CheckedChanged");
TextBox1.Text += a[0].Method.ToString();
}
if (item is TextBox)
{
Delegate[] a = GetEventSubscribers(item, "TextChanged");
TextBox1.Text += a[0].Method.ToString();
}
}
}
public static Delegate[] GetEventSubscribers(object target, string eventName)
{
Type t = target.GetType();
var eventInfo = t.GetEvent(eventName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
do
{
FieldInfo[] fia = t.GetFields(
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.NonPublic);
foreach (FieldInfo fi in fia)
{
if (fi.Name == eventName)
{
Delegate d = fi.GetValue(target) as Delegate;
if (d != null)
return d.GetInvocationList();
}
else if (fi.FieldType == typeof(EventHandlerList))
{
var obj = fi.GetValue(target) as EventHandlerList;
var eventHandlerFieldInfo = obj.GetType().GetField("head", BindingFlags.Instance | BindingFlags.NonPublic);
do
{
var listEntry = eventHandlerFieldInfo.GetValue(obj);
var handler = listEntry.GetType().GetField("handler", BindingFlags.Instance | BindingFlags.NonPublic);
if (handler != null)
{
var subD = handler.GetValue(listEntry) as Delegate;
if (subD.GetType() != eventInfo.EventHandlerType)
{
eventHandlerFieldInfo = listEntry.GetType().GetField("next", BindingFlags.Instance | BindingFlags.NonPublic);
obj = listEntry as EventHandlerList;
continue;
}
if (subD != null)
{
return subD.GetInvocationList();
}
}
}
while (eventHandlerFieldInfo != null);
}
}
t = t.BaseType;
} while (t != null);
return new Delegate[] { };
}
protected void Button2_Click(object sender, EventArgs e)
{
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
}
public static class ControlExtensions
{
public static IEnumerable<t> GetAllControlsOfType<t>(this Control parent) where T : Control
{
var result = new List<t>();
foreach (Control control in parent.Controls)
{
if (control is T)
{
result.Add((T)control);
}
if (control.HasControls())
{
result.AddRange(control.GetAllControlsOfType<t>());
}
}
return result;
}
}