user123456789 user123456789 - 5 months ago 32
ASP.NET (C#) Question

javascript error: Cannot read property 'options' of null

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">
<asp:DropDownList ID="ddlReasons" runat="server" class="ReasonDDL" ></asp:DropDownList>


You'll need to get the ClientId...


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


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

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