Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello Frinds,

I am using javascript. I call javascript function on del OnclintClick event. when I clicked on code behind on del button. it's give me Confirm box msg when I click on ok it's return YES. but second time Click on Ok its. append string like Ex "Yes,Yes"
how could I restrict to append it.
My Code Is -
Javascript code ----

C#
function Confirm() {
            var confirm_value
            confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";

            if (confirm("Do you want to delete supplier ?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
            }

            document.forms[0].appendChild(confirm_value);
        }


C# Code----------
C#
string confirmValue = string.Empty;
                    confirmValue = Request.Form["confirm_value"];
                    if (confirmValue == "Yes")
                    {
                        string str = string.Empty;
                        int rowIndex = Convert.ToInt32(e.CommandArgument);
                        ViewState["Index"] = rowIndex;
                        int Id = Convert.ToInt32(grdTenDistSupp.Rows[rowIndex].Cells[0].Text);
                        str = ClsResponse.DeleteTenderDistributionById(Id);
                        if (str == "SUCCESS")
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Supplier deleted successfully ');", true);
                            TenderDistributionSuppliersByTCId();

                        }
                    }


Thanks
Rameshwar
Posted

The issue here is you are appending the same element over and over again on multiple clicks. You can remove the previously added element before adding a new one like

JavaScript
function Confirm() {

    var form = document.forms[0];

    // Remove the previous element added
    var oldInput = document.getElementById('myInput');
    if (oldInput !== null) form.removeChild(oldInput);

    // Add a new element
    var confirm_value = document.createElement("INPUT");
    confirm_value.setAttribute('id', 'myInput');
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";

    if (confirm("Do you want to delete supplier ?")) {
        confirm_value.value = "Yes";
    } else {
        confirm_value.value = "No";
    }

    form.appendChild(confirm_value);
}
 
Share this answer
 
Comments
rameshvar 1-Oct-15 1:50am    
Thanks Palash you'r suggestion working... Thank u so much dear.
Palash Mondal_ 1-Oct-15 1:59am    
Glad it helped!
Clear the Yes/No value before assigning it.
JavaScript
function Confirm() {
    var confirm_value;
    
    confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    confirm_value.value = "";
    confirm_value.value = "";
    if (confirm("Do you want to delete supplier ?")) {
        confirm_value.value = "Yes";
    } else {
        confirm_value.value = "No";
    }
    
    document.forms[0].appendChild(confirm_value);
}


-KR
 
Share this answer
 
Comments
rameshvar 1-Oct-15 1:48am    
Thnaks Krunal Rohit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900