c# - How to call a JavaScript function multiple times in a loop on page reload with ASP.NET - Stack Overflow

I'm using the local database functionality in Chrome and Safari and what I do when I want to save

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:

function deletePatient(patientID) {
        MaRDB.transaction(
        function (transaction) {
            transaction.executeSql("DELETE FROM Patients WHERE id = " + patientID + ";");
        }
    );
}

I then call this function from the code behind if the insert was successfull

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Delete", "<script language='javascript'>$(document).ready(function() {deletePatient(" + id + ");});</script>");

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?

 protected void btnSave_Click(object sender, EventArgs e)
        {
            bool successfullySent = false;

            SharePointConnection();
            int count = Convert.ToInt32(txtRows.Text);

            for (int i = 0; i <= 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("<Method ID='1' Cmd='New'>");

            sb_method.Append("<Field Name='Title'>" + name + "</Field>");
            sb_method.Append("<Field Name='Address'>" + address + "</Field>");
            sb_method.Append("<Field Name='City'>" + city + "</Field>");
            sb_method.Append("<Field Name='State'>" + state + "</Field>");
            sb_method.Append("<Field Name='ZIP'>" + zip + "</Field>");
            sb_method.Append("<Field Name='Country'>" + country + "</Field>");
            sb_method.Append("<Field Name='Phone'>" + phone + "</Field>");

            sb_method.Append("</Method>");

            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", "<script language='javascript'>$(document).ready(function() {deletePatient(" + id + ");});</script>");

            }
            else
            {
                divErrorMessage.Visible = true;
                lblErrorMessage.Text = "Failed to Save, Please Try Again";
            }
        }

}

Thanks in advance.

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:

function deletePatient(patientID) {
        MaRDB.transaction(
        function (transaction) {
            transaction.executeSql("DELETE FROM Patients WHERE id = " + patientID + ";");
        }
    );
}

I then call this function from the code behind if the insert was successfull

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Delete", "<script language='javascript'>$(document).ready(function() {deletePatient(" + id + ");});</script>");

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?

 protected void btnSave_Click(object sender, EventArgs e)
        {
            bool successfullySent = false;

            SharePointConnection();
            int count = Convert.ToInt32(txtRows.Text);

            for (int i = 0; i <= 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("<Method ID='1' Cmd='New'>");

            sb_method.Append("<Field Name='Title'>" + name + "</Field>");
            sb_method.Append("<Field Name='Address'>" + address + "</Field>");
            sb_method.Append("<Field Name='City'>" + city + "</Field>");
            sb_method.Append("<Field Name='State'>" + state + "</Field>");
            sb_method.Append("<Field Name='ZIP'>" + zip + "</Field>");
            sb_method.Append("<Field Name='Country'>" + country + "</Field>");
            sb_method.Append("<Field Name='Phone'>" + phone + "</Field>");

            sb_method.Append("</Method>");

            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", "<script language='javascript'>$(document).ready(function() {deletePatient(" + id + ");});</script>");

            }
            else
            {
                divErrorMessage.Visible = true;
                lblErrorMessage.Text = "Failed to Save, Please Try Again";
            }
        }

}

Thanks in advance.

Share Improve this question edited Oct 25, 2010 at 7:14 Morgan asked Oct 25, 2010 at 3:20 MorganMorgan 2772 gold badges7 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

I'm assuming you're calling the RegisterClientScriptBlock multiple times? In that case, the second parameter of your RegisterClientScriptBlock is the unique key of the script you're trying to inject. Since its always the same, in effect you're basically 'overwriting' each previous script with the latest one.

Try it again, and make sure your key is unique every time you call the RegisterClientScriptBlock (for example, append a counter to it?).

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Delete" + counter.ToString(), "<script language='javascript'>$(document).ready(function() {deletePatient(" + id + ");});</script>");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745673397a4639542.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信