Contract between ASPX and ASCX
In the last versions of ASP.NET where not exist the compilation equals ASP.NET 2.0, easily we can invoke an ASPX method inside an User Control (ASCX), casting Page property to a Page type that would be a container but, this can cause a problem when an ASPX not provides a respective method.
With a new compilation in ASP.NET 2.0, it's dificult casting to a Page type because the name is resolved in compilation-time, so, in design-time we don't know this type. If you aren't using the Web Application Project (WAP) and want invoke a Page's method, you will need to create a contract through Interface. This Interface should be only implemented in ASPX where you want the User Control invoke a method. Below is an example:
public interface IConnection
{
void ExecuteProcedure();
}
public partial class _Default : System.Web.UI.Page, IConnection
public partial class Default2 : System.Web.UI.Page
[ ASCX ]
IConnection connection = this.Page as IConnection;
if (connection != null)
connection.ExecuteProcedure();
else
Response.Write("The Page container isn't a IConnection type!");
How you can note, the Default.aspx Page implements a Interface IConnection and Default2.aspx doesn't. Finally, inside an User Control, casting the Page Property to IConnection through as operator that, if isn't a compatible type, it will return null. If you have been using Visual Basic 2005, use the TryCast operator instead.