1

I have written this JavaScript function that locates the users current scroll position after activating/deactivating a person.

The code works well but the only weird thing is when the page is getting updated, this strange rendering happens a second before displaying the design.

What have I done wrong here? (I can also point out that I get a HTTP 302 error when this happens)

JS:

function loadDoc() {
        $('.TellusAlternatingRowColor').load("AdminListUsers.aspx?column=Disabled&direc=False&a=chstat&z=+");
                   }

C#:

var toggleUrl = "AdminListUsers.aspx?column=" + (IsClicked.FirstOrDefault().Key ?? "Name") + "&direc=" + (IsClicked.FirstOrDefault().Value) + "&a=chstat&q=" + id.ToString() + "&d=" + disabled + "&z=" + Server.UrlEncode(txtSearchFor.Text);

    var hl = new HyperLink();
    hl.Text = status;
    hl.Style.Add(HtmlTextWriterStyle.Color, (disabled ? "red" : "green"));
    hl.NavigateUrl = toggleUrl;
    hl.Attributes.Add("onclick", "loadDoc();return true;");
    cell.Controls.Add(hl);
    tr.Cells.Add(cell);

    cell = new TableCell();
    cell.Width = new Unit("10%");

    cell.Controls.Add(new LiteralControl("<nobr>"));
  • you appear to be loading the same thing twice, once via native XHR, and again via jquery's load method. the XHR version will scroll down, but then refreshes the page immediately, which seems a bit pointless, why bother scrolling if you're just going to kill the page and refresh it? And since the two calls run asynchronously, there'll be a race to see which one finishes first, which might look like a "strange rendering" if you're not paying attention to what's actually happening on the network. – ADyson Jun 13 '17 at 13:13
  • @ADyson Yes you are correct but this is the only solution I see for this to work, I dont know any other way to save my scroll position with this redirection of data. Also just so that the row you choose shall be updated is flexible with AJAX. I dont know any other way to be honest, can you help me out here? – andrekordasti Jun 13 '17 at 13:40
  • why do you need to reload the page after the ajax call? This will just destroy the content you retrieved via ajax. If you don't refresh the page, then you don't need to save the scroll position. Unless I have misunderstood, I think all you need is the call to jQuery `.load()` method, and remove the rest. This will place your updated content into the .TellusAlternatingRowColor element. – ADyson Jun 13 '17 at 14:33
  • @ADyson Please check the updated question. Have I understood you correctly? Because this code behaves the same as the previous one. – andrekordasti Jun 14 '17 at 07:27
  • That was my suggestion, yes. I've just noticed that you're doing this via a hyperlink, but you've set a NavigateUrl and also not suppressed the default behaviour. I suggest setting the "navigateURL" property to `"#"`. – ADyson Jun 14 '17 at 08:59
  • Also, what does "AdminListUsers.aspx?column=Disabled&direc=False&a=chstat&z=+" actually return? Normally an aspx page returns a whole HTML page including the ``, `` tags etc - if you put this inside another element, it makes your page invalid. If you want to use ajax, you should use a WebMethod to return just the HTML that should actually be inserted into the element. – ADyson Jun 14 '17 at 09:00
  • @ADyson Please check this question instead, this seems to be a better approach. https://stackoverflow.com/questions/44538810/ajax-function-not-saving-scroll-position-after-redirection – andrekordasti Jun 14 '17 at 09:00
  • why did you ask a second identical question? And the same issue I just identified above with the hyperlink will still apply to that – ADyson Jun 14 '17 at 09:03
  • Possible duplicate of [Ajax function not saving scroll position after redirection](https://stackoverflow.com/questions/44538810/ajax-function-not-saving-scroll-position-after-redirection) – ADyson Jun 14 '17 at 10:10

0 Answers0