Click here to Skip to main content
15,886,258 members
Articles / Web Development / ASP.NET
Article

Dynamically appeal of methods from a class

Rate me:
Please Sign up or sign in to vote.
1.67/5 (3 votes)
25 Apr 20062 min read 14.7K   11  
An article showing how to call dynamical (from url) the methods from a class

Introduction

Sample Image of Default Page

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

Sample Image of Specific Page

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

C#
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
{
    /// <summary>
    /// Class with methods and propreties 
    /// that are loaded selective from default.aspx
    /// </summary>
    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

C#
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"];
            //execute the DynChooseFunc.<<strMethodName>> method
            lbTitle.Text = dcf.GetType().GetMethod(strMethodName).Invoke(dcf, null).ToString();
        }
        else
        {
            //execute the DynChooseFunc.PageNameDefault method
            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?

C#
DynChooseFunc dcf = new DynChooseFunc();

Obtains the action value from the URL:

C#
Request.Params["action"]

Invokes the method strMethodName from DynChooseFunc() class. Here I must mention that strMethodName is dynammicaly interclaced in code:

C#
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.

C#
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Moldova (Republic of) Moldova (Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --