Reputation: 2004
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>
Upvotes: 1
Views: 11953
Reputation: 784
One more way to do it using JQuery :
var ddlReason = $("[id*=ddlReasons]");
var selectedValue = ddlReason.val();
Upvotes: 0
Reputation: 18769
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.
Upvotes: 1