title: How to call a JavaScript function multiple times in a loop on page reload with ASP.NET
body: <p>I'm using the local database functionality in Chrome and Safari and what I do when I want to save this to a remote database is to create a hidden textfield and then using JSON to stringify each row. In the code behind I then parse each JSON object and insert it into the list. What I want to do now is to delete these rows from the local database. I have a JavaScript function called deletePatient:</p>
  
  <pre><code>function deletePatient(patientID) {
          MaRDB.transaction(
          function (transaction) {
              transaction.executeSql("DELETE FROM Patients WHERE id = " + patientID + ";");
          }
      );
  }
  </code></pre>
  
  <p>I then call this function from the code behind if the insert was successfull</p>
  
  <pre><code>Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Delete", "&lt;script language='javascript'&gt;$(document).ready(function() {deletePatient(" + id + ");});&lt;/script&gt;");
  </code></pre>
  
  <p>However, it only deletes the patient with the lowest ID (the first JSON object). When I step through the code it goes back to that code for each ID but only deletes one. If I try with an alert it also only shows one ID even though it iterates through the code N number of times. I guess it's some kind of conflict with postback and executing a JavaScript function here but is it possible to solve?</p>
  
  <pre><code> protected void btnSave_Click(object sender, EventArgs e)
          {
              bool successfullySent = false;
  
              SharePointConnection();
              int count = Convert.ToInt32(txtRows.Text);
  
              for (int i = 0; i &lt;= count; i++)
              {   
                  string p = String.Format("{0}", Request.Form["hiddenField" + i]).ToString();
                  JObject o = JObject.Parse(p);
                  id = (int)o["id"];
                  string name = (string)o["name"];
                  string address = (string)o["address"];
                  string city = (string)o["city"];
                  string state = (string)o["state"];
                  string zip = (string)o["zip"];
                  string country = (string)o["country"];
                  string phone = (string)o["phone"];
  
              StringBuilder sb_method = new StringBuilder();
              sb_method.Append("&lt;Method ID='1' Cmd='New'&gt;");
  
              sb_method.Append("&lt;Field Name='Title'&gt;" + name + "&lt;/Field&gt;");
              sb_method.Append("&lt;Field Name='Address'&gt;" + address + "&lt;/Field&gt;");
              sb_method.Append("&lt;Field Name='City'&gt;" + city + "&lt;/Field&gt;");
              sb_method.Append("&lt;Field Name='State'&gt;" + state + "&lt;/Field&gt;");
              sb_method.Append("&lt;Field Name='ZIP'&gt;" + zip + "&lt;/Field&gt;");
              sb_method.Append("&lt;Field Name='Country'&gt;" + country + "&lt;/Field&gt;");
              sb_method.Append("&lt;Field Name='Phone'&gt;" + phone + "&lt;/Field&gt;");
  
              sb_method.Append("&lt;/Method&gt;");
  
              XmlDocument x_doc = new XmlDocument();
              XmlElement xe_batch = x_doc.CreateElement("Batch");
              xe_batch.SetAttribute("OnError", "Continue");
  
              xe_batch.InnerXml = sb_method.ToString();
  
              try
              {
                  //updating the list
                  XmlNode xn_return = listsObj.UpdateListItems(ConfigurationManager.AppSettings["SaveToSPList"].ToString(), xe_batch);
  
                  if (xn_return.InnerText == "0x00000000")
                  {
                      successfullySent = true;
                  }
                  else
                  {
                      successfullySent = false;
                  }
              }
              catch
              {
                  successfullySent = false;
              }
  
              if (successfullySent)
              {
                  divSuccessfulMessage.Visible = true;
                  lblSuccessfulMessage.Text = "Report Successfully Saved";
  
                  Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Delete", "&lt;script language='javascript'&gt;$(document).ready(function() {deletePatient(" + id + ");});&lt;/script&gt;");
  
              }
              else
              {
                  divErrorMessage.Visible = true;
                  lblErrorMessage.Text = "Failed to Save, Please Try Again";
              }
          }
  
  }
  </code></pre>
  
  <p>Thanks in advance.</p>