473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem with AJAX

139 100+
Hai All,

I got one problem in ajax.im applying ajax for 3 dropdown lists when im selecting 3 dropdown values in first time it is working, in 2nd time it is taking the request as postback for same values and notpostback for other values.If i want to select same values for 2nd or 3rd time what i have to do..?plz any one help me.........


Thanks,
Aswath.
Dec 4 '07 #1
17 1533
Frinavale
9,735 Expert Mod 8TB
Hai All,

I got one problem in ajax.im applying ajax for 3 dropdown lists when im selecting 3 dropdown values in first time it is working, in 2nd time it is taking the request as postback for same values and notpostback for other values.If i want to select same values for 2nd or 3rd time what i have to do..?plz any one help me.........


Thanks,
Aswath.
The most obvious cause to the problem is if you are filling the drop down lists in page_load during every post back. This will remove any selection on the lists that were made during the last post back.

Check this before I start suggesting other reasons.

-Frinny
Dec 4 '07 #2
bhappy
139 100+
Hai ,

Thanks for replay,
For every request it is removing selection list.so im storing values in sessions and after using im clearing sessions.so in 2nd time if i select same value what i select in 1st time it is taking as postback request and i didn't get any session value .this is my problem...

Thanks,
Aswath.
Dec 5 '07 #3
bhappy
139 100+
Hai all,

Any answer to my problem....?

Thanks,
Aswath.
Dec 5 '07 #4
bhappy
139 100+
Hai
Thanks for replay,maintaining sessions is not the problem it is taking 2nd request as postback for same values.

Thanks,
Aswath.
Dec 6 '07 #5
Frinavale
9,735 Expert Mod 8TB
Hai
Thanks for replay,maintaining sessions is not the problem it is taking 2nd request as postback for same values.

Thanks,
Aswath.
Hi Aswath,

Have you tried to use a CascadingDropDown ?

If this control doesn't help you, could you also post your code that handles the postback events for your dropDownLists.

Thanks,

-Frinny
Dec 6 '07 #6
bhappy
139 100+
Hi,
im using 3 dropdownlists country,state,city.for example if i select city1 under stat1 under country1 i will get some values for country,state & city and im storing that values in sessions after inserting one record im clearing all sessions.if again i will select city1 under state1 under country1 it is taking as postback request and im not getting any values for country,state & city.If i select city2 under stat1 under country1 then i will get session value...this is my problem.

