problems resetting dynamic dropdownlist

Last post 07-02-2007 12:04 PM by Yani Dzhurov. 4 replies.

Sort Posts:

  • problems resetting dynamic dropdownlist

    06-29-2007, 10:48 AM
    • Loading...
    • epalla
    • Joined on 06-14-2007, 7:29 PM
    • Posts 9

    I have a series of dropdownlists that I need to be able to reset with a button.  I'm using tableadapters to populate the lists, with the following code to insert headers at the beginning of each list (the values that I need things to be reset to): (I call this onprerender of all the ddl's, as you can probably gather from the name)

    1        // This is a preloading method that sets the headers on the dropdownlists
    2        public void DropDownLists_PreRender(object sender, EventArgs e)
    3        {
    4            // local variables, set header to blank so ASP.NET doesn't yell at us.
    5            DropDownList droplist;
    6            droplist = (DropDownList)sender;
    7            string header = "";
    8    
    9            // switch on the ID of the dropdownlist that sent the postback
    10           switch (droplist.ID.ToString())
    11           {
    12               case "DropDownList1": header = "Platform"; break;
    13               case "DropDownList2": header = "Environment"; break;
    14               case "DropDownList3": header = "Major Ver"; break;
    15               case "DropDownList4": header = "Minor Ver"; break;
    16               case "DropDownList5": header = "Physical Server"; break;
    17               case "DropDownList6": header = "Instance"; break;
    18               case "DropDownList7": header = "DBAS N"; break;
    19               case "DropDownList8": header = "OS Version"; break;
    20           }
    21   
    22           // if the list is already populated and we haven't already inserted
    23           // the header, do it now.
    24           if (droplist.Items.Count > 0 && droplist.Items[0].Text != header)
    25           {
    26               droplist.Items.Insert(0, new ListItem(header, ""));
    27           }
    28   
    29           // if we've finished all the inserting, update the page count
    30           // this can NOT be placed inside the switch, or the OS Version
    31           // won't list properly on load.
    32           if (droplist.ID.ToString() == "DropDownList8")
    33           {
    34               UpdatePageCount();
    35           }
    36   
    37       }
    

     This code works fine for setting headers of the lists.  The problem isn't in the switch, I've done it with 8 if loops too, this is just where I'm at with it right now.  What I need is to reset the lists to that first value with a button, so I'm using this simple code:

    1        // This is the clear filters button.  Reset all search parameters
    2        protected void Button1_Click(object sender, EventArgs e)
    3        {
    4            DropDownList1.ClearSelection();
    5            DropDownList2.ClearSelection();
    6            DropDownList3.ClearSelection();
    7            DropDownList4.ClearSelection();
    8            DropDownList5.ClearSelection();
    9            DropDownList6.ClearSelection();
    10           DropDownList7.ClearSelection();
    11           DropDownList8.ClearSelection();        
    12           
    13           DropDownList1.SelectedIndex = 0;
    14           DropDownList2.SelectedIndex = 0;
    15           DropDownList3.SelectedIndex = 0;
    16           DropDownList4.SelectedIndex = 0;
    17           DropDownList5.SelectedIndex = 0;
    18           DropDownList6.SelectedIndex = 0;
    19           DropDownList7.SelectedIndex = 0;
    20           DropDownList8.SelectedIndex = 0;
    21           /* set by finding the text value.  This sets it to the 1st one too!
    22           DropDownList8.SelectedIndex = DropDownList8.Items.IndexOf(DropDownList8.Items.FindByText("OS Version"));
    23           */
    24       }
    

    As you can see I've tried setting them the commented way as well, which resets most of them but doesn't solve the problem.  So here's the odd behavior I'm getting:

    Say I set the "Physical Server" dropdownlist and then need to clear the filters.  When I push the clear filters button, all the lists will be reset to their starting point except for the Instance dropdownlist, which will be reset to the first value in the list (not the header).  If I set multiple dropdownlists, it will clear all of them, but the next dropdown after the last one that was set will always be at the first value in the list instead of at the header that I'm putting in. (The header is still there, it's just set to the next value instead of to that one.)

    Now I'm thinking this is a problem with the code above and not with the declarations of the data sources or with the lists because all the data in the lists is right, it's just not getting reset to the correct value.  I think maybe there are two '0' values in the list - one set dynamically from the data source and one set by me when I add the header.  That doesn't really explain the behavior that I'm getting though.

    To Clarify, suppose here are my dropdownlists:

    Platform

    1. Platform 
    2. SQLServer
    3. UDB
    4. Oracle

    Environment

    1. Environment 
    2. Production
    3. Development
    4. Stage
    5. Lab

    If I set the Platform List to any value, when I click the reset button Platform will be reset to Platform, but Environment will invariably be set to "Production" instead of the header item of "Environment".  This is the case for all the lists in the set.

  • Re: problems resetting dynamic dropdownlist

    07-02-2007, 11:09 AM
    Answer
    • Loading...
    • Yani Dzhurov
    • Joined on 11-23-2006, 9:02 AM
    • Sofia, Bulgaria
    • Posts 516

    Hi,

    you are adding the headers in a very intersting way :) 

    Do you know that there is a property that could help you to add the headers in design time:

    <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
            <asp:ListItem Value="Header" Text="Header" />
    </asp:DropDownList>

     AppendDataBoundItems allows you to combined ListItems from the aspx code with the ones returned by tableadapters.

    Regarding the clear selection, try this function instead:

    1        // This is the clear filters button.  Reset all search parameters
    2        protected void Button1_Click(object sender, EventArgs e)
    3        {
    12          
    13           DropDownList1.SelectedIndex = -1;
    14           DropDownList2.SelectedIndex = -1;
    15           DropDownList3.SelectedIndex = -1;
    16           DropDownList4.SelectedIndex = -1;
    17           DropDownList5.SelectedIndex = -1;
    18           DropDownList6.SelectedIndex = -1;
    19           DropDownList7.SelectedIndex = -1;
    20           DropDownList8.SelectedIndex = -1;

           }

    Hope this will help you.

    Cheers,

    Yani 

  • Re: problems resetting dynamic dropdownlist

    07-02-2007, 11:25 AM
    • Loading...
    • mythu
    • Joined on 05-11-2007, 12:39 PM
    • Posts 54

    Hi,

    My Answer might be silly but it might work.

    If you really had confusion in choosing index as -1 or 0, you can raise the Pre_render event dynamically in the code behind on a button click. so that will reset your Drop down list boxes to default selection

    ---------------------------------------------
    Please Mark Post that helped you as answer
  • Re: problems resetting dynamic dropdownlist

    07-02-2007, 11:35 AM
    • Loading...
    • epalla
    • Joined on 06-14-2007, 7:29 PM
    • Posts 9

    Setting it to -1 seems to have worked..  I have no idea why, but it did so I'm okay with it.

  • Re: problems resetting dynamic dropdownlist

    07-02-2007, 12:04 PM
    • Loading...
    • Yani Dzhurov
    • Joined on 11-23-2006, 9:02 AM
    • Sofia, Bulgaria
    • Posts 516

    Ok, here it is the explanation why it does not work with '0'.

    Here is the code for SelectedIndex property of DropDownList:

     

    [WebCategory("Behavior"), DefaultValue(0), DesignerSerializationVisibility(0), Themeable(false), Bindable(true), WebSysDescription("WebControl_SelectedIndex"), Browsable(false)]
    public virtual int SelectedIndex
    {
    get { for (int i = 0; i < this.Items.Count; i++)
    {
    if (this.Items[i].Selected)
    {
    return i;
    }
    }
    return -1;
    }
    set { if (value < -1)
    {
    if (this.Items.Count != 0)
    {
    throw new ArgumentOutOfRangeException("value", SR.GetString("ListControl_SelectionOutOfRange", new object[] { this.ID, "SelectedIndex" }));
    }
    value = -1;
    }
    if (((this.Items.Count != 0) && (value < this.Items.Count)) || (value == -1))
    {
    this.ClearSelection();
    if (value >= 0)
    {
    this.Items[value].Selected = true;
    }
    }
    else if (this._stateLoaded)
    {
    throw new ArgumentOutOfRangeException("value", SR.GetString("ListControl_SelectionOutOfRange", new object[] { this.ID, "SelectedIndex" }));
    }
    this.cachedSelectedIndex = value;
    }
    }


    At the moment the Click event of your Button1 is fired, your drop downs still don't have the 'header' items, since they are added at later stage (Pre_Init event).
    So what you are doing is instructing  the drop down list to mark the item at position '0' as marked:
      this.Items[value].Selected = true;

     Note that the when you add a new item at position '0' in the collection, the selection will not change, since it's done on Item basis.

    When you add a new item at position '0', the old one is shifted to position '1' but it's still marked as Selected.

    So at rendering the DropDownList looks for the one which Selected property is set to true, not the one that is at position SelectedIndex.

    When setting SelectedIndex = -1; no item is marked as selected. And since you add your 'headers' at position '0' they will be first one that will be shown by your drop downs.
     

    Hope this will help.

    Cheers,

    Yani
     

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter