Categories

Tools

bitdefender

1&1 Web Hosting

Confirmation dialog for deleting a record in a GridView


When you want to delete a row in a GridView ASP.NET, is not normally post any confirmation. In some cases, to avoid accidental deletions of records, it is useful to bring up a confirmation dialog. If the user clicks the OK button, the record will be deleted from the database, if the user clicks Cancel no records will be deleted.

The following example shows how to insert a button in the GridView to delete the row. You use the OnClickClient event of the button, the javascript code is inserted to show a confirmation dialog and returns true if OK is pressed, false otherwise.

<asp:GridView runat="server"
	ID="GridViewUsers"
	AutoGenerateColumns="False"
	DataKeyNames="Email"
	DataSourceID="AccessDataSourceUsers">
	<Columns>
		<asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" SortExpression="Email" />
		<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
		<asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
		<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
		<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
		<asp:TemplateField>
			<ItemTemplate>
				<asp:ImageButton runat="server"
					ID="ImageButtonDelete"
					ImageUrl="images/delete.png"
					ToolTip="Delete current row"
					Text="Delete"
					CommandName="Delete"
					OnClientClick="return window.confirm('Do you want to delete the selected record?');" />
			</ItemTemplate>
		</asp:TemplateField>
	</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSourceUsers" runat="server" ConflictDetection="CompareAllValues" DataFile="~/App_Data/DB-User.mdb"
	SelectCommand="SELECT [Email], [Name], [Surname], [City], [Country] FROM [Accounts] ORDER BY [Name]"
	DeleteCommand="Delete * FROM [Accounts] WHERE [Email]=?"></asp:AccessDataSource>
Posted in ASP.NET by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.