Hi mfoof,
Sorry for the delay. took me a while to figure this one out.
Anyway, here's what happened:
By default, a ButtonColumn renders as an <input type="button"> HTML element. Normally, this is fine, as some JavaScript is embedded to submit the form when the button is clicked. However, with the delete confirmation code, things get messed up. Take a look at this:
<input type="button" value="Delete" onclick="return confirm('Are you
sure you want to delete this product?');javascript
:__doPostBack
('ctl00$MainContentPlaceHolder$gvProduct','DeleteI tem$0')" />
When you click OK, you effectively have this:
onclick="return true";
This blocks the following JavaScript code from being executed and the form is never executed.
The fix is reasonably easy though.
1. Open the Products page in Design View
2. Open the Smart Task and choose Edit Columns
3. Locate the Delete column and convert it to a template by clicking the blue link.
You should end up with something like this:
<asp:TemplateField HeaderText="Delete" ShowHeader="False">
<ItemStyle Width="75px" />
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Button ID="Button1" runat="server"
CausesValidation="false" CommandName="DeleteItem" Text="Delete"
/>
</ItemTemplate>
</asp:TemplateField>
4. Modify the code so you end up with this:
<asp:TemplateField HeaderText="Delete" ShowHeader="False">
<ItemStyle Width="75px" />
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Button ID="Button1"
CommandArgument='<%# Eval("Id") %>'
runat="server" CausesValidation="false"
CommandName="DeleteItem" Text="Delete"
OnClientClick="return confirm('Are you sure you want to delete
this product?');" />
</ItemTemplate>
</asp:TemplateField>
(I added the CommandArgument and the OnClientClick properties)
Because the command argument now contains the product ID, you need to change some code in the code behind as well.
5. Locate the gvProduct_RowCommand event handler and change the case for deleteitem to this:
Case "deleteitem"
productId = Convert.ToInt32(e.CommandArgument)
Product.Delete(productId)
gvProduct.DataBind()
6. Run the page. If everything worked out as planned (it's a million degrees out here so maybe due to the heat I left something out) you should now be able to delete products.
If not, please let me know.
Next time when you post code here, can you please add a few line breaks? The page I am typing this message in is a about 3 feet wide... ;)
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of
ASP.NET 2.0 Instant Results and
Beginning Dreamweaver MX / MX 2004