views:

402

answers:

4

Hello,

I have a GridView which supports deleting. I'd like to add a pop up window with a question like 'Are you sure you want to delete this row?'.

My code:

<asp:GridView id="GridAccounts" runat="server" AutoGenerateColumns="False" 
        ShowFooter="True" DataKeyNames="ID" 
        DataSourceID="AccountDataSource" onrowcommand="GridAccounts_RowCommand">
        <SelectedRowStyle BackColor="Lime" />
        <Columns>
            <asp:CommandField ButtonType="Image" ShowDeleteButton="True" DeleteImageUrl="~/Pictures/delete.jpg" />
            <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID">
                <EditItemTemplate>
                    <asp:Label ID="LabelAccountIDUpdate" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:Button ID="ButtonAccountIDInsert" runat="server" CommandName="Insert" Text="Insert" />
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="LabelAccountID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Code behind:

protected void GridPaymentMethod_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton deleteButton = (ImageButton)e.Row.Cells[0].Controls[0];
            MyMoney.PaymentMethodRow row = (MyMoney.PaymentMethodRow)((System.Data.DataRowView)e.Row.DataItem).Row;
            deleteButton.OnClientClick = string.Format("return confirm('Are you sure you want to delete payment method {0}?');", row.Name.Replace("'", @"\'"));
        }
    }

This renders as:

<input type="image" src="Pictures/delete.jpg" alt="Delete" onclick="return confirm('Are you sure you want to delete payment method Gotovina?');javascript:__doPostBack('ctl00$MainContent$GridPaymentMethod','Delete$0')" style="border-width:0px;" />

If I click OK on confirmation window, postback occurs, but nothing happens. If I comment out RowDataBound code, than delete works OK. Code whithout confirmation pop up:

<input type="image" src="Pictures/delete.jpg" alt="Delete" onclick="javascript:__doPostBack('ctl00$MainContent$GridPaymentMethod','Delete$0')" style="border-width:0px;" />

What am I doing wrong?

+2  A: 

I believe this is an example of what you are trying to do. It's cleaner and you don't have to go nutz with the code behind.

jimyshock
Quite. There's nothing magical about a CommandField - it just adds a standard LinkButton with a CommandName set to the action you want to launch. You can just replace it with your own LinkButton with the correct CommandName (eg. CommandName="Delete" would trigger delete event).
Dan Diplo
I used this tutorial: http://www.asp.net/Learn/data-access/tutorial-22-cs.aspx
_simon_
Great tip on commandName without OnClick handler in aspx.
adrianos
A: 
deleteButton.OnClientClick = string.Format("((!confirm('Are you sure you want to delete payment method {0}?'))?return false);", row.Name.Replace("'", @"\'"));
andres descalzo
+1  A: 

In your code you must change ButtonType="Image" to ButtonType="Link" - then onclick="..." will be rendered without javascript:___doPostBack(...) part. And in the GridPaymentMethod_RowDataBound event set something like deleteButton.Text = "<img src=\"path_to_image\" ... />" (use html entities instead of <>).

Or you can use ImageButton with ComamndName="delete" and ConfirmButtonExtender from ASP.NET AjaxToolkit suite.

Tadas
A: 

jimyshock, I used your approach and it works OK. Just one question - how can I add actual data into my question?

Now I have:

<asp:ImageButton ID="DeleteButton1" runat="server" CausesValidation="false" CommandName="Delete"
OnClientClick="return confirm('Are you sure you want to delete this payment method?');" ImageUrl="~/Pictures/delete.jpg" />

But I'd like to have something like this:

<asp:ImageButton ID="DeleteButton1" runat="server" CausesValidation="false" CommandName="Delete"
OnClientClick="return confirm('Are you sure you want to delete payment method <%# Bind('Name') %>');" ImageUrl="~/Pictures/delete.jpg" />

Text iside text inside text... Is this even possible?

_simon_