Fredrik Normén's Blog - NSQUARED²
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Microsoft Most Valuable Professional
     .Net Framework - ASP.Net - Architecture - Development
NOTE: This list of posts will only list the 15 latest posts, to see the rest, select from the Archives located in the menu to the left. The RSS will only list the 10 lastest posts.
Create your own Parameter to the data-source control's parameters collection

Category:  ASP.Net 2.0

When you use the data-source controls such the SqlDataSource or ObjectDatSource etc, you probably also add parameters to the data-source control’s specified commands, such as the SessionParameter, ControlParameter, ProfileParameter etc. You can also create your own parameters. This post is about how you can create your own Parameter for the data-source controls.

 

When you create your custom parameter you need to create a class that inherits the System.Web.UI.WebControls.Parameter class:

 

public class MyParameter : System.Web.UI.WebControls.Parameter

 

The Parameter has two methods that you can override, the Evaluate and the Clone method. The Evaluate method is the method that will at runtime be executed to get the value of the parameter that should be added as a value to your specified data-source control’s commands. The Clone method is used to clone the parameter.

 

protected override object Evaluate(HttpContext context, Control control)

{

   return "My value";

}

 

 

protected override Parameter Clone()

{

    return new MyParameter(this);

}

 

The Evaluate method has two arguments, the current HttpContext of the request and the Control where the parameter is bound to. It’s the Evaluate method that you should implement the logic that should return the value of your own custom parameter.

 

The Clone method can be implemented to clone the parameter. When you implement the Clone method you can also implement a constructor that will take the current instance of the parameter as an argument:

 

protected MyParameter(MyParameter original) : base(original)

{

   this.MyProperty = original.MyProperty;

}

 

Make sure to call the base class constructor that takes a Parameter as an argument.

 

Note: The MyProperty in the constructor above is an example of a property that is added to the custom parameter, the property can be added as an attribute to the parameter when it’s added to the data-source control’s parameter collection:

 

<asp:ObjectDataSource ...>

<SelectParameters>

            <myParam:MyParameter MyProperty=”My value” .../>

</SelectParameters>

 

For example the SessionParameter has a property with the name SessionField that is used to specify which property of a control that should be used to return a value for the SessionParameter. So the implementation of the SessionParameter’s Evaluate method, use the value from the SessionField property to know which session to get the value of a session, for example:

 

protected override object Evaluate(HttpContext context, Control control)

{

      if ((context != null) && (context.Session != null))

      {

            return context.Session[this.SessionField];

      }

      return null;

}

 

Every property that you add to the parameter can be used as an attribute. So if you need to specify some values to the parameter that should be used in the Evaluate method to get the value of the parameter, you can implement them as properties.

 

Before you can use your custom parameter on the Web Form, you need to use the @Register directive, so the Web Form can find your parameter.

 

The following code is an example of a custom parameter, it will only return the value specified to the MyProperty:

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Web.UI.WebControls;

 

namespace ClassLibrary1

{

    public class MyParameter : Parameter

    {

        public MyParameter(string name, object value) : base(name)

        {

            this.MyProperty = value;

        }

 

 

        public MyParameter(string name, TypeCode type, object value) : base(name, type)

        {

            this.MyProperty = value;

        }

 

 

        protected MyParameter(MyParameter original) : base(original)

        {

            this.MyProperty = original.MyProperty;

        }

 

 

        protected override object Evaluate(HttpContext context, Control control)

        {

            return "My value";

        }

 

 

        protected override Parameter Clone()

        {

            return new MyParameter(this);

        }

 

 

        public string MyProperty

        {

            get

            {

                object obj = base.ViewState["MyProperty"];

 

                if (obj == null)

                    return string.Empty;

 

                return (string)obj;

            }

            set

            {

                if (this.MyProperty != value)

                {

                    base.ViewState["MyProperty"] = value;

                    base.OnParameterChanged();

                }

            }

        }

     }

}

 

Posted: Sunday, January 22, 2006 - 11:22 GMT+1    Print     E-mail    Comments (5)
   fredrik.nsquared2.com - 2007