Friday, 19 October 2012

Delete Confirmation in GridView


Delete Confirmation in GridView

Today I will going to show you how to apply delete confirmation on clicking Delete Button inside a Gridview..




Step 1:

Include this javascript in your masterpage head or in the head section of the page where u want this functionality:


<%--JavaScript Code For Confirmation Messagebox--%>

<script type="text/javascript" language="javascript">

        function ConfirmOnDelete(item)

        {

          if (confirm("Are you sure to delete: " + item + "?")==true)

            return true;

          else

            return false;

        }

</script>

<%--Code Ends Here--%>


Step 2:

Then in the gridview rowdatabound event:


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowState != DataControlRowState.Edit) // check for RowState

        {

            if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType

            {

                string id = e.Row.Cells[0].Text; // Get the id to be deleted

                //cast the ShowDeleteButton link to linkbutton

                LinkButton lb = (LinkButton)e.Row.Cells[9].Controls[0];

                if (lb != null)

                {

                    //attach the JavaScript function with the ID as the paramter

                    lb.Attributes.Add("onclick", "return ConfirmOnDelete('" + id + "');");

                }

            }

        }

    }


** Note have to specify the value of RowDatabound in the gridview Property> Event Section

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top