this is my code
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2.  
  3.         var  country = document.getElementById("dpd_country");   
  4.         var state = document.getElementById("dpd_state");
  5.         var city = document.getElementById("dpd_city");
  6.         var obj;   
  7.  
  8.     function populatestates()
  9.     {
  10.     //alert(document.getElementById("dd_coun").value);
  11.     var cid = document.getElementById("dpd_country").value       
  12.     obj = GetXmlHttpRequest();   
  13.     if (obj!=null )
  14.      {
  15.        obj.onreadystatechange = stateprocess;            
  16.        obj.open("GET","getcontact.aspx?id="+cid+"&status=0",false);
  17.        obj.send(null);      
  18.       }      
  19.     } 
  20.  
  21.     function dummycity()
  22.     {
  23.      var ci = document.getElementById("dpd_state").value   
  24.      obj = GetXmlHttpRequest();   
  25.      if (obj!=null )
  26.       {
  27.        obj.onreadystatechange = ncity;            
  28.         obj.open("GET","getcontact.aspx?id="+ci+"&status=1",false);
  29.        obj.send(null);      
  30.       }
  31.     }
  32.  
  33.  
  34.      function getplace()
  35.     {
  36.      var ci = document.getElementById("dpd_city").value   
  37.  
  38.      obj = GetXmlHttpRequest();   
  39.  
  40.      //alert(ci)
  41.  
  42.      if (ci =="1" || ci=="Other City")
  43.  
  44.                {
  45.             Form1.txt_othercity.disabled=false
  46.              Form1.txt_othercity.value=""
  47.  
  48.             }
  49.      else
  50.          {
  51.           Form1.txt_othercity.value=""
  52.           Form1.txt_othercity.disabled=true
  53.  
  54.             if (obj!=null)
  55.                 {
  56.                 obj.onreadystatechange = place;          
  57.                 obj.open("GET","getcontact.aspx?id="+ci+"&status=2",false);
  58.                 obj.send(null);      
  59.                 }
  60.             }
  61.  
  62.     }
  63.  
  64.  
  65.     function place()
  66.     {
  67.     }
  68.  
  69.  
  70.     function ncity()
  71.     {
  72.     if(obj.readyState == 4)
  73.     {
  74.         if (obj.status == 200)
  75.         {
  76.             retval = ""
  77.             var retval = obj.responseText;               
  78.             retval += "</City>"
  79.             clearSelect(document.getElementById("dpd_city"));           
  80.             populatecity(retval)
  81.             //alert("e")       
  82.         }
  83.         else
  84.           {
  85.             alert('error');   
  86.           }
  87.         }
  88.     }   
  89.  
  90.    function stateprocess()
  91.     {   
  92.      if(obj.readyState == 4)
  93.      {
  94.         if (obj.status == 200)
  95.         {
  96.                     retval = ""
  97.             var retval = obj.responseText;               
  98.             retval += "</States>"
  99.             clearSelect(document.getElementById("dpd_state"));   
  100.              populatestate(retval)   
  101.  
  102.          }
  103.          else
  104.          {
  105.          alert('error');   
  106.          }
  107.       }
  108.     }   
  109.  
  110.   function populatestate(response)
  111.   {
  112.  
  113.         var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")//Create the XMLDOM object
  114.         xmlDoc.async = false
  115.         xmlDoc.loadXML(response)       
  116.             var opt
  117.             var stateselem = xmlDoc.getElementsByTagName("States")
  118.             var stateelem = stateselem[0].getElementsByTagName("dp_state")
  119.             var opt = document.createElement("option")
  120.             opt.setAttribute("value",-1)
  121.             var textnode = document.createTextNode ("Select State")
  122.             opt.appendChild(textnode)
  123.               document.getElementById ("dpd_state").appendChild(opt)                           
  124.     for (var i = 0;i<stateelem.length ;i++)
  125.     {
  126.     var textnode = document.createTextNode (stateelem[i].getAttribute("cstate"))
  127.     //alert(stateelem[i].getAttribute("id"))
  128.     appendtoselect( document.getElementById ("dpd_state"),stateelem[i].getAttribute("id"),textnode)   
  129.     //alert(stateelem[i].getAttribute("id"))
  130.     }           
  131.   }
  132.  
  133.   function populatecity(response)
  134.   {
  135.         var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")//Create the XMLDOM object
  136.         xmlDoc.async = false
  137.         xmlDoc.loadXML(response)       
  138.             var opt
  139.             var cityselem = xmlDoc.getElementsByTagName("City")
  140.             var cityelem = cityselem[0].getElementsByTagName("dp_city")
  141.             var opt = document.createElement("option")
  142.             opt.setAttribute("value",-1)
  143.             var textnode = document.createTextNode ("Select City")
  144.             var textnode1 = document.createTextNode ("Other City")
  145.             opt.appendChild(textnode)
  146.             //opt.appendChild(textnode1)
  147.               document.getElementById ("dpd_city").appendChild(opt)       
  148.                appendtoselectcity( document.getElementById ("dpd_city"),1,textnode1)                   
  149.     for (var i = 0;i<cityelem.length ;i++)
  150.     {
  151.     var textnode = document.createTextNode (cityelem[i].getAttribute("ccity"))
  152.     //alert(stateelem[i].getAttribute("cstate"))
  153.     //alert(cityelem[i].getAttribute("id"))
  154.     appendtoselectcity( document.getElementById ("dpd_city"),cityelem[i].getAttribute("id"),textnode)   
  155.     }           
  156.    }
  157.  
  158.     function GetXmlHttpRequest()
  159.     { 
  160.  
  161.     var xreq = null;
  162.     //alert('asdf');
  163.     if (window.XMLHttpRequest)
  164.     {
  165.     xreq = new XMLHttpRequest();  
  166.     }
  167.     else if(typeof ActiveXObject != "undefined")
  168.     {
  169.     xreq = new ActiveXObject("Microsoft.XMLHTTP");
  170.        //alert("hello"+xreq);
  171.     }
  172.     return xreq;
  173.     }   
  174.  
  175.  
  176.     function clearSelect(select)
  177.     {
  178.     //alert(document.getElementById ("dd_state"));
  179.     //alert(select);
  180.      select.options.length = 0;//Set the select box's length to 1 so only "--Select--" is availale in the selection on calling this function.
  181.     //process()
  182.     //document.getElementById ("dd_city").options.length = 0;//You may want to write your own clearSelect logic
  183.     }       
  184.  
  185.     function process()
  186.     {
  187.     if(obj.readyState == 4)
  188.     {
  189.  
  190.         if (obj.status == 200)
  191.         {
  192.         retval = ""
  193.                 var retval = obj.responseText;       
  194.             //    alert(obj.responseText)
  195.             retval += "</Country>"
  196.             clearSelect(country);
  197.             populatelist(retval)       
  198.          }
  199.          else
  200.      {
  201.      alert('error');   
  202.      }
  203.     }
  204.    }
  205.  
  206.    function appendtoselect(select,value,content)
  207.   {
  208.   var opt
  209.  // alert(value) 
  210.                 opt = document.createElement("option")//Create an Element of type option
  211.                 opt.setAttribute("value",value)
  212.                 //opt.value = value;
  213.                 //Set the option's value
  214.  
  215.                 opt.appendChild(content)
  216.  
  217.                 //Attach the text content to the option
  218.                  document.getElementById ("dpd_state").appendChild(opt)
  219.   }
  220.  
  221.   function appendtoselectcity(select,value,content)
  222.   {
  223.   var opt
  224.  // alert(value) 
  225.                 opt = document.createElement("option")//Create an Element of type option
  226.                 opt.setAttribute("value",value)
  227.                 //opt.value = value;
  228.                 //Set the option's value
  229.  
  230.                 opt.appendChild(content)
  231.  
  232.                 //Attach the text content to the option
  233.                  document.getElementById ("dpd_city").appendChild(opt)
  234.    }
  235.         </script>
