Simulating actions in ASP.NET
I've been trying to figure out how I could work in both ASP.NET and Unit Testing environments. For instance, if the user clicks a Register button, that raises a click event. But in a unit testing environment, this doesn't work like this. So what could replace it? It may be possible through reflection, by using Reflection to invoke the method that would raise the event. Another alternative, one that could work in an API, is to have a PerformClick method in the user control class, which could work like this:
public abstract class ClickableUserControlBase : System.Web.UI.UserControl
{
public event EventHandler Click;
protected virtual void OnClick(EventArgs e)
{
if (Click != null)
Click(this, e);
}
protected override void OnLoad(EventArgs e)
{
//Attach to the button click event
}
public void PerformClick()
{
this.OnClick(EventArgs.Empty);
}
void Button_Click(object sender, EventArgs e)
{
this.PerformClick();
}
}
This way, it is possible to call the button click through code. Note that this approach is a more MVC-based approach, where this base class is defined in a class library, and not directly in a web project. Now, there are two ways to perform a click, one through an API call, and one through an actual button click.
This can work with selections as well; a custom page/user control class can have a means to Select or Deselect rows in a data source or a table. The selected item could be passed through an event argument, making it easy to work with selected items.