CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Apr 2005
    Posts
    78

    Question doPostBack then view source not updated

    Hi,

    I am landing on a page which lists a few thousand entries. When you land on the first page you get to see the first 20 items and then to navigate through the rest you have lots of next page links.

    Internally these are navigated to by postbacks of the form below:

    href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$dgSearch','Page$3')">


    When i say click on 'Page 3' the browser view is updated with fresh data for page 3 but when I then view source, what i see is still page 1 data. So the view has updated but view source has not been updated?

    Can anybody explain to me whats going on here?

    Thanks.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: doPostBack then view source not updated

    That's because it's using AJAX. Your computer is sending a request for data. Then, JavaScript updates the HTML contents. However, the page's source is not changed.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Apr 2005
    Posts
    78

    Re: doPostBack then view source not updated

    Quote Originally Posted by PeejAvery View Post
    That's because it's using AJAX. Your computer is sending a request for data. Then, JavaScript updates the HTML contents. However, the page's source is not changed.
    Ok.

    But is there a way underneath programatically to get at this new data as it must be there for when i then click on subsequent links that are only in the new page?

    I am using the document object model I say retrieve the body page element after each download but its still just the first page?

    Thanks.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: doPostBack then view source not updated

    Using responseText or responseXML you can see what information the server has sent back. No matter what, it won't update the actual source of the page, because the source hasn't changed.

    Or using DOM will always give you the actual, current structure.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Apr 2005
    Posts
    78

    Re: doPostBack then view source not updated

    Quote Originally Posted by PeejAvery View Post
    Using responseText or responseXML you can see what information the server has sent back. No matter what, it won't update the actual source of the page, because the source hasn't changed.

    Or using DOM will always give you the actual, current structure.
    Thats what I mean I am using the DOM. But when i get the main body element and then get the html\text from this it is still the first page. Where is it hiding the updates?
    Does anyone know how to get this from the DOM?

    Thanks.

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: doPostBack then view source not updated

    Are you sure you are looking at the DOM nodes which were updated?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Apr 2005
    Posts
    78

    Re: doPostBack then view source not updated

    Quote Originally Posted by PeejAvery View Post
    Are you sure you are looking at the DOM nodes which were updated?
    I think it must be something to do with this, as it must be in there somewhere.

    I assumed by taking the body element that would include all its children( including any that have been changed) and then taking the full text from this? That it has derived its text from iterating through all its children. Maybe for performance it has cached the source?

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: doPostBack then view source not updated

    You should walk the DOM if you want all child nodes. Here's a simple example.

    Code:
    function walkDOM(obj) {
      // obj is the current DOM object
      alert(obj.innerHTML);
    
      node = node.firstChild;
      while (obj) {
        walkDOM(obj);
        node = node.nextSibling;
      }
    }
    
    // call the walk function
    walkDOM(document.getElementsByTagName('body')[0]);
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    Apr 2005
    Posts
    78

    Re: doPostBack then view source not updated

    Certianly you leading me onto AJAX is a big help.
    Here is a good article on the subject although taken more from the perspective of a web-developer needing to debug the generated source

    http://ericappel.net/blog/ViewHTMLSo...tedByAJAX.aspx

    It certainly is there as when viewing the page with the update but where 'view source' still only returns the original page, you can enter the following into the address bar

    javascript:'<xmp>' + window.document.body.outerHTML+ '</xmp>'

    Which then gives you the generated source.

  10. #10
    Join Date
    Apr 2005
    Posts
    78

    Smile Working, but...

    I have something working now. By taking the text from the main body element after each internal navigation via the doPostBack.

    But have some problem with events, as the document is not downloaded no DocumentComplete event is being fired and its not clear what event to intercept for when its finally downloaded.

    I am comparing current source with previous source(with some optimisations) inbetween DoEvents and its working nicely but im aware this is not the proper way to do it. Ok for proof of concept.

    Im not familar with the DOM up till now just always overridden the stock events that come with the browser control wrapper.

    For these internal updates I really would like to just get at the element being updated. I assume an element within the html page has an id of 'ctl00$ContentPlaceHolder1$dgSearch'

    Is there not a search function to return a HTMLElement with this id, or must you traverse all the nodes comparing each one?

    Thanks for your help so far

  11. #11
    Join Date
    May 2002
    Posts
    10,943

    Re: Working, but...

    To get an element by ID directly, you can use document.getElementById('ID_HERE'). But, I'm guessing that ctl00$ContentPlaceHolder1$dgSearch is part of a reference to a JavaScript framework. The only other place I have seen a naming scheme similar to that is on MySpace.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  12. #12
    Join Date
    Apr 2005
    Posts
    78

    Question One problem remaining

    Ok, that id is used on the server side it seems, i found the corresponding one for the client side and am able to retrieve the element successfully.

    My one problem now is how do you determine using the DOM when this AJAX update has completed?

    I have update problems now, of when exactly i can go and retrieve the latest text for this element?

    Thanks.

  13. #13
    Join Date
    May 2002
    Posts
    10,943

    Re: doPostBack then view source not updated

    You need to wait until the readyState has changed.

    Sounds to me like you should go brush up a little more on AJAX from W3Schools.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  14. #14
    Join Date
    Apr 2005
    Posts
    78

    Question Re: doPostBack then view source not updated

    Quote Originally Posted by PeejAvery View Post
    You need to wait until the readyState has changed.

    Sounds to me like you should go brush up a little more on AJAX from W3Schools.
    Thanks for that.. im not actually a web-developer it working out how to do that in my client app via the DOM.

    I am using the WebBrowser control and handling events this way. I am using the ready state for detecting when a document download is complete but for AJAX these events are not 'bubbling' up to the webbrowser control.

    So i guess in this context the readystate is only firing for this specific element?

  15. #15
    Join Date
    May 2002
    Posts
    10,943

    Re: doPostBack then view source not updated

    If you're attempting to handle this through a control, then you are severly limited. The WebBrowser control cannot directly access JavaScript events...which is what readyState is.

    Out of curiosity, are you writing this in VB.NET? C#? What language?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured