Null Reference Exception When Trying to get Selected Value for Dropdownlist Inside FormView

StackOverflow https://stackoverflow.com/questions/21098762

  •  27-09-2022
  •  | 
  •  

Domanda

I have a dropdown list inside an asp.net formview and when I attempt to get the selected value of the ddl with jquery, I am getting a Null Reference Exception.

jQuery:

 if ($('#<%=fvSubscriber.FindControl("ddlTransactionType").ClientID %>').val() == "Disconnect")
 {
     alert("test");
 }

Markup:

 <asp:DropDownList ID="ddlTransactionType" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlTransactionType_SelectedIndexChanged" TabIndex="1">
     <asp:ListItem Selected="True" Value="Choose">Choose Type...</asp:ListItem>
     <asp:ListItem Value="Enroll">Enroll</asp:ListItem>
     <asp:ListItem Value="Disconnect">Disconnect</asp:ListItem>
     <%-- DISABLED transaction types during data entry phase
     <asp:ListItem Value="Transfer">Transfer</asp:ListItem>
     <asp:ListItem Value="Update">Update</asp:ListItem>
     <asp:ListItem Value="DeEnrollDeceased">De-enroll Deceased</asp:ListItem>
     <asp:ListItem Value="DeEnrollLeaving">De-enroll Leaving</asp:ListItem>
     <asp:ListItem Value="DeEnrollFailedRecertification">De-enroll Failed Recertification</asp:ListItem>
     <asp:ListItem Value="DeEnrollNonUsage">De-enroll NonUsage</asp:ListItem> --%>
 </asp:DropDownList>

The Null Reference Exception is on <%=fvSubscriber.FindControl("ddlTransactionType").ClientID %>
The exception happens when attempting to load the page. So the way I read this is that it does find the control but not the selected value. What am I missing here?

È stato utile?

Soluzione

Try:

if ($('[id*="ddlTransactionType"] option:selected').val() == "Disconnect")
 {
     alert("test");
 }

jQuery selector [id*="ddlTransactionType"] selects all elements whose ID contains ddlTransactionType. To get selected value from drop down, add option:selected (try to add it in your code to see will you get an exception).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top