Friday, September 16, 2011

Confirmation while deleting record from gridview in asp.net


Here i used LinkButton to show Delete link on gridview
<asp:GridView ID="GridView1" Runat="server" 
    DataSourceID="employeeDataSource" AutoGenerateColumns="False" DataKeyName="empid" >
   
    <Columns>

        <asp:BoundField HeaderText="Last" DataField="LastName" 
           SortExpression="LastName"></asp:BoundField>

        <asp:BoundField HeaderText="First" DataField="FirstName" 
           SortExpression="FirstName"></asp:BoundField>

        <asp:BoundField HeaderText="Hire Date" DataField="HireDate" 
           SortExpression="HireDate"
            DataFormatString="{0:d}">
        </asp:BoundField>

        <asp:TemplateField HeaderText="Seniority">
            <ItemTemplate>
               <asp:LinkButton id="Linkbutton1" runat="server" onClientClick="return confirm('Do you want to delete')" onCommand="Delete">Delete</LinkButton>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>

    <SelectedRowStyle ForeColor="White" Font-Bold="True" 
         BackColor="#CE5D5A"></SelectedRowStyle>
    <RowStyle BackColor="#F7F7DE"></RowStyle>
</asp:GridView>

<asp:SqlDataSource id="employeeDataSource" runat="server" SelectCommand="select LastName,FirstName,HireDate from employee" DeleteCommand="Delete from employee where empid=@empid">

while deleting any particular record, it will ask for confirmation.
Here i used "onClienClick" property in linkbutton,it will run before getting postback and ask user for confirmation if user click ok "onCommand" get fire by calling DeleteCommand and record get deleted.

No comments:

Post a Comment