Posted on Feb 28, 2008

How To Dynamically Set a LinqDataSource to filter the results based on user input

Here’s an example of how to dynamically set the where property on a LinqDataSource to filter data based on user input from a textbox.  In this case we have a Linq entity called ContractsEntity that has a table called ContractHeaders.  Not shown in this example is the listview (in the Ajax UpdatePanel) that is bound to the LinqDataSource.  The listview will show all contracts by default, but I want the user to be able to enter text to filter the results.  The user enters data in the textbox and clicks on the Search button.  This causes the UpdatePanel to send an Ajax request which fires off the SearchContractNumButton_Click event.  In the event I set the LinqDataSource Where property to do the filtering.

The ASP.Net source looks like this:

<form id=”form1″ runat=”server”>

<div>

<asp:LinqDataSource ID=”ContractHeadersLinqDataSource” runat=”server”

ContextTypeName=”ContractsEntity” TableName=”ContractHeaders”>

</asp:LinqDataSource>

<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>

</asp:ScriptManager>

<asp:UpdatePanel ID=”UpdatePanel1″ runat=”server”>

<ContentTemplate>

<div>

<p>Enter a contract to search for:</p>

<p>Contract # <asp:TextBox ID=”SearchContractNumTextBox” runat=”server”></asp:TextBox>

<asp:Button ID=”SearchContractNumButton” runat=”server”

onclick=”SearchContractNumButton_Click” Text=”Search” />

</p>

</div>

</ContentTemplate>

</asp:UpdatePanel>

</div>

</form>

In the code behind the Button’s OnClick event looks like this:

protected void SearchContractNumButton_Click(object sender, EventArgs e)

{

ContractHeadersLinqDataSource.Where = “ContractNum.Contains(\”" + SearchContractNumTextBox.Text + “\”)”;

}

After further testing I have determined that this is not the best way of doing things, it stops working with paging.  Once I have figured out a better way of doing this I’ll post it.

A better way of doing this requires absolutely no code!  Configure your LinqDataSource in design view and click Next in the Choose a Context Object window.  Click on the Where button to configure the Where.  Make sure Automatically generate the Where Expression… checkbox is not checked.  In the Where Expression text area enter

ContractNum.Contains(@ContractNum)

Click on the Add Parameter button to add the Parameter.  In Parameter Source choose Control.  In ControlID choose SearchContractNumTextBox.  Click on the Show advanced properties link to show advanced properties.  Change ConvertEmptyStringToNull to False (Missing this step will cause an error on the page).  Change Name to ContractNum.  See the screenshot below.  Click OK and Finish.  Your filtering should be all set to go!

LinqDataSource Filter On User Input

View Comments

  • James Riall says:

    That worked very nicely! Thanks.

  • michael says:

    After further testing I have determined that this is not the best way of doing things, it stops working with paging. — Why does it stop paging in listview’s page control? have you ever figured it out ?

  • kyanachik says:

    Unfortunately I haven’t worked with the UpdatePanels for quite a while. We moved to using the Yahoo Datatable calling Page Methods. http://developer.yahoo.com/yui/datatable/ In order for the Yahoo Connection Manager (Ajax) to work with ASP.Net Page Methods you have to change it’s default behavior from Get to Post.

    YAHOO.util.Connect.resetDefaultHeaders();
    YAHOO.util.Connect.setDefaultPostHeader(false);
    YAHOO.util.Connect.initHeader(“content-type”, “application/json; charset=utf-8″, true);

    Subsequently we have moved to ASP.Net MVC which has greatly reduced the amount of client-side javascript but enhanced Ajax tremendously.

blog comments powered by Disqus