Problem with AJAX

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhappy
    New Member
    • Jul 2007
    • 139

    Problem with AJAX

    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.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by bhappy
    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

    Comment

    • bhappy
      New Member
      • Jul 2007
      • 139

      #3
      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.

      Comment

      • bhappy
        New Member
        • Jul 2007
        • 139

        #4
        Hai all,

        Any answer to my problem....?

        Thanks,
        Aswath.

        Comment

        • bhappy
          New Member
          • Jul 2007
          • 139

          #5
          Hai
          Thanks for replay,maintain ing sessions is not the problem it is taking 2nd request as postback for same values.

          Thanks,
          Aswath.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by bhappy
            Hai
            Thanks for replay,maintain ing 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 CascadingDropDo wn ?

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

            Thanks,

            -Frinny

            Comment

            • bhappy
              New Member
              • Jul 2007
              • 139

              #7
              Hi,
              im using 3 dropdownlists country,state,c ity.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
              [code=javascript]
              <script language="javas cript" type="text/javascript">

              var country = document.getEle mentById("dpd_c ountry");
              var state = document.getEle mentById("dpd_s tate");
              var city = document.getEle mentById("dpd_c ity");
              var obj;

              function populatestates( )
              {
              //alert(document. getElementById( "dd_coun").valu e);
              var cid = document.getEle mentById("dpd_c ountry").value
              obj = GetXmlHttpReque st();
              if (obj!=null )
              {
              obj.onreadystat echange = stateprocess;
              obj.open("GET", "getcontact.asp x?id="+cid+"&st atus=0",false);
              obj.send(null);
              }
              }

              function dummycity()
              {
              var ci = document.getEle mentById("dpd_s tate").value
              obj = GetXmlHttpReque st();
              if (obj!=null )
              {
              obj.onreadystat echange = ncity;
              obj.open("GET", "getcontact.asp x?id="+ci+"&sta tus=1",false);
              obj.send(null);
              }
              }


              function getplace()
              {
              var ci = document.getEle mentById("dpd_c ity").value

              obj = GetXmlHttpReque st();

              //alert(ci)

              if (ci =="1" || ci=="Other City")

              {
              Form1.txt_other city.disabled=f alse
              Form1.txt_other city.value=""

              }
              else
              {
              Form1.txt_other city.value=""
              Form1.txt_other city.disabled=t rue

              if (obj!=null)
              {
              obj.onreadystat echange = place;
              obj.open("GET", "getcontact.asp x?id="+ci+"&sta tus=2",false);
              obj.send(null);
              }
              }

              }


              function place()
              {
              }


              function ncity()
              {
              if(obj.readySta te == 4)
              {
              if (obj.status == 200)
              {
              retval = ""
              var retval = obj.responseTex t;
              retval += "</City>"
              clearSelect(doc ument.getElemen tById("dpd_city "));
              populatecity(re tval)
              //alert("e")
              }
              else
              {
              alert('error');
              }
              }
              }

              function stateprocess()
              {
              if(obj.readySta te == 4)
              {
              if (obj.status == 200)
              {
              retval = ""
              var retval = obj.responseTex t;
              retval += "</States>"
              clearSelect(doc ument.getElemen tById("dpd_stat e"));
              populatestate(r etval)

              }
              else
              {
              alert('error');
              }
              }
              }

              function populatestate(r esponse)
              {

              var xmlDoc=new ActiveXObject(" Microsoft.XMLDO M")//Create the XMLDOM object
              xmlDoc.async = false
              xmlDoc.loadXML( response)
              var opt
              var stateselem = xmlDoc.getEleme ntsByTagName("S tates")
              var stateelem = stateselem[0].getElementsByT agName("dp_stat e")
              var opt = document.create Element("option ")
              opt.setAttribut e("value",-1)
              var textnode = document.create TextNode ("Select State")
              opt.appendChild (textnode)
              document.getEle mentById ("dpd_state").a ppendChild(opt)
              for (var i = 0;i<stateelem.l ength ;i++)
              {
              var textnode = document.create TextNode (stateelem[i].getAttribute(" cstate"))
              //alert(stateelem[i].getAttribute(" id"))
              appendtoselect( document.getEle mentById ("dpd_state"),s tateelem[i].getAttribute(" id"),textnode )
              //alert(stateelem[i].getAttribute(" id"))
              }
              }

              function populatecity(re sponse)
              {
              var xmlDoc=new ActiveXObject(" Microsoft.XMLDO M")//Create the XMLDOM object
              xmlDoc.async = false
              xmlDoc.loadXML( response)
              var opt
              var cityselem = xmlDoc.getEleme ntsByTagName("C ity")
              var cityelem = cityselem[0].getElementsByT agName("dp_city ")
              var opt = document.create Element("option ")
              opt.setAttribut e("value",-1)
              var textnode = document.create TextNode ("Select City")
              var textnode1 = document.create TextNode ("Other City")
              opt.appendChild (textnode)
              //opt.appendChild (textnode1)
              document.getEle mentById ("dpd_city").ap pendChild(opt)
              appendtoselectc ity( document.getEle mentById ("dpd_city"),1, textnode1)
              for (var i = 0;i<cityelem.le ngth ;i++)
              {
              var textnode = document.create TextNode (cityelem[i].getAttribute(" ccity"))
              //alert(stateelem[i].getAttribute(" cstate"))
              //alert(cityelem[i].getAttribute(" id"))
              appendtoselectc ity( document.getEle mentById ("dpd_city"),ci tyelem[i].getAttribute(" id"),textnode )
              }
              }

              function GetXmlHttpReque st()
              {

              var xreq = null;
              //alert('asdf');
              if (window.XMLHttp Request)
              {
              xreq = new XMLHttpRequest( );
              }
              else if(typeof ActiveXObject != "undefined" )
              {
              xreq = new ActiveXObject(" Microsoft.XMLHT TP");
              //alert("hello"+x req);
              }
              return xreq;
              }


              function clearSelect(sel ect)
              {
              //alert(document. getElementById ("dd_state") );
              //alert(select);
              select.options. length = 0;//Set the select box's length to 1 so only "--Select--" is availale in the selection on calling this function.
              //process()
              //document.getEle mentById ("dd_city").opt ions.length = 0;//You may want to write your own clearSelect logic
              }

              function process()
              {
              if(obj.readySta te == 4)
              {

              if (obj.status == 200)
              {
              retval = ""
              var retval = obj.responseTex t;
              // alert(obj.respo nseText)
              retval += "</Country>"
              clearSelect(cou ntry);
              populatelist(re tval)
              }
              else
              {
              alert('error');
              }
              }
              }

              function appendtoselect( select,value,co ntent)
              {
              var opt
              // alert(value)
              opt = document.create Element("option ")//Create an Element of type option
              opt.setAttribut e("value",value )
              //opt.value = value;
              //Set the option's value

              opt.appendChild (content)

              //Attach the text content to the option
              document.getEle mentById ("dpd_state").a ppendChild(opt)
              }

              function appendtoselectc ity(select,valu e,content)
              {
              var opt
              // alert(value)
              opt = document.create Element("option ")//Create an Element of type option
              opt.setAttribut e("value",value )
              //opt.value = value;
              //Set the option's value

              opt.appendChild (content)

              //Attach the text content to the option
              document.getEle mentById ("dpd_city").ap pendChild(opt)
              }
              </script>[/code]

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                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)...ther efore 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

                Originally posted by bhappy
                Im using 3 dropdownlists country,state,c ity.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.

                Comment

                • bhappy
                  New Member
                  • Jul 2007
                  • 139

                  #9
                  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.Att ributes.Add("on Change", "return populatestates( );")
                  dpd_state.Attri butes.Add("onCh ange", "return dummycity();")
                  dpd_city.Attrib utes.Add("onCha nge", "return getplace();")
                  Thanks,
                  Aswath.

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Originally posted by bhappy
                    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.Att ributes.Add("on Change", "return populatestates( );")
                    dpd_state.Attri butes.Add("onCh ange", "return dummycity();")
                    dpd_city.Attrib utes.Add("onCha nge", "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

                    Comment

                    • bhappy
                      New Member
                      • Jul 2007
                      • 139

                      #11
                      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.

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Originally posted by bhappy
                        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

                        Comment

                        • bhappy
                          New Member
                          • Jul 2007
                          • 139

                          #13
                          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.att ributes.add('on change',....... ....)

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


                          Thanks,
                          Aswath.

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            Originally posted by bhappy
                            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.att ributes.add('on change',....... ....)

                            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.

                            Comment

                            • bhappy
                              New Member
                              • Jul 2007
                              • 139

                              #15
                              Hai

                              This is my getcontact.aspx code

                              code(asp.net)
                              [code=vbnet]
                              con = New SqlConnection(c onstr)
                              con.Open()
                              Dim s As New StringBuilder
                              s = New StringBuilder
                              Dim cid As String = Request.QuerySt ring("id")
                              a = cid

                              If Request.QuerySt ring("status") = 0 Then
                              Session("values tate") = 0
                              Session("valuec ity") = 0
                              Dim sql1 As String = "select statename as cstate,stateid as id from dp_state where country_id = '" & cid & "' order by statename for xml auto"
                              com = New SqlCommand(sql1 , con)
                              dr = com.ExecuteRead er()
                              If dr.HasRows Then
                              If dr.Read Then
                              s.Append(dr.Get String(0).Trim( ))
                              End If
                              End If
                              Response.Conten tType = "Text/xml"

                              Response.Write( "<States>" + s.ToString())
                              Response.End()
                              End If

                              If Request.QuerySt ring("status") = 1 Then
                              Session("valuec ity") = 0
                              valuestate = Request.QuerySt ring("id")
                              Session("values tate") = Request.QuerySt ring("id")
                              Dim sql1 As String = "select cityname as ccity,cityid as id from dp_city where stateid = '" & cid & "' order by cityname for xml auto"
                              com = New SqlCommand(sql1 , con)
                              dr = com.ExecuteRead er()
                              If dr.HasRows Then
                              If dr.Read Then
                              s.Append(dr.Get String(0).Trim( ))
                              End If
                              End If
                              Response.Conten tType = "Text/xml"
                              Response.Write( "<City>" + s.ToString())
                              Response.End()
                              End If
                              If Request.QuerySt ring("status") = 2 Then
                              Session("valuec ity") = Request.QuerySt ring("id")
                              valuecity = Request.QuerySt ring("id")
                              end if
                              [/code]
                              Thanks,
                              Aswath.
                              Last edited by Frinavale; Dec 14 '07, 04:21 PM. Reason: Added [code] tags

                              Comment

                              Working...