locked
ASP form notrecognising a JS script RRS feed

  • Question

  • User876561910 posted

    Hi

    I am seeing this error message

    mpiler Error Message: CS1061: 'ASP.untitled_1_aspx' does not contain a definition for 'ShowDivs' and no extension method 'ShowDivs' accepting a first argument of type 'ASP.untitled_1_aspx' could be found (are you missing a using directive or an assembly reference?)

    Any ideas as to why ?

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@ Page Language="C#" %>
    <html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
    
    <head runat="server">
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Untitled 1</title>
    
    <script language="javascript" type="text/javascript">
            function ShowDivs() {
                var txt = document.getElementById("<%=MyDDL.ClientID %>").value;
    		var numdivs = 5;
    		var num = document.getElementById("MyDDL").value;
    		for (var i =1; i<=numdivs; i++) {
    		var which = "MyDiv_" + i;  
    			document.getElementById(which).style.display = num>=i? "block" : "none"; }}
        </script>
        
    </head>
    
    <body>
    
    <form id="form1" runat="server" >
    How many divs do you want to see ? 
    <asp:DropDownList runat="server" id="MyDDL" OnSelectedIndexChanged="ShowDivs()">
    	<asp:listitem></asp:listitem>
    	<asp:listitem>1</asp:listitem>
    	<asp:listitem>2</asp:listitem>
    	<asp:listitem>3</asp:listitem>
    	<asp:listitem>4</asp:listitem>
    	<asp:listitem>5</asp:listitem>				
    	</asp:DropDownList>
    	
    		<div id="MyDiv_1" style="display:none">  This is div 1</div><br/>
    		<div id="MyDiv_2" style="display:none">  This is div 2</div><br/>
    		<div id="MyDiv_3" style="display:none">  This is div 3</div><br/>
    		<div id="MyDiv_4" style="display:none">  This is div 4</div><br/>
    		<div id="MyDiv_5" style="display:none">  This is div 5</div><br/>
    </form>
    
    </body>
    
    </html>
    
    

    Friday, December 21, 2018 4:12 PM

Answers

  • User475983607 posted

    OnSelectedIndexChanged is a server side event handler not a client side event.

    <asp:DropDownList runat="server" id="MyDDL" OnSelectedIndexChanged="ShowDivs()">

    The error message is telling you that the handler does not exist in the code behind.

    Add an onchange="" attribute to fire a JavaScript event handler.

    <asp:DropDownList runat="server" id="MyDDL" onchange="ShowDivs()">

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 21, 2018 4:21 PM

All replies

  • User475983607 posted

    OnSelectedIndexChanged is a server side event handler not a client side event.

    <asp:DropDownList runat="server" id="MyDDL" OnSelectedIndexChanged="ShowDivs()">

    The error message is telling you that the handler does not exist in the code behind.

    Add an onchange="" attribute to fire a JavaScript event handler.

    <asp:DropDownList runat="server" id="MyDDL" onchange="ShowDivs()">

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 21, 2018 4:21 PM
  • User876561910 posted

    Thanks

    Friday, December 21, 2018 4:37 PM