Introduction
The picture shows the executed page after click on -Default Page- link, or on initial load.
This article wants to point on dynamic aspect of requesting the functions during the execution of your web site.
Think on the folowing situation: you have to invoke some methods from a class, in this example we call it: DynChooseFunc, but we want this to do from the url and not from code like: NameFunc()
, but NameFunc - represents a parameter in the url string, for example: http://www.yoursite.net/default.aspx?action=NameFunc. Now that you got the idea what this code does, let's examine how can we accomplish this.
Repesentation
The picture shows the executed page after click on -Specific Page- link.
Here I must mention that the Default.aspx contains a TreeView (From Navigation Panel) ans a Label (From General Panel). The label has the goal of showing us what is currently loaded in our browser.
Using the code
Here I present you the class DynChooseFunc
with two functions: PageNameDefault()
and PageNameSpecific()
. They both return string value (for simplicity, of course). We consider the source code of the file DynChooseFunc.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace AppDiadiora
{
public class DynChooseFunc
{
public DynChooseFunc()
{
}
public string PageNameDefault()
{
return "Default page";
}
public string PageNameSpecific()
{
return "Index page";
}
}
}
And next is the code behind for default.aspx
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AppDiadiora;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DynChooseFunc dcf = new DynChooseFunc();
if (Request.Params["action"] != null)
{
string strMethodName = Request.Params["action"];
lbTitle.Text = dcf.GetType().GetMethod(strMethodName).Invoke(dcf, null).ToString();
}
else
{
lbTitle.Text = dcf.GetType().GetMethod("PageNameDefault").Invoke(dcf, null).ToString();
}
}
}
Comments
Creation of an object of class DynChooseFunc()
in order to gain access to Did you learn anything interesting/fun/annoying while writing the code? Did you
do anything particularly clever or wild or zany?
DynChooseFunc dcf = new DynChooseFunc();
Obtains the action
value from the URL:
Request.Params["action"]
Invokes the method strMethodName
from DynChooseFunc()
class. Here I must mention that strMethodName
is dynammicaly interclaced in code:
dcf.GetType().GetMethod(strMethodName).Invoke(dcf, null)
Assign the returned function's result to the label named lbTitle
in order to show the most simply possible the results. Of course you may make it more sophysticated to work for example with xml files, datasets and so on.
lbTitle.Text = dcf.GetType().GetMethod(strMethodName).Invoke(dcf, null).ToString();
Conclusions
If you look more closely and understand how this works, than in my opinion it's another implementation of the proxy pattern.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.