Mubarek
Mubarek

Reputation: 2689

Confirmation with Data when Deleting a row in GridView is triggered only when data is number or null

I want to confirm the deletion of a row displaying a field value along with a message but this works only when the field contains numbers or null. The event is not triggered otherwise.

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                        CommandName="Delete" Text="Delete"  OnClientClick='<%# "return ConfirmDel(" + Eval("Fname") + ");" %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Fname" HeaderText="Fname" SortExpression="Fname" />
        . . . .
</asp:GridView>

and here is the javascript

 function ConfirmDel(fname) {
        return confirm('Are you sure to delete ' + fname);
 }

I searched a while for a similar question but found not the exact one.

Upvotes: 2

Views: 2011

Answers (1)

Crab Bucket
Crab Bucket

Reputation: 6277

It really looks fine. The only think I could think of is that the event doesn't fire when Eval("Fname") evaluates to Null.

I would try

OnClientClick='<%# "return xx(" + String.IsNullOrEmpty(Eval("Fname").ToString()) ? 
                                  "debug - is this firing" : Eval("Fname") + ");" %>'

Obviously remove the debug message one you have it working

Your xx function is incorrect as well - I'm assuming this is just a cut and paste error

function xx(fname) {         
    return confirm('Are you sure to delete ' + fname); 
}

Note ending }

Lastly - I really would change the name of xx to something more descriptive. I'm sure this is just debug/demo code so apologies for pointing it our. Just to flag up for other readers really.

EDIT

I think this is it

 OnClientClick="return xx('<%# Eval('Fname') %>');"

Note the two additional '. You need to pass this through as a string otherwise you will get strange effects depending what goes in. I would still guard against nulls as before though

Also the Eval is in the wrong place - you are evaluating the entire javascript string but you just should do the datafield.

EDIT 2

I'm trying the above and it just isn't working. If I were you I would abandon the notion of using eval to build the OnClientClick in the markup. Do it in the code behind in the RowDataBound event - using the FindControl method to pick out the link. That definitely works (honest)

I think this is my last word to be honest. Good luck

Almost the last word - just for completeness here's what I would do

void detailsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         string foreName = DataBinder.Eval(e.Row.DataItem, "Fname").ToString();
         LinkButton lnkButton = e.Row.FindControl("lnkLinkID") as LinkButton;
         lnkButton.OnClientClick = "Delete message to " + foreName
    } 
}

Upvotes: 1

Related Questions