2

I have a gridview, and a linkbutton on this gridview.

When linkbutton is clicked, rowCommand fires, however I want to ask user to comfirm the click with a comfirmation box,

  • if yes -> rowCommand fires,
  • if no-> nothing happens.

I couldn't find a way to it.

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
HOY
  • 1,067
  • 10
  • 42
  • 85

3 Answers3

7

Add this as the LinkButton's OnClientClick property:

OnClientClick="return confirm('Do you really want?');"
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

try this.

if (e.Row.RowType == DataControlRowType.DataRow){  
 LinkButton link = (LinkButton)e.Row.FindControl("LinkButton1");    
 link .Attributes.Add("onclick", "return confirm('Are you sure to proceed with this 
action?');");
}
Nudier Mena
  • 3,254
  • 2
  • 22
  • 22
0

in my code behind:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    LinkButton del = e.Row.Cells[2].Controls[0] as LinkButton;
    del.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this role?');");
}

and in my markup:

<asp:GridView
    ID="GridViewRoles"
    runat="server"
    Width="350px"
    EmptyDataText="No Roles"
    AutoGenerateColumns="False"
    AllowPaging="False"
    PageSize="50"
    AllowSorting="True"
    CssClass="gridview"
    AlternatingRowStyle-CssClass="even"
    OnRowCommand="GridViewRoles_RowCommand"
    OnRowDataBound="GridViewRoles_RowDataBound"
    OnRowDeleting="GridViewRoles_RowDeleting" OnRowEditing="GridViewRoles_RowEditing">
    <Columns>

        <asp:BoundField DataField="Role" HeaderText="Role" SortExpression="Role" HeaderStyle-Width="170px" HeaderStyle-HorizontalAlign="Left" />
        <asp:ButtonField CommandName="Edit" Text="Edit" HeaderStyle-Width="50px" />
        <asp:CommandField ShowDeleteButton="True" />

    </Columns>
    <AlternatingRowStyle CssClass="even" />
</asp:GridView> 
Robert Green MBA
  • 1,834
  • 1
  • 22
  • 45