-1

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.

bruno
  • 2,154
  • 5
  • 39
  • 64

2 Answers2

0

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?

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • 2
    Yes, the exception was triggered on that line. I will try it. Thanks. Why ClientID? – bruno Jul 01 '13 at 11:16
  • @bruno Why ClientID, because this is the one that return the id as is render on html by asp.net. Look also: http://stackoverflow.com/questions/5763557/accessing-control-client-name-and-not-id-in-asp-net/5763632#5763632 – Aristos Jul 01 '13 at 12:02
  • @bruno Still you have nulls on your parametres. On which line you get that ? – Aristos Jul 01 '13 at 13:49
  • If i use var dT=document.getElementById("<%=ddlTopSearch.ClientID%>").value; it gives me the error. If i use var dT=document.getElementById("<%=ddlTopSearch.ClientID%>"); it gives me null. – bruno Jul 01 '13 at 13:50
  • Like u said, i must have null parametres. I´m going to check it. – bruno Jul 01 '13 at 13:52
  • @bruno I have update the answer, on `select`, dropdownlist control the value must be get by this way http://stackoverflow.com/questions/1085801/how-to-get-the-selected-value-of-dropdownlist-using-javascript – Aristos Jul 01 '13 at 13:54
-1

Please provide the more details

If you are using the master page in your .aspx page then you should know that the control id is changed at run time.

I think in your case the problem is due to the wrong control id.

Click here for more information regarding control id changing due to master page.

Suri
  • 518
  • 2
  • 6
  • 20