user1254053
user1254053

Reputation: 765

Can not get dropdown selected value

I have a usercontrol and its code is:

<asp:UpdatePanel ID="updBank" runat="server">
  <ContentTemplate>
      <asp:UpdateProgress ID="udpPro" runat="server" AssociatedUpdatePanelID="" DynamicLayout="true">
            <ProgressTemplate>
                <div style="position: fixed; width: 500px; padding-top: 70px; text-align: center; z-index: 9999999;">
                    <img src="images/indicator.gif">
                </div>
            </ProgressTemplate>
      </asp:UpdateProgress>        

    <asp:DropDownList ID="ddlBankType" runat="server"></asp:DropDownList>
    <asp:Button ID="btnFilter" runat="server" Text="Button" OnClick="btnFilter_Click" OnClientClick="SetValues();"  />
    <br />

    <asp:ListView ID="lstBankList" runat="server" OnPagePropertiesChanging="lstBankList_PagePropertiesChanging" >
    <LayoutTemplate> 
        <table border="1" cellpadding="2" cellspacing="0" id="Table1" runat="server" class="TableCSS" style="width:100%;">
            <tr id="Tr1" runat="server" class="TableHeader">     
                <td id="Td2" runat="server">Bank</td>
                <td id="Td3" runat="server">Type</td>                         
            </tr>

            <tr id="ItemPlaceholder" runat="server"></tr> 
            <tr id="Tr2" runat="server"> 
                <td id="Td7" runat="server" align="right" colspan="7" class="TableData"> 
                    <asp:DataPager ID="lvDataPager1" runat="server" PagedControlID="lstBankList" PageSize="2">
                    <Fields>
                        <asp:NumericPagerField ButtonType="Link" />
                    </Fields>
                </asp:DataPager>
                </td>
            </tr>
        </table>          
    </LayoutTemplate>
    <EmptyDataTemplate>No data..</EmptyDataTemplate>
    <ItemTemplate>
        <tr runat="server" class="TableData">
            <td runat="server"><asp:Label ID="Label2" runat="server" Text='<%#Eval("Bank")%>' /></td>
            <td runat="server"><asp:Label ID="Label3" runat="server" Text='<%#Eval("BankType")%>' /></td>                
        </tr>
    </ItemTemplate>
    </asp:ListView>

    <asp:HiddenField ID="hdnBankTypeId" runat="server" />

    </ContentTemplate>
</asp:UpdatePanel>

I want to set dropdown selected value hidden field but don't know how to do it. I have tried this:

   <script>
       function SetValues() {
           alert($('#<%=ddlBankType.ClientID %> option:selected').val());
         $('[ID$="hdnBankTypeId"]')[0].value = $('#<%=ddlBankType.ClientID %> option:selected').val();
       }
    </script>

I am not sure how to retrieve a dropdown value within a update panel and that is witin a usercontrol. Please advise.

Edit: I want the selected value and not the selected text.

Upvotes: 2

Views: 85

Answers (2)

Charu Maheshwari
Charu Maheshwari

Reputation: 1643

Try using the below code to get the value. Also make sure that you are getting the dropdown element before. Use

console.log($('#<%=ddlBankType.ClientID %>').length)
console.log($('#<%=ddlBankType.ClientID %> :selected').text());

Upvotes: 1

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

you can use below code to set value

$('#hdnBankTypeId').val($('#<%=ddlBankType.ClientID %> option:selected').val());

NOTE: you can directly use .val() to read value of dropdown instead of reading value from selected option

$('#hdnBankTypeId').val($('#<%=ddlBankType.ClientID %>').val());

Upvotes: 1

Related Questions