Introduction
A few days ago, someone asked me how to invoke a method in a WebUserControl that is being loaded programmatically. I took the opportunity to put together a sample code to provide the answer based on different access level. I hope you all will find this information useful. Sample code is available to download.Putting everything together
Shown in Listing 1 is the code behind of the WebUserControl.Listing 1
|
Here is the code to load the WebUserControl
Listing 2
|
Use the following code to retrieve the Control Type. Notice that the BaseType property is being used instead GetType() method itself in order to access private, static and constant fields using reflection.
Listing 3
Type cType = webControl.GetType().BaseType;Invoke the public method.
Listing 4
MethodInfo method = cType.GetMethod("Method1");
if (method != null)
{
method.Invoke(webControl, null);
}
Invoke the public method with one argument.
Listing 5
method = cType.GetMethod("MethodWithOneArg");
if (method != null)
{
method.Invoke(webControl, new object[] { "hello" });
}
Invoke the public method with multiple arguments.
Listing 6
method = cType.GetMethod("MethodWithTwoArg");
if (method != null)
{
method.Invoke(webControl, new object[] { "Number", 1000 });
}
Invoke private method in the WebUserControl.
Listing 7
method = cType.GetMethod("PrivateMethod", BindingFlags.NonPublic | BindingFlags.Instance);
if (method != null)
{
method.Invoke(webControl, null);
}
Invoke private method with multiple arguments in the WebUserControl
Listing 8
method = cType.GetMethod("PrivateMethodWithArgs", BindingFlags.NonPublic | BindingFlags.Instance);
if (method != null)
{
method.Invoke(webControl, new object[] { "arg1", 5555, "arg 2" });
}
Invoke protected method in the WebUserControl
Listing 9
method = cType.GetMethod("ProtectedMethod", BindingFlags.NonPublic | BindingFlags.Instance);
if (method != null)
{
method.Invoke(webControl, null);
}
Invoke internal method in the WebUserControl
Listing 10
method = cType.GetMethod("InternalMethod", BindingFlags.NonPublic | BindingFlags.Instance);
if (method != null)
{
method.Invoke(webControl, null);
}
Invoke overload method and capture the return value.
Listing 11
method = cType.GetMethod("MethodWithReturnValue", Type.EmptyTypes);
if (method != null)
{
rValue = method.Invoke(webControl, null) as string;
literal.Text += "Invoked overload method: " + rValue + "<br />";
}
Invoke overload method with argument and capture the return value.
Listing 12
method = cType.GetMethod("MethodWithReturnValue",
BindingFlags.Public | BindingFlags.Instance, null,new Type[]
{typeof(string) }, null);
if (method != null)
{
rValue = method.Invoke(webControl, new object[] { "there!" }) as string;
literal.Text += "Invoked overload method with arguments: " + rValue + "<br />";
}
Access the static field in the WebUserControl, get and set its value.
Listing 13
FieldInfo field = cType.GetField("staticVar");
if (field != null)
{
rValue2 = field.GetValue(null) as string;
literal.Text += "Static variable - before modify: " + rValue + "<br />";
field.SetValue(null, "New static value");
rValue2 = field.GetValue(null) as string;
literal.Text += "Static variable - after modified: " + rValue2 + "<br />";
}
Access the constant field in the WebUserControl.
Listing 14
field = cType.GetField("constVar");
if (field != null)
{
rValue = field.GetValue(null) as string;
literal.Text += "Constant variable: " + rValue + "<br />";
}
Get and set the property in the WebUserControl through reflection.
Listing 15
PropertyInfo property = cType.GetProperty("Text");
if (property != null)
{
//set
property.SetValue(webControl, "Textbox value", null);
//get
rValue = property.GetValue(webControl, null) as string;
literal.Text += "Set Textbox value to: " + rValue + "<br />";
}
Get controls by ID and retrieve its value.
Listing 16
field = cType.GetField("TextBox1", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
if (field != null)
{
rValue = ((TextBox)field.GetValue(webControl)).Text;
literal.Text += "Find Control by ID: " + rValue + "<br />";
}
Conclusion
If you find any bugs or disagree with the contents, please drop me a line and I'll work with you to correct it.
Download
Download source
Resources Get controls by name using reflection
Type.BaseType Property

Join the conversation! Your thoughts help the community grow.