Search code examples
c#javascriptasp.netjscript

Microsoft JScript runtime error: Object required in form


I have this html code:

<td align="center">
    <asp:Button ID="btnPesquisar" runat="server" Text="Pesquisar" Width="150px" CssClass="manu_btn" OnClientClick="return validaParamsEstatisticaTopSearch();" ToolTip="Prima para efectuar a pesquisa">
    </asp:Button>
</td>
<td style="font-size: 10px;">
    <asp:DropDownList ID="ddlTopSearch" runat="server" Width="250px" Font-Size="10px" AutoPostBack="true" ToolTip="Escolha o tipo de pesquisa">
    </asp:DropDownList>
</td>

and my function:

function validaParamsEstatisticaTopSearch(){

var dT=document.getElementById("ddlTopSearch").value;
var dL=document.getElementById("ddlLingua").value;
var tT=document.getElementById("txtNTop");

var tdI=document.getElementById("txtDiaI");
var tmI=document.getElementById("txtMesI");
var taI=document.getElementById("txtAnoI");

var tdF=document.getElementById("txtDiaF");
var tmF=document.getElementById("txtMesF");
var taF=document.getElementById("txtAnoF");


if( dT=="*" )
{ 
    alert("Por favor escolha uma das opções de selecção de pesquisa ('TopSearch')!");
    return false;
}

return true;
}

and it´s giving me an "Microsoft JScript runtime error: Object required".

I can´t seem to figure out what i am doing wrong.


Solution

  • This error is usually appears when the getElementById is return null on the parametre, and you then try to use that parameter.

    For example on this line, you probably did not find the ddlTopSearch

    var dT=document.getElementById("ddlTopSearch").value;
    

    because asp.net render it on page diferently, to get it correct/rendered ID use the ClientID as:

    var dT=document.getElementById("<%=ddlTopSearch.ClientID%>").value;
    

    The same with the rest ids.

    Also, I must note here that the Drop Down List did not get the value as the rest of input controls. To get the value you must use this javascript code:

    var e = document.getElementById("<%=ddlTopSearch.ClientID%>");
    var sTopSearchValue = e.options[e.selectedIndex].value;
    

    reference: Get selected value in dropdown list using JavaScript?