Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am using C#.net, My problem is - I am populating dropdownlist using Ajax,Javascript, its working in IE but not in Firefox and Chrome.
Please help me


C#
<pre lang="cs"><script type="text/javascript">
function showHint(str)
{
var xmlhttp;
var splitValue;

if (str.length==0)
  {
  document.getElementById(&quot;txtHint&quot;).innerHTML=&quot;&quot;;
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
    {
        document.getElementById(&#39;ddl_location&#39;).length=0;
        //document.form1.ddl_location.options.length=0;
        if(navigator.appName==&quot;Microsoft Internet Explorer&quot;)
        {
            var res=xmlhttp.responseText;
            var splitStr=res.split(&quot;,&quot;);

            var arrRes=new Array();
            for (var i=0; i &lt; splitStr.length;++i)
            {
                splitValue=splitStr[i].split(&quot;-&quot;);
                addOption(document.getElementById(&#39;ddl_location&#39;).value, splitValue[0],i);
                //addOption(document.form1.ddl_location, arrRes[i], arrRes[i]);

            }
        }
        else
        {
            document.getElementById(&quot;txtHint&quot;).innerHTML=xmlhttp.responseText;
        }
    }
  }
xmlhttp.open(&quot;GET&quot;,&quot;GetLocations.aspx?reqL=&quot;+str,true);
xmlhttp.send();
}

function addOption(selectbox,text,value )
{
var select = document.getElementById(&#39;ddl_location&#39;).value;
var optn = document.createElement(&quot;option&quot;);  //it was option
optn.text = text;
optn.value = value;
document.getElementById(&#39;ddl_location&#39;).options.add(optn);
}</pre>

</script>



Aspx Page:
<body>

<form name="form1" id="form1" runat="server">

First name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
<asp:DropDownList ID="ddl_location" name="ddl_location" runat="server" AutoPostBack="True" Width="198px" >



</form>
</body>
</html>


This is working on IE but not in Chrome and Firefox
Posted
Updated 13-Apr-12 1:24am
v3
Comments
jim lahey 13-Apr-12 5:01am    
Help us to help you: we can't telepathically see your code so you'll have to show it to us in the traditional way
deepakaitr12345 13-Apr-12 6:54am    
I think insted of using the document.getelementbyid("")
mathod use the $get or $find mathod

hope this will solve your problem

1 solution

Please use a libary to ensure cross browser compatibility. jQuery is a great free library with an active community developing it plus loads of plugins. I strongly advise you to use jQuery[^] or maybe even PrototypeJS[^]. I wouldn't really recommend PrototypeJS anylonger since jQuery is the better alternative in my opinion, but having an alternative can be handy sometimes :).

Regards,

Manfred
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900