1

I have a GridView that has a column with a textbox where the user can enter a value and a column with a dropdownlist. If the user enter a values that is not equal to 1 in the textbox, they must select a value from the dropdownlist. The default value in the DDL is "Select" which is just an empty value that was coded in: ddlReasons.Items.Insert(0, new ListItem("Select"));. The DDL is created dynamically but select will always be the default value.

 function UpdateSerialQtyRcvd(SerNoID, QtyRcvd) {
        if (QtyRcvd != 1) {
            var ddl = document.getElementById("ddlReasons");
            var selectedValue = ddl.options[ddl.selectedIndex].value;
            if (selectedValue == "Select") {
                alert("Must select reason");
            }

        }
        else {
            PageMethods.UpdateSerialQtyRcvdUserControl(SerNoID, QtyRcvd, OnUpdateSuccess, OnUpdateFail);
        }
    }

If the user enters a value that is not 1 then I need to check what value is in the DDL. If "Select" is the value I need the user to select something else in the DDL but I am getting this error on the line var selectedValue = ddl.options[ddl.selectedIndex].value;

Uncaught TypeError: Cannot read property 'options' of null

Code for dropdownlist:

<asp:TemplateField HeaderText="Reason">
 <ItemTemplate>
  <asp:DropDownList ID="ddlReasons" runat="server" class="ReasonDDL" ></asp:DropDownList>
 </ItemTemplate> 
</asp:TemplateField>
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
user123456789
  • 1,914
  • 7
  • 44
  • 100
  • Try with jquery to get the selected value from drop down. [jQuery Get Selected Option From Dropdown](http://stackoverflow.com/questions/10659097/jquery-get-selected-option-from-dropdown) like `var selectedValue = $('#ddlReasons').find(":selected").text();` or `var selectedValue = $('#ddlReasons :selected').text();` – Braj Jul 29 '15 at 09:03
  • 4
    `document.getElementById("ddlReasons");` returns null, i.e. your dropdown couldn't be found. Post the relevant HTML. – phuzi Jul 29 '15 at 09:06
  • the dropdown should have unique Id, since you are using the dropdown in a GridView, you need to access the dropdown by class Name – sudhansu63 Jul 29 '15 at 09:09
  • 1
    Is `"ddlReasons"` the `ClientID` of the dropdown control? – Andreas Jul 29 '15 at 09:10
  • @phuzi code for dropdownlist added – user123456789 Jul 29 '15 at 09:13
  • @Braj I added the code you suggested but when checking `if (selectedValue == "Select")` selectedValue is " " – user123456789 Jul 29 '15 at 09:18

2 Answers2

1

You'll need to get the ClientId...

Javascript:

var ddl = document.getElementById("<%=ddlReasons.ClientID%>");

JQuery:

var ddl = $('#<%=ddlReasons.ClientID%>');

Check out the MSDN documentation on how to access the Client ID and the different settings.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
0

One more way to do it using JQuery :

var ddlReason = $("[id*=ddlReasons]");
var selectedValue = ddlReason.val();
Parthipan Natkunam
  • 756
  • 1
  • 11
  • 16