简体   繁体   中英

How to display JavaScript confirmation dialog box AND run a function in GridView C# asp.net?

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()");
        }
    }

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; ");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM