Tuesday, 11 June 2019

ASP.NET - JavaScript - Fix Error - JavaScript runtime error: Unable to set property 'IsValid' of undefined or null reference

0x800a138f - JavaScript runtime error: Unable to set property 'IsValid' of undefined or null reference

Watch this example on YouTube




to fix it replace

   <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Validate();" />

with

   <asp:Button ID="Button1" runat="server" Text="Button" />

Whole code:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPApplication._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>Test 1</asp:ListItem>
        <asp:ListItem>Test 2</asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Validate();" />

    <asp:ValidationSummary runat="server" HeaderText="Fix the following:" ShowMessageBox="true"
         ValidationGroup="MyValidationGroup" />
    <asp:CustomValidator runat="server" ErrorMessage="Something went wrong"
         ClientValidationFunction="Validate" ValidationGroup="MyValidationGroup" >

    </asp:CustomValidator>

    <script lang="javascript" type="text/javascript">
        function Validate(source, args) {
            var e = document.getElementById('<%= DropDownList1.ClientID %>')
            var str = e.options[e.selectedIndex].text;
            if (str != "") {
                args.IsValid = true;
            }
            else {
                args.IsValid = false;
            }
        }
    </script>
</asp:Content>

No comments:

Post a Comment