CodeSmith Community
Your Code. Your Way. Faster!

DropDownList and DataSource Filter

Latest post 03-27-2007 1:26 PM by bgjohnso. 12 replies.
  • 03-19-2007 1:16 AM

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    DropDownList and DataSource Filter

    Hi!

    Isn't the following code supposed to work? What I would expect is to get the Clients filtered according to the Season (Saison) DropDownList, but it does not work. Some filtering is happening but it seems to be random. I can select the same DropDownList item many times and get different results. What am I missing? Maybe my understanding of Filters is totally wrong. I also tried with Find instead of GetPaged and with:

    <data:SqlParameter Name="Parameters">

    instead of:

    <data:SqlParameter Name="WhereClause" UseParameterizedFilters="false">

     but none of that worked.

     Here is the ASPX code:
     

     

            <p>

                Saison:

                <asp:DropDownList

                    ID="ddlSaison"

                    DataSourceID="SaisonDataSource"

                    DataValueField="SaisonID"

                    DataTextField="SaisonNomFR"

                    AutoPostBack="true"

                    AppendDataBoundItems="true"

                    OnSelectedIndexChanged="SaisonChanged"

                    runat="server">

                    <asp:ListItem Value="" Text="" Selected="True" />

                </asp:DropDownList>

            </p>

            <data:ClientRepeater

                ID="clientRepeater1"

                runat="server"

                DataSourceID="clientDataSource"

                >

               

                <HeaderTemplate>

                    <h1>List of clients</h1>

                </HeaderTemplate>

               

                <ItemTemplate>

                    <b><%# Container.ClientNom %></b><br />

                    <%# Container.ClientDescriptionFR %>

                    <hr />

                </ItemTemplate>

            </data:ClientRepeater>

           

            <data:ClientDataSource

                ID="clientDataSource"

                runat="server"

                SelectMethod="GetPaged"

                EnablePaging="False"

                EnableSorting="False"

                Sort="ClientNom ASC"

                >

                <Parameters>

                    <data:SqlParameter Name="WhereClause" UseParameterizedFilters="false">

                        <Filters>

                            <data:ClientFilter Column="SaisonID" ControlID="ddlSaison" PropertyName="SelectedValue" ComparisionType="Equals" />

                        </Filters>

                    </data:SqlParameter>

                </Parameters>

            </data:ClientDataSource>

           

            <data:SaisonDataSource ID="SaisonDataSource" SelectMethod="getAll" runat="server" />


    And the code-behind:

     

            protected void Page_Load(object sender, EventArgs e)

            {

                if (!IsPostBack)

                {

                    ddlSaison.DataBind();

                    clientRepeater1.DataBind();

                }

            }

     

            protected void SaisonChanged(object sender, EventArgs e)

            {

                clientRepeater1.DataBind();

            }

     

    Cheers!

    JF
     

     

    Filed under:
    • Post Points: 90
  • 03-19-2007 10:35 AM In reply to

    Re: DropDownList and DataSource Filter

    Hi JF

    Im new in this too, but my guessing is that in your code-behind you don't have to to do the bind.

    In the Designer Mode of the Page, selecet the ddl and set in the property or the smart tag the datasource and that all.
     

    elfederiko
    • Post Points: 35
  • 03-19-2007 11:03 AM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 767
    • Points 22,605

    Re: DropDownList and DataSource Filter

    Elfrederico is correct.  You should not have to use any code-behind for what you are trying to do.

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 35
  • 03-19-2007 11:20 AM In reply to

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 925
    • Points 34,785

    Re: DropDownList and DataSource Filter

    I had a little poke around under the hood with this and I think there may be a problem with the repeater.

    I created a clean page (Northwind) which had a similar setup to the above (from Suppliers/Products) and I couldn't get it to work either.  It seems to use the previous selectedvalue (rather than the latest) from the drop down when it does the filter.

    To check it wasn't the datasource I dropped a entitygridview on the same page and that worked fine.

    I haven't used the repeater before so I may be missing something as well but I thinks its worth further investigation.

    hth

    swin

     

    my sample... 

    <%@ Page Language="C#" Trace="true" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>Suppliers:
        <asp:DropDownList runat="server" ID="eddl"
            AutoPostBack="true"
            DataSourceID="suppliersDS" DataTextField="CompanyName" DataValueField="SupplierID"
            OnSelectedIndexChanged="eddl_SelectedIndexChanged" />&nbsp;<br />
        <data:ProductsRepeater runat="server" ID="pr" DataSourceID="productsDS" >
            <HeaderTemplate>
                <h1>List Of Products</h1>
            </HeaderTemplate>
            <ItemTemplate>
                <b><%# Container.ProductID %></b>&nbsp;
                <%# Container.ProductName %>&nbsp;
                <%# Container.SupplierID %>
                <hr />
            </ItemTemplate>
        </data:ProductsRepeater>
       
        <data:ProductsDataSource runat="server" ID="productsDS" SelectMethod="GetPaged" Sort="ProductName" EnablePaging="true" EnableSorting="true" >
            <Parameters>
                <data:SqlParameter Name="WhereClause" UseParameterizedFilters="false" >
                    <Filters>
                        <data:ProductsFilter Column="SupplierID" ControlID="eddl" PropertyName="SelectedValue" ComparisionType="Equals" />
                    </Filters>
                </data:SqlParameter>
            </Parameters>
        </data:ProductsDataSource>
       
        <data:SuppliersDataSource runat="server" ID="suppliersDS" SelectMethod="GetAll" Sort="CompanyName" />
       
        </div>
        </form>
    </body>
    </html>

    code behind... 

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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;

    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {
        }
        protected void eddl_SelectedIndexChanged( object sender, EventArgs e )
        {
            pr.DataBind();
        }
    }
     

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 35
  • 03-19-2007 12:31 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Re: DropDownList and DataSource Filter

    Hi!

    In fact, I first tried to do this without any code-behind, but since it was not working, I thought maybe the ClientDataSource is getting bound before the DropDownList and that's why it is not working. So I added the code-behind, but it did not work either.

    By the way Swin, I was also under the impression that the SelectedValue used by the DataSource was the previous one. Thanks for looking into this!

    It would be nice to have some documentation about filters. I'm never quite sure on how they should be used. I've seen examples using <data:SqlParameter Name="WhereClause" UseParameterizedFilters="false"> and others using <data:SqlParameter Name="Parameters">. Also, I don't know when to use UseParameterizedFilters="true".

    Cheers!

    JF
     

    • Post Points: 5
  • 03-19-2007 6:54 PM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 767
    • Points 22,605

    RE: DropDownList and DataSource Filter

    JF,

     

    Ok, the fact that the repeater was always one request behind really bugged me so I spent some time to see if I could track down the issue.  It all came down to the fact that the data request was initiated in the CreateChildControls method, which was called prior to the page load completing.  Unfortunately, the datasource controls don’t update their filters until the page has loaded, which is why everything was behind.  I changed the code for the repeater controls so that they inherit from CompositeDataBoundControl rather than Control, which actually makes the code a bit simpler and doesn’t require explicit calls to the data source controls.  I haven’t had a chance to thoroughly test this, but I have attached a patch for you to try.  Apply it to the WebLibrary\UI directory.  Let me know how it works and if you have any issues.

     

    The type of SqlParameter that you use will depend on the select method that you are using:  GetPaged = WhereClause; Find = Parameters.  I honestly don’t know when you use the UserParameterizedFilters attribute…

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 35
  • 03-19-2007 10:10 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Re: RE: DropDownList and DataSource Filter

    Hi Ben!

    Thanks so much! I would like to try your patch, but I don't see any attachment. Did you forget to attach the patch, or is it me who's blind?

    Cheers!

    JF


     

    • Post Points: 35
  • 03-20-2007 2:44 AM In reply to

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 925
    • Points 34,785

    Re: RE: DropDownList and DataSource Filter

    Nice one Ben, but JF is right, the patch doesn't seem to be attached.

    swin 

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 35
  • 03-20-2007 10:39 AM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 767
    • Points 22,605

    Re: RE: DropDownList and DataSource Filter

    Ok, it's there now.  I responded through email and attached the patch with the email.  Apparently, it doesn't process attachments...  Oh, well.

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 35
  • 03-25-2007 1:15 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Re: RE: DropDownList and DataSource Filter

    Hi Ben!

    Thanks for the patch! Using my example, I tested it, and I found three things that are not working properly. By the way, I removed the code-behind.

    1) "using System.Web.UI.WebControls;" is mising in the View Repeater template. I had to add this line by hand in my .cs files in order for my application to compile properly.

    2) If I use the following DropDownList everything works fine after I do a selection, but not the first time the page loads, in which case the Repeater displays the full list of clients as if there was no WHERE clause. This DropDownList contains three choices (1- 4 Seasons, 2- Summer, 3- Winter). None of them is selected, so the first item is displayed by default when the page loads:

                <asp:DropDownList

                    ID="ddlSaison"

                    DataSourceID="SaisonDataSource"

                    DataValueField="SaisonID"

                    DataTextField="SaisonNomFR"

                    AutoPostBack="true"

                    runat="server">

                </asp:DropDownList>

     

    3) If I use the following DropDownList with an empty default value, the Repeater always displays the full "Client' list, no matter what I select:

     
                <asp:DropDownList

                    ID="ddlSaison"

                    DataSourceID="SaisonDataSource"

                    DataValueField="SaisonID"

                    DataTextField="SaisonNomFR"

                    AutoPostBack="true"

                    AppendDataBoundItems="true"

                    runat="server">

                    <asp:ListItem Value="" Text="" Selected="True" />

                </asp:DropDownList>

    If you need more information, please don't hesitate to let me know. 

     Cheers!

     JF
     

    Filed under: ,
    • Post Points: 5
  • 03-26-2007 6:57 PM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 767
    • Points 22,605

    RE: RE: DropDownList and DataSource Filter

    Hey Velum,

     

    1.  I caught this also and have updated my local copy (as this has not been checked in).

     

    2.  I’m assuming that you would like it to filter based on the whatever the first element is, correct?  To be honest, this one has me stumped a bit right now.  It has to do with the way the filters are collected and timing of when everything happens, but I can’t quite figure it out yet.  I’ll keep working on it…

     

    3.  I can’t reproduce this.  From what I can tell, all you have added is a default item to the list.  When I do this, things still work as expected when I select different entries from the list.

     

    <asp:DropDownList ID="ddlEmployee" DataSourceID="EmployeesDS" DataValueField="EmployeeID"

                DataTextField="EmployeeID" AutoPostBack="true" AppendDataBoundItems="true"

                runat="server">

                <asp:ListItem Value="" Text="" Selected="true"/>

             </asp:DropDownList>

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 35
  • 03-26-2007 9:35 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Re: RE: RE: DropDownList and DataSource Filter

    Hi Ben!

    1. Ok

    2. That's correct, I would like the Repeater to filter based on the element displayed in the Drop Down List, in other words, the first element.

    3. That was a problem in my ASPX code. I did not set the DropDownList ID properly. My fault! Sorry about this.

    Cheers!

    JF

     

    • Post Points: 35
  • 03-27-2007 1:26 PM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 767
    • Points 22,605

    Re: RE: RE: DropDownList and DataSource Filter

    Velum,

     

    Ok, I think I have tracked down the issue with item #2.  The problem comes down to the way that the DropDownList works when bound to a datasource.  There is a method called GetValue in FormUtilBase.generated.cs that is responsible for extracting the SelectedValue of the dropdownlist.  It just rolls through the items in the list and looks for the Selected property to be true.  The problem is that when the dropdownlist is first bound on the page, none of the items have the Selected property set, even though the first item is displayed and “selected”.  I have modified the code a bit so that it now works as you would expect it to (and the way it works when using other datasource objects like SqlDataSource).

     

    Here is the modified snippet of code:

     

    else if ( control is ListControl )

    {

          CommaDelimitedStringCollection values = new CommaDelimitedStringCollection();

          ListControl list = control as ListControl;

     

          ListItem firstItem = null;

          foreach ( ListItem item in list.Items )

          {

                if ( item.Selected )

                {

                      values.Add(item.Value);

                }

                if (firstItem == null)

                {

                      firstItem = item;

                }

          }

          if (firstItem != null && list is DropDownList && values.Count == 0)

          {

              //DropDownList must have one item selected

              values.Add(firstItem.Value);

          }

          value = values.ToString();

    }

     

    Added lines are in green.

     

    I have also attached a patch that contains the modifications to the TableRepeater, ViewRepeater and the change above.

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 5
Page 1 of 1 (13 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems