This is a mirror of official site: http://jasper-net.blogspot.com/

ASP.NET Load WebUserControl programmatically and invoke method using Reflection

| Sunday, October 17, 2010
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.

public string Text
{

get { return TextBox1.Text; }
set { TextBox1.Text = value; }
}

public static string staticVar = "Static Value";
public const string constVar = "Constant Value";

public void Method1()
{

Label1.Text = "Invoked public method with no argument";
}

public void MethodWithOneArg(string arg1)
{

Label2.Text = "Invoked public method with one argument, value: " + arg1;
}

public void MethodWithTwoArg(string arg1, int arg2)
{

Label3.Text = string.Format("Invoked public method with two argument, value: {0} and {1}", arg1, arg2.ToString());
}

private void PrivateMethod()
{

Label4.Text = "Invoked private method";
}

private void PrivateMethodWithArgs(string arg1, int arg2, string arg3)
{

Label5.Text = string.Format("Invoked private method with three argument, value: {0} , {1} and {2}", arg1, arg2.ToString(), arg3);
}

Read more: C# Corner

Posted via email from .NET Info

0 comments: