locked
DropdownList SelectedIndex Always 1 upon postback RRS feed

  • Question

  • User-698295532 posted

    Hi,

    I have this simple code consists of a dropdownlist and a gridview control.  dropdownlist data is populated from SqlDS_Applicant datasource and the data is appended to the first entry with "Select An Applicant" text.  No matter which data entry (besides Select an Applicant) is selected, upon postback, the selectedindex is always 1 and the gridview data populated is thus belong to the first data entry.

    Please advise.

        Public Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
                     
            If Not IsPostBack Then
                DropDownList_Applicant.DataBind()
                DropDownList_Applicant.SelectedIndex = 0
            Else
                
                If Request("__EVENTTARGET").Contains("DropDownList_Applicant") Then
                    
                    '   The following Response.Write always:
                    '   Return 0 if the first entry of the dropdownlist is picked, that is "Select an Applicant"
                    '   Return 1, regardless which data entry in the dropdownlist is selected.
                    '   ==========================================================================================
                    
                    Response.Write(DropDownList_Applicant.SelectedIndex)
                    
                End If
                                        
                '   As the selectedindex is always 0 or 1, the following GV_Working_Area is either empty (for the "Select an Applicant"
                '   entry or DB data for the first data entry in dropdownlist.
                '   ====================================================================================================================
                            
                GV_Working_Area.DataBind()
                
            End If
    
        End Sub
           
        
        Protected Sub DropDownList_Applicant_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
                  
            '   Removed for simplicity - Do Nothing
            '   ====================================
            
        End Sub
    
    
        <div class="header">
            Problem Dropdown List, SelectedIndex always 1 OR 0 when first entry is selected
        </div>
        <p></p>
        <asp:DropDownList ID="DropDownList_Applicant" runat="server" AutoPostBack="true" AppendDataBoundItems="true" 
            DataSourceID="SqlDS_Applicant" DataTextField="applicant" DataValueField="dc"
            OnSelectedIndexChanged="DropDownList_Applicant_SelectedIndexChanged">
            <asp:ListItem Value="NA">Select An Applicant</asp:ListItem>
        </asp:DropDownList>
        <p>
        </p>
        <asp:GridView ID="GV_Working_Area" runat="server" DataSourceID="SqlDS_Working_Area" AutoGenerateColumns="False" >
            <Columns>
                <asp:BoundField DataField="applicant" HeaderText="Requestor" />
                <asp:BoundField DataField="ID" HeaderText="ID" />
                <asp:BoundField DataField="name" HeaderText="Name" />
            </Columns>
        </asp:GridView>
    
        <asp:SqlDataSource ID="SqlDS_Working_Area" runat="server" ConnectionString="<%$ ConnectionStrings:str_Connection %>"
            ProviderName="<%$ ConnectionStrings:str_Connection.ProviderName %>"
            SelectCommand="SELECT * FROM my_Working_Area WHERE applicant = ? and dc=? and status='NEW' order by id">
            <SelectParameters>
                <asp:ControlParameter Name="applicant" ControlID="Dropdownlist_Applicant" PropertyName="SelectedItem.Text" />
                <asp:ControlParameter Name="dc" ControlID="Dropdownlist_Applicant" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:SqlDataSource>
    
        <asp:SqlDataSource ID="SqlDS_Applicant" runat="server" ConnectionString="<%$ ConnectionStrings:str_Connection %>"
            ProviderName="<%$ ConnectionStrings:str_Connection.ProviderName %>"
            SelectCommand="select distinct applicant, dc from my_working_area where status='NEW' order by applicant">
        </asp:SqlDataSource>
    
    
    

    Wednesday, March 6, 2019 5:03 AM

Answers

  • User-698295532 posted

    Problem resolved.

    It is due to the duplication of datavaluefield of the said dropdownlist.

    Thank you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 6, 2019 6:52 AM
  • User61956409 posted

    Hi TiongChin,

    Welcome to ASP.NET forums.

    dropdownlist data is populated from SqlDS_Applicant datasource and the data is appended to the first entry with "Select An Applicant" text.  No matter which data entry (besides Select an Applicant) is selected, upon postback, the selectedindex is always 1

    Firstly, glad to hear that you resolve the issue by yourself.

    Besides, as you mentioned, in your code logic, we can find that you set DataValueField="dc" for your DropDownList control "DropDownList_Applicant", if the returned records with same/duplicate value of "dc" field, which would cause the issue that is same appears in your project.

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 6, 2019 7:38 AM

All replies

  • User-698295532 posted

    Problem resolved.

    It is due to the duplication of datavaluefield of the said dropdownlist.

    Thank you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 6, 2019 6:52 AM
  • User61956409 posted

    Hi TiongChin,

    Welcome to ASP.NET forums.

    dropdownlist data is populated from SqlDS_Applicant datasource and the data is appended to the first entry with "Select An Applicant" text.  No matter which data entry (besides Select an Applicant) is selected, upon postback, the selectedindex is always 1

    Firstly, glad to hear that you resolve the issue by yourself.

    Besides, as you mentioned, in your code logic, we can find that you set DataValueField="dc" for your DropDownList control "DropDownList_Applicant", if the returned records with same/duplicate value of "dc" field, which would cause the issue that is same appears in your project.

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 6, 2019 7:38 AM