0

When I delete a GridView row, I want to display JavaScript confirmation dialog box AND run a function. How do you do that?

Something like that:

protected void GridViewActivities_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
            lb.Attributes.Add("onClick", "return confirm('You are sure?'); + MyFunction()");
        }
    }
2
  • With the code above, does the confirmation appear and work? Commented May 28, 2018 at 17:02
  • the code you add for onclick event doesn't seem to be correct javascript Commented May 28, 2018 at 17:36

1 Answer 1

1

If you want to run your function based on confirmation box result, try this code

protected void GridViewActivities_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
            lb.Attributes.Add("onclick", "var result = confirm('You are sure?'); if(result) { MyFunction(); } return true; ");
        }
    }

If you want to run your function irrespective of the confirmation box result, try this code:

protected void GridViewActivities_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
            lb.Attributes.Add("onclick", "confirm('You are sure?'); MyFunction(); return true; ");
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

It's more correct to return result instead of true, otherwise the postback of the linkbutton would also run when the user answers the confirmation with no/false.
Well it depends what the requirements are. In the question the only requirement was to call the custom function after confirmation box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.