locked
Dropdown list selecting wrong value RRS feed

  • Question

  • User-1215707945 posted

    I have a drop down list which is populated by a query.
    Item text = resource and item value = position.
    My problem seems to occur when more than one resource have the same value (position).
    The users select a resource and click the submit button. The selectedItems text and value are passed as 2 parameters to a stored procedure.
    Sample data:
    JeffW - Support
    JohnB - Manager
    FredD - Sales
    JaneG - Accounts
    SarahP - Manager

    Now, when I select SarahP and submit I hit my breakpoint where the parameters are assigned their value.
    The values are JohnB and Manager NOT SarahP and Manager.
    I have tried 100 times and each time the same thing. Selecting a resource that has a unique value does not cause this issue (to date)
    Even stranger is that when the page posts back after having done it's code behind JohnB is now the currently selected item in the list!
    I have tried adding

    if (!IsPostBack)

    {

    this.ddl_positions.DataBind();

    }

    To the page load event but no difference.
    I have tried ddl_positions.selectedText to ddl_positions.SelectedItem.Text but no change.

    theCommand.Parameters.Add("@resource", System.Data.SqlDbType.VarChar);
    theCommand.Parameters.Add("@position", System.Data.SqlDbType.VarChar);
    theCommand.Parameters["@resource"].Value = ddl_positions.SelectedItem.Text;
    theCommand.Parameters["@position"].Value = ddl_positions.SelectedValue;
    theCommand.ExecuteNonQuery();

    Tuesday, January 14, 2020 12:00 AM

All replies

  • User-1716253493 posted

    The posible reason is ddl items populated with wrong value and text pair 

    Tuesday, January 14, 2020 12:34 AM
  • User-1215707945 posted

    No, I have viewed the page source and the text/value pairs are exactly as they should be

    Tuesday, January 14, 2020 12:57 AM
  • User283571144 posted

    Hi QPR_JAY ,

    Directly fetching the selected item from drop down list won't be working because it will be always the first one with the matched value. When the page is post back, the select index will be changed by iterating the list and matching the value. That way, it won't be set to the second or later item with the same value.

    According to your scenario, I suggest you add javascript and hidden field control to pass the selected index to the server so that you can get the selected text and value in code-behind.

    Below is a demo to demonstrate that works for your sample data.

    ASPX page: 

      <script type="text/javascript">
            $(document).ready(function () {
                restoreDDLIndex();
                storeDDLIndex();
    
            });
    
            function restoreDDLIndex() {
                var hf = $("#<%=hf_ddl.ClientID%>").get(0);
                var ddl = $("#<%=ddl_positions.ClientID%>").get(0);
                ddl.selectedIndex = hf.value;
            }
    
            function storeDDLIndex() {
                var hf = $("#<%=hf_ddl.ClientID%>").get(0);
                var ddl = $("#<%=ddl_positions.ClientID%>").get(0);
                hf.value = ddl.selectedIndex;
            }
    
        </script>
        <form id="form1" runat="server">
            <div>
                <asp:DropDownList ID="ddl_positions" runat="server" ></asp:DropDownList>
            </div>
            <asp:Button ID="SubmitBtn" Text="Get DDL Selected Text and value" runat="server" OnClientClick="storeDDLIndex()" onclick="SubmitBtn_Click"/>
            <asp:HiddenField ID="hf_ddl" runat="server" />
            <div>
                DDL_SelectText: 
            <asp:Label ID="DDL_SelectText" runat="server"></asp:Label>
            </div>
            <div>
                DDL_SelectValue: 
            <asp:Label ID="DDL_SelectValue" runat="server"></asp:Label>
            </div>
    
        </form> 
    
    

    Code-behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    BindDDL();
                }
            }
    
            private void BindDDL()
            {
                // Clear the previous selected mark
                ddl_positions.Items.Clear();
    
                // Rebind the sample data
                ListItem initialItem = new ListItem("Choose One Item")
                {
                    Selected = true
                };
    
                ListItem[] items = { new ListItem("JeffW ", "Support"),
                                     new ListItem("JohnB", "Manager"),
                                     new ListItem("FredD", "Sales"),
                                     new ListItem("JaneG", "Accounts"),
                                     new ListItem("SarahP", "Manager")};
    
                foreach(ListItem item in items)
                {
                    ddl_positions.Items.Add(item);
                }
    
            }
    
    
    
            protected void SubmitBtn_Click(object sender, EventArgs e)
            {
    
                int index = Convert.ToInt32(hf_ddl.Value);
    
                DDL_SelectText.Text = ddl_positions.Items[index].Text;
                DDL_SelectValue.Text = ddl_positions.Items[index].Value;
            }
    

    Result:

     

    Best Regards,

    Brando

    Tuesday, January 14, 2020 8:43 AM
  • User-1215707945 posted

    Thanks (and for taking the time to put this together!), however this seems like a lot of work-a-rounds and I have a simpler way.
    I am just going to use "resource" as the text and the value for the drop down list items. Then I can look up the "position" in my stored procedure.

    Strange that the DDL can not just pass the exact text/value of the currently selected item without looking at other items and getting itself confused. The text and values of all the non-selected items should be irrelevant?

    Tuesday, January 14, 2020 8:58 PM
  • User283571144 posted

    Hi QPR_JAY,

    It seems like that you want to confirm if the DDL will check the non-selected items? Do I understand right?

    DDL does pass all the text and value including selected and no-selected. When server getting the post back text and value, it then checks the selected value to change the index. The others will not join this process.

    Best regards,

    Brando
     

    Wednesday, January 15, 2020 3:18 AM