Custom Parameter for a DataSource
While using a cookie parameter for an ObjectDataSource I discovered that you get the whole cookie, not the value from a key....So instead of my method getting passed the value, it was getting passed key=value. Needless to say I was a little miffed. But I quickly discovered how easy it is to create my own custom data parameter.
[ToolboxData("<{0}:CookieParameter ></{0}:CookieParameter>")]
[System.Drawing.ToolboxBitmap(typeof(Parameter))]
public class CookieParameter : Parameter
{
private string _cookieKey;
public string Key
{
get { return _cookieKey; }
set { _cookieKey = value; }
}
private string _cookie;
public string Cookie
{
get { return _cookie; }
set { _cookie = value; }
}
public CookieParameter() : base()
{
}
public CookieParameter(string name, object value) : base(name)
{
}
public CookieParameter(string name, TypeCode type, object value) : base(name, type)
{
}
protected CookieParameter(CookieParameter original)
: base(original)
{
}
protected override object Evaluate(HttpContext context, Control control)
{
if ((context != null) && (context.Session != null))
{
return context .Request.Cookies[Cookie].Values.Get(Key);
}
return null;
}
protected override Parameter Clone()
{
return new CookieParameter(this);
}
}
Then in the data source control just do this
<asp:ObjectDataSource ID="MyDataSource" runat="server">
<SelectParameters>
<Data:CookieParameter Cookie="cookie" Name="myParameter" Type="Object" Key="cookieKey" />
</SelectParameters>
</asp:ObjectDataSource>