Confirmation dialog cancel button not cancelling event?

I have added confirm delete functionality to my GridView, however, when I cancel the confirm dialog using its Cancel button, the delete event still occurs. Any ideas?? I think it is because I have attached the confirm delete script to the GridView cell and not the control.
protected void GridView_RowDataBoundEventHandler(object sender, GridViewRowEventArgs e)
		{
			GridView gridView = (GridView)sender;
			if ( e.Row.RowType == DataControlRowType.DataRow )
			{				
            	DataRowView dataRowView = (DataRowView)e.Row.DataItem; //DataSet
				//DbDataRecord rowView = (DbDataRecord)e.Row.DataItem; //IDataReader
               	//Control c = (Control)e.Row.FindControl("xxxxx");
                //string xxxxx = (string)DataBinder.Eval(dataRowView, "xxxxx");	
				
				//confirm delete
                foreach (DataControlField field in ((GridView)sender).Columns)
                {
					Type fieldType = field.GetType();
                    if (this.ConfirmDelete && fieldType.Name == "CommandField" && ((CommandField)field).ShowDeleteButton)
                    {//confirm delete.
					
                        e.Row.Cells[gridView.Columns.IndexOf(field)].Attributes.Add("onclick", "return confirm(\"Are you sure you want to delete this item?\");");
                    }
                }
			}
		}

Open in new window

smaccaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ashish PatelCommented:
Try this

e.Row.Cells[gridView.Columns.IndexOf(field)].Attributes.Add("onClick", "return confirm('Are you sure you want to delete this item?');");

and if this doesnt work then just paste the source of html file here for this confirmation part.
0
p_davisCommented:
where is your logic to handle the return value?
0
Alpesh PatelCommented:
hi Smacca
first of all main thing is that from where ( from which control) your delete  event get fired

if it is a control inside the grid column or grid cell then the scenario is as under

your control fired postback for delete
its get bubbling to the grid. so grid column get click and it fire confirm buttton... which let u alert the confirm message ...... now your interact with that message...... and that message return false to the column control.... now actaully what heppen your page get triggered postback from the other control and your column get return false for postback so that is wrong.....

so try to assign the "onclick" attribute of the control throught which u get triggered delete.....

i think its hard to describe this thing but i tried my level best
if u have any doubt u can ask me

thanx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smaccaAuthor Commented:
You are exactly correct - thanks heaps for that.
				//confirm delete
                foreach (DataControlField field in ((GridView)sender).Columns)
                {
					Type fieldType = field.GetType();
                    if (this.ConfirmDelete && fieldType.Name == "CommandField" && ((CommandField)field).ShowDeleteButton)
                    {//confirm delete.
 
                        ImageButton btnDelete = (ImageButton)e.Row.Cells[gridView.Columns.IndexOf(field)].Controls[0];                         
                        btnDelete.CausesValidation = false;
                        btnDelete.Attributes.Add("onclick", "return confirm(\"Are you sure you want to delete this item?\");");
                    }
                }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.