DropDown List - Default Item - Two RowFilters possible?

Discussion in 'ASP .Net Web Controls' started by Harry, Apr 15, 2004.

  1. Harry

    Harry Guest

    Hi,

    I have a problem with the default Item of an asp:dropdownlist.

    I databind the dropdown list in the code with values from a SQL
    Database as per below.

    ViewOptions.RowFilter = "Code LIKE 'MM%'";
    ddlRam.DataSource = ViewOptions;
    ddlRam.DataTextField = "Name";
    ddlRam.DataValueField = "IncPrice";
    ddlRam.DataBind();

    But what I would like now, is for the default option to be set to a
    different value based on the below filter:
    ViewOptions.RowFilter = "PrRange LIKE 'S' AND Code Like 'MM%'";


    Can anyone tell me if this is actually possible.
    (Ideally something like the below line would be perfect, if it
    worked!!)
    ddlRam.SelectedItem = ViewOptions.RowFilter = "PrRange LIKE 'S' AND
    Code Like 'MM%'";


    ANy help appriciated as always.

    Thanks
    H
    Harry, Apr 15, 2004
    #1
    1. Advertising

  2. You cannot do this like you have indicated, but after the bind you could
    look through your DataSet (after your selection filter) and use the
    ..Selected = true of the items in the ddlRam dropdown that match the filtered
    results.

    You could also package this up as a new function on a derived dropdownlist
    to you could use a filter clause for selected items...

    Fred

    "Harry" <> wrote in message
    news:...
    > Hi,
    >
    > I have a problem with the default Item of an asp:dropdownlist.
    >
    > I databind the dropdown list in the code with values from a SQL
    > Database as per below.
    >
    > ViewOptions.RowFilter = "Code LIKE 'MM%'";
    > ddlRam.DataSource = ViewOptions;
    > ddlRam.DataTextField = "Name";
    > ddlRam.DataValueField = "IncPrice";
    > ddlRam.DataBind();
    >
    > But what I would like now, is for the default option to be set to a
    > different value based on the below filter:
    > ViewOptions.RowFilter = "PrRange LIKE 'S' AND Code Like 'MM%'";
    >
    >
    > Can anyone tell me if this is actually possible.
    > (Ideally something like the below line would be perfect, if it
    > worked!!)
    > ddlRam.SelectedItem = ViewOptions.RowFilter = "PrRange LIKE 'S' AND
    > Code Like 'MM%'";
    >
    >
    > ANy help appriciated as always.
    >
    > Thanks
    > H
    Fred Hirschfeld, Apr 15, 2004
    #2
    1. Advertising

  3. Harry

    Harry Singh Guest

    Thans for the reply Fred.
    Is there any chance you could provide more details on your proposed
    solution.
    (Link to sample would be fine, and then i'll try to work ou for myself!)

    I don't really understand how I would put your suggestions into
    practice!

    Many thanks
    H




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Harry Singh, Apr 15, 2004
    #3
  4. I don't actually have some specific, but I will try some directly here (not
    compiled...). Hope this gets you closer to what you are looking for... This
    may not be optimal in terms of performance but should demonstrate what I was
    indicating:


    public class FilterSelectDropDownList : DropDownList {
    private string m_FilterCriteria = string.Empty;

    public FilterSelectDropDownList() : base() {
    }

    public string FilterCriteria {
    get {
    return m_FilterCriteria;
    }
    set {
    m_FilterCriteria = value;

    // if there is bound data, apply the select filter now.
    ApplyFilter();
    }
    }

    public override void DataBind() {
    base.DataBind();

    ApplyFilter();
    }

    protected virtual void ApplyFilter() {
    if (m_FilterCriteria != string.Empty && DataSource != null &&
    DataSource is DataView) {
    DataView dv = DataSource as DataView;

    string oldFilter = dv.RowFilter;
    dv.RowFilter = m_FilterCriteria;

    foreach(DataViewRow dvr in dv) {
    object valueVal = dvr.Row[this.DataValueField];

    foreach(ListItem item in this.Items) {
    if (item.Value.Equals(valueVal)) {
    item.Selected = true;
    }
    }
    }

    dv.RowFilter = oldFilter;
    }
    }
    }
    Fred Hirschfeld, Apr 16, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kris Rockwell

    DropDown List Default Selected Item

    Kris Rockwell, Apr 11, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    27,713
    Dan Brussee
    Apr 12, 2004
  2. Dica
    Replies:
    4
    Views:
    722
  3. Replies:
    0
    Views:
    373
  4. Mark Rae

    Bitwise comparison in RowFilters

    Mark Rae, Oct 7, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    719
    Mark Rae
    Oct 7, 2006
  5. M. Craig

    How to set background color of List item in dropdown list

    M. Craig, Nov 12, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    275
    M. Craig
    Nov 12, 2003
Loading...

Share This Page