Tuesday, August 27, 2013

Problem>> Adding Confirmation to delete operation in Gridview .
Solution >>

 protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
 if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //CommandField, i.e. I placed it as the first column
                foreach (Control control in e.Row.Cells[0].Controls)
                {
                      //change the button type depending on webcontrol.(button type)
                    //Swap string with:
                    //System.Web.UI.WebControls.DataControlLinkButton for LinkButton
                    //System.Web.UI.WebControls.DataControlImageButton for ImageButton
                    if (control.ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
                    {
                        LinkButton currentButton = (LinkButton)control;

                        if (currentButton.Text == "Delete")
                        {
                            currentButton.OnClientClick =
                                "if (!window.confirm('Are you sure?')) return false;";
                        }
                    }
                }
}

No comments:

Post a Comment