A Richer DynamicDataFilter – Part 5 ColumnContains
Posted by jheyse on 23rd May 2008
I’m not really sure what to call this post, so it’s just the name of the control I wrote. A reader left me the following message on a previous post during this series on the extended DynamicDataFilter:
Hi
I was wondering if you could guide me on how to do the following:
On every List page, I need a dropdownlist populated with all the column names from the Table. Adjacent to that should be a textbox where the user can enter some text. And then a ‘Search’ button.
When clicked on this button, the following should happen:
Perform search on the Table where dropdownlist.selectedcolumnname CONTAINS textbox.text.
Repopulate Gridview with the search results.
I’d appreciate any help or atleast an idea on how to go about this. I’m new to Dynamic Data
Abby, unfortunately the functionality you are looking for isn’t currently supported in DynamicData, the main issue is that the LinqDataSource does not understand CONTAINS the where parameters you supply are all equals. But, the DynamicData Filtering I have written does support this. (My code isn’t production quality, and should only be used in production at your own risk.) Here is a quick screen shot:
To allow for this I created a ColumnContains control which has a DrowDownList of the columns available in the DataSource’s Table and a TextBox which accepts your CONTAINS value.
<table>
<tr>
<td>
<asp:DropDownList ID="ddlColumn" runat="server">
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="tbText" runat="server"></asp:TextBox>
</td>
</tr>
</table>
The code behind for the control is responsible for binding the list of string based columns to the DropDownList and creates the LikeExpressionParameter which is responsible for the LIKE query.
public partial class ColumnContains : Catalyst.Web.DynamicData.FilterTemplateUserControlBase
{
protected void Page_Init(object sender, EventArgs e)
{
ddlColumn.DataTextField = "Name";
ddlColumn.DataSource = Table.Columns.Where(c => c.TypeCode == TypeCode.String);
ddlColumn.DataBind();
}
private MetaTable Table
{
get
{
IDynamicDataSource source = this.FindDataSourceControl();
if(source != null)
return source.GetTable();
return null;
}
}
public override IEnumerable<Parameter> GetWhereParameters(IDynamicDataSource dataSource)
{
yield return new LikeExpressionParameter()
{
Name = ddlColumn.SelectedValue,
Like = LikeExpressionParameter.LikeType.Contains,
Value = tbText.Text
};
}
public MetaColumn Column
{
get
{
return Table.GetColumn(ddlColumn.SelectedValue);
}
set
{
ddlColumn.SelectedIndex = -1;
if (value != null)
{
ListItem li = ddlColumn.Items.FindByValue(value.Name);
if (li != null)
li.Selected = true;
}
}
}
public string Value
{
get
{
return tbText.Text;
}
set
{
tbText.Text = value;
}
}
public override void LoadQueryStringParameters(NameValueCollection parameters)
{
string columnName = parameters[string.Format("{0}_column", this.ID)];
Column = Table.GetColumn(columnName);
Value = parameters[string.Format("{0}_value", this.ID)];
}
public override NameValueCollection SaveQueryStringParameters()
{
NameValueCollection collection = new NameValueCollection();
collection.Add(string.Format("{0}_column", this.ID), Column.Name);
collection.Add(string.Format("{0}_value", this.ID), Value);
return collection;
}
public override void Clear()
{
Column = null;
Value = string.Empty;
}
}
Posted in ASP.NET, C#, Dynamic Data, LINQ | 1 Comment »








