0

I have to click twice for the activation/deactivation of a user for some reason. Obviously I dont want that, it should be enough with one click. What am I doing wrong here?

(I am guessing that it's something wrong with the AJAX call)

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;"); //Calling the function here
cell.Controls.Add(hl);
tr.Cells.Add(cell);

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

cell.Controls.Add(new LiteralControl("<nobr>"));

var linkbtn = new HyperLink
{
  NavigateUrl = toggleUrl,
  Width = 16,
  Height = 16,
  CssClass = disabled ? "user-status-disabled" : "user-status-enabled"
};
linkbtn.Attributes.Add("id", "aButton_" + id);
linkbtn.Attributes.Add("onclick", "loadDoc();return true;"); //Calling the function here
cell.Controls.Add(linkbtn);
cell.Controls.Add(new LiteralControl("&nbsp; "));

JavaScript:

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function () {
    if (this.readyState === 4 && this.status === 200) {
      window.scrollTo(window.pageXOffset, window.pageYOffset);
      window.location.reload();
    }
  };
  xhttp.open("GET", "AdminListUsers.aspx?column=Disabled&direc=False&a=chstat&z=+", true);
  xhttp.send();
  $('.TellusAlternatingRowColor').load(document.URL + ' .TellusAlternatingRowColor');
}

Image for DataRows

  • 1
    ``XMLHttpRequest`` is asynchronous. Are you sure it's not just resolved by the time you click it again after clicking it the first time? – Joshua Jun 13 '17 at 07:26
  • Ah you are actually correct. I have to refresh the page to see the updated data though! I wish there was a way for it to do it by itself... Because you I am actually sorting the DataTable, so if I refresh the entire page, I have to resort it witch is not so optimal I guess. – andrekordasti Jun 13 '17 at 07:32
  • I guess I'll answer the post, then. – Joshua Jun 13 '17 at 08:30
  • have you checked whether removing the return true from that onclick event just try like hl.Attributes.Add("onclick", "loadDoc();"); and in linkbtn.Attributes.Add("onclick", "loadDoc();"); – Biby Augustine Jun 13 '17 at 08:59
  • @BibyAugustine Yes I have tried that without success. – andrekordasti Jun 13 '17 at 09:17
  • If it helps: check out the image I inserted. – andrekordasti Jun 13 '17 at 09:20

1 Answers1

0

The object you're using to contact the server, the XMLHttpRequest is part of the Ajax concept. Ajax stands for Asynchronous JavaScript and XML. This means that as soon as the request from the URL passed resolves, the event tied to onreadystatechange will be fired. Should the readyState and status be appropriate, your window calls will be resolved, too.

Being asynchronous, the whole browser won't stop because the server hasn't responded yet, the whole program will continue. You can still press the button because of this, but nothing will happen until that request is responded to. Clicking it again is sending another request to your server, which I'm assuming is also resolved, but your calls on window have happened by this point.

Joshua
  • 648
  • 7
  • 18