user1080139
user1080139

Reputation: 165

Confirm for Linkbutton before performing delete in asp.net

I have a delete Linkbutton in my asp.net form. When the user clicks the button it will delete the contents from my db, which has been coded in the OnClick() of the button. But before that I need to have a confirm box which has yes or no options. How do I go about the deletion if the user clicks on "yes" and restrict deletion if user clicks on "no"? My problem is that onclick and onclientclick do not work together. Please help me ..Here is the sample code..

Where do I place the following script?

<script type="text/javascript">
    function Confirm() {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Do you want to delete data?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
    }
</script>

Code Behind:

protected void Mark_Click(object sender, EventArgs e)
{
    a.Delete(name);
}

Upvotes: 4

Views: 9568

Answers (2)

Romil Kumar Jain
Romil Kumar Jain

Reputation: 20775

Use following code:

OnclientClick="return Confirm()"

Return true/false from Confirm() function, if Confirm() return true then postback will happen otherwise not.

Refer online sample to test this

Note: We have used Confirm() in upper case because post has user defined function Confirm(). so please don't confuse with inbuilt function confirm() of java script.

Upvotes: 6

Syed Yunus
Syed Yunus

Reputation: 308

use OnClientClick="return confirm()"

Upvotes: 1

Related Questions