Dec 6 '07 #7
Frinavale
9,735 Expert Mod 8TB
I didn't realize that you are not using Ajax.NET....

I wasn't expecting to see your Ajax code...could you please post your .NET code for your Page_Load event and also the code that handles the event for your dropDownList Selection Change.

I have a feeling that your problem comes from the fact that you are not changing the DropDownList selection and so your PostBack event never takes place. This could be the reason for why these values are not stored in session.

To make this more clear:
When you change the selected index of a DropDownList it performs a PostBack to the server and your .NET code is executed, at that point the selected value is stored into session (from what I understand).

So the first time you go through the application there is nothing selected.

Then you click on the first DropDownList...at this point the index is changed (because it's now selected)...the server side code is executed and your value is stored in session.

Then you click on the second DropDownList...at this point the index is changed (because it too is selected)...the server side code and your value is stored in session.

Then you click on the third DropDownList...at this point the index is changed (same reason) and the server side code is executed...you then store everything into a database and clear the session.

Now you want to change one of the values and have the server update the database....but the problem is that none of the other DropDownLists have performed a PostBack (none of the selected indexes have changed)...therefore no values are stored in session.

In order to get around this I suggest removing the asynchronous call from the last DropDownList and perform a Full Page PostBack instead. At this point, when returning, you should set all of your DropDownList to select ""...this will force the user to select a new value for each of your DropDownLists and your values will be stored in session properly.

You know, you don't even have to store the values in session....on the last PostBack, you could just retrieve the values directly from the DropDownLists instead.

Does this help you at all?

If not, please post your .NET code so that I can better understand what the problem could be.


-Frinny

Im using 3 dropdownlists country,state,city.for example if i select city1 under stat1 under country1 i will get some values for country,state & city

and im storing that values in sessions after inserting one record im clearing all sessions.

if again i will select city1 under state1 under country1 it is taking as postback request and im not getting any values for country,state & city.If i select city2 under stat1 under country1 then i will get session value...this is my problem.
Dec 6 '07 #8
bhappy
139 100+
Hai Frinny,
Thanks for replay and sorry for dealy,Im happy that u understood my problem,In your sollution,
"Removing the asynchronous call from the last DropDownList and perform a Full Page PostBack instead. At this point, when returning, you should set all of your DropDownList to select ""...this will force the user to select a new value for each of your DropDownLists and your values will be stored in session properly."

Here how to remove asynchronous call for last dropdown list...?
my ajax code in page load is

dpd_country.Attributes.Add("onChange", "return populatestates();")
dpd_state.Attributes.Add("onChange", "return dummycity();")
dpd_city.Attributes.Add("onChange", "return getplace();")
Thanks,
Aswath.
Dec 12 '07 #9
Frinavale
9,735 Expert Mod 8TB
Hai Frinny,
Thanks for replay and sorry for dealy,Im happy that u understood my problem,In your sollution,
"Removing the asynchronous call from the last DropDownList and perform a Full Page PostBack instead. At this point, when returning, you should set all of your DropDownList to select ""...this will force the user to select a new value for each of your DropDownLists and your values will be stored in session properly."

Here how to remove asynchronous call for last dropdown list...?
my ajax code in page load is

dpd_country.Attributes.Add("onChange", "return populatestates();")
dpd_state.Attributes.Add("onChange", "return dummycity();")
dpd_city.Attributes.Add("onChange", "return getplace();")
Thanks,
Aswath.
Hi Aswath,

After reviewing what I had said earlier I believe you should remove all of the Ajax calls to the server and simply change the last dropDownList to auto PostBack.

Well, unless you require each list to be generated based off of the selection off the last list...then using Ajax would really help you.

Try commenting out the above posted code, change the dpd_country and dpd_state so that they do not perform a post back...make sure that the dpd_city does perform a post back...and change the code that handles your dpd_city's selected index change event so that it retrieves the values from dpd_country and dpd_state.

If this doesn't work, please post your .NET server side code that handles your dpd_city selected index change event.

-Frinny
Dec 13 '07 #10
bhappy
139 100+
Hai ,
Thanks for Replay,
If i give autopostback to true then the total page is refreshing.and another issue is if the dpd_city does perform a post back If i select same state/country which i was selected previously,then im not getting any session values for state/country.I tried like removing asynchronous calls, then also it is not working...

Thanks,
Aswath.
Dec 14 '07 #11
Frinavale
9,735 Expert Mod 8TB
Hai ,
Thanks for Replay,
If i give autopostback to true then the total page is refreshing.and another issue is if the dpd_city does perform a post back If i select same state/country which i was selected previously,then im not getting any session values for state/country.I tried like removing asynchronous calls, then also it is not working...

Thanks,
Aswath.
I'm going to need to see your .NET code to help you out here.

There is a free package distributed by Microsoft called AJAX.NET. It simplifies using Ajax with .NET web applications, providing tools such as an UpdatePanel....I really think that you would benefit by looking into this package.

Please post your .NET code of the methods that handle the selected index change for each of your DropDownLists.

-Frinny
Dec 14 '07 #12
bhappy
139 100+
Hai Frinny,

Thanks for replay,
Im using .net1.1 version it doesn't support ajax framework.in .net2.0 ajax framework will work,and my totel code is in Post# 7.where im binding all dropdownlist details in <html> and im calling those functions in code-behind like dpd_country.attributes.add('onchange',...........)

for country dpdlist -- populatestates() --method
for stste dpdlist -- dummycity() --method
for city dpdlist -- getplace() -- method these all methods in my Post# 7


Thanks,
Aswath.
Dec 14 '07 #13
Frinavale
9,735 Expert Mod 8TB
Hai Frinny,

Thanks for replay,
Im using .net1.1 version it doesn't support ajax framework.in .net2.0 ajax framework will work,and my totel code is in Post# 7.where im binding all dropdownlist details in <html> and im calling those functions in code-behind like dpd_country.attributes.add('onchange',...........)

for country dpdlist -- populatestates() --method
for stste dpdlist -- dummycity() --method
for city dpdlist -- getplace() -- method these all methods in my Post# 7


Thanks,
Aswath.
Could you please post your .NET code for your getcontact.aspx page.
Dec 14 '07 #14
bhappy
139 100+
Hai

This is my getcontact.aspx code

code(asp.net)
Expand|Select|Wrap|Line Numbers
  1.  con = New SqlConnection(constr)
  2.         con.Open()
  3.         Dim s As New StringBuilder
  4.         s = New StringBuilder
  5.         Dim cid As String = Request.QueryString("id")
  6.         a = cid
  7.  
  8.         If Request.QueryString("status") = 0 Then
  9.             Session("valuestate") = 0
  10.             Session("valuecity") = 0
  11.             Dim sql1 As String = "select statename as cstate,stateid as id from dp_state where country_id = '" & cid & "' order by statename for xml auto"
  12.             com = New SqlCommand(sql1, con)
  13.             dr = com.ExecuteReader()
  14.             If dr.HasRows Then
  15.                 If dr.Read Then
  16.                     s.Append(dr.GetString(0).Trim())
  17.                 End If
  18.             End If
  19.             Response.ContentType = "Text/xml"
  20.  
  21.             Response.Write("<States>" + s.ToString())
  22.             Response.End()
  23.         End If
  24.  
  25.         If Request.QueryString("status") = 1 Then
  26.             Session("valuecity") = 0
  27.             valuestate = Request.QueryString("id")
  28.             Session("valuestate") = Request.QueryString("id")
  29.             Dim sql1 As String = "select cityname as ccity,cityid as id from dp_city where stateid = '" & cid & "' order by cityname for xml auto"
  30.             com = New SqlCommand(sql1, con)
  31.             dr = com.ExecuteReader()
  32.             If dr.HasRows Then
  33.                 If dr.Read Then
  34.                     s.Append(dr.GetString(0).Trim())
  35.                 End If
  36.             End If
  37.             Response.ContentType = "Text/xml"
  38.             Response.Write("<City>" + s.ToString())
  39.             Response.End()
  40.         End If
  41.         If Request.QueryString("status") = 2 Then
  42.             Session("valuecity") = Request.QueryString("id")
  43.             valuecity = Request.QueryString("id")
  44. end if
  45.  
Thanks,
Aswath.
Dec 14 '07 #15
Frinavale
9,735 Expert Mod 8TB
OK I've tried 3 times now to reply to your message, but some thing's wrong with my browser and it keeps getting destroyed.

Anyways, I've spent too much time trying to answer your question already so I'm going to keep this short.

You aren't using .NET to it's fullest potential.
Since you are not using ASP.NET objects (like DropDownLists), and you are only using HTML <select> instead, you should consider using Hidden Fields instead of Session to store your selected values.

The reason why I didn't recommend this before was because I thought you were using the ASP.NET DropDownList. This object can be used by Server Code to determine it's selected value. The selected values, along with all of the other values, are stored in something called the ViewState. The ViewState is then used to recreate this object on the server and lets you access the Selected value.

Because you are not using ASP.NET objects in your solution, consider using Hidden Fields to remember the selected <select> list values and to pass these values to the server code.

So instead of setting the Session("valuestate") = Request.QueryString(...) in your server code. Declare a Hidden Field variable named "valuestate" and set it's value to the dpd_state list's selected value. You should do this in your Ajax function populatestates before you make the HttpRequest.

You'll have to do this for all of your lists.
In your ServerSide code, check the if all of these hidden values contain selected values. If this is the case, you should update your database. Otherwise, you should just populate the list that requires population.

This way you will update the database every time the user has selected all of the values, and then changes a selection.

Hope you understand what I'm trying to say.

-Frinny
Dec 14 '07 #16
bhappy
139 100+
Hai Frinny,

Many Many Thanks my problem resolved.Your help is precious,If you don't mind one more small issue,This code is working fine in IE but doesn't work in Mozilla.....
if you know any idea plz tell me...........

Thanks Once again.........


Aswath.
Dec 15 '07 #17
Frinavale
9,735 Expert Mod 8TB
Hai Frinny,

Many Many Thanks my problem resolved.Your help is precious,If you don't mind one more small issue,This code is working fine in IE but doesn't work in Mozilla.....
if you know any idea plz tell me...........

Thanks Once again.........


Aswath.
The reason why your code does not work in Mozilla (or any other browser) is because you are using ActiveX controls. These controls only work for IE (and only work if the end user has ActiveX enabled).

-Frinny
Dec 17 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

42
by: Greg | last post by:
Hi, I've designed a bookmark in Ajax / PHP that I will put soon on sourceforge.net. But I've got an very tricky bug. I try it on some computers with Internet Explorer/Windows, Firefox...
3
by: Alok yadav | last post by:
I have an open IP and on that IP our main application is hosted. it uses ajax. in web.config file i have register ajax handlers. there are also other sites or project on that IP. now my problem is...
13
by: alvinwoon | last post by:
URL: http://events.unl.edu/ Description: i coded a quick and dirty key navigation for the calendar. if you press left arrow on your keyboard, it will navigate to the previous date and fire an...
13
by: Karl | last post by:
Hi all. I've recently rebuilt my Vista Ultimate workstation with Visual Studio Team Developer edition. Ive also put on the SQL Server 2005 tools and it's fully patched. I was building a web...
3
by: equazcion | last post by:
Hi, I have an image reference (IMG) in my page that changes depending on the value of a database field. Clicking the image triggers an Ajax call to change the database field (toggles the field...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
3
by: willl69 | last post by:
Hi All, Ive been having a problem of late with one of my sites that uses PHP5 / Ajax. The problem is that periodically the ajax functions lock up and it gets stuck in the loading phase of the...
4
by: sheldonlg | last post by:
I haven't received an answer with my other post, so I am rephrasing it here. In php I have a 2D array which I "print". The headers force it to be a file on the user's system. The user receives...
12
by: sheldonlg | last post by:
Here is my situation. I am coding in an AJAX framework for an intranet application behind a vpn. Therefore, I can't give you a URL for the actual app. I made a sample app for viewing and am...
3
omerbutt
by: omerbutt | last post by:
hi there i have downloaded a prototype tooltip from http://www.nickstakenburg.com/projects/prototip/ the logic it uses is to call the script after creating the <div> for example i am using the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.