【问题标题】:Null Reference Exception When Trying to get Selected Value for Dropdownlist Inside FormView尝试获取 FormView 内下拉列表的选定值时出现空引用异常
【发布时间】:2014-01-14 02:54:26
【问题描述】:

我在 asp.net formview 中有一个下拉列表,当我尝试使用 jquery 获取 ddl 的选定值时,我得到一个空引用异常。

jQuery:

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

标记:

 <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>

Null Reference Exception 位于 &lt;%=fvSubscriber.FindControl("ddlTransactionType").ClientID %&gt;
尝试加载页面时发生异常。所以我读这个的方式是它确实找到了控件但没有找到选定的值。我在这里错过了什么?

【问题讨论】:

    标签: jquery asp.net formview


    【解决方案1】:

    试试:

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

    jQuery 选择器[id*="ddlTransactionType"] 选择 ID 包含 ddlTransactionType 的所有元素。 要从下拉列表中获取选定的值,请添加 option:selected(尝试将其添加到您的代码中以查看是否会出现异常)。

    【讨论】:

    • 没有例外!!谢谢,你能解释一下这是做什么的吗?为什么我的 findcontrol 语法在这种情况下无效?
    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 2014-07-11
    • 2011-02-02
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    相关资源
    最近更新 更多