Click here to Skip to main content
15,884,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have used buttonfield in gridview.
i want to put delete confirmation on it.

how do this.
My code that i have try is given below.

<asp:GridView ID="GridView1" runat="server" Width="100%"
                  DataKeyNames="NoticeId" onrowdatabound="GridView1_RowDataBound"
                  onrowdeleting="GridView1_RowDeleting" AutoGenerateEditButton="True"
                  onrowediting="GridView1_RowEditing">
                  <Columns>
                      <asp:ButtonField CommandName="Delete" Text="Delete" ButtonType="Link"/>
                      <asp:BoundField />

                  </Columns >
              </asp:GridView>


in rowdatabound of gridview
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
  foreach (DataControlFieldCell cell in e.Row.Cells)
                    {
                        // check all cells in one row
                        foreach (Control control in cell.Controls)
                        {
                            // Must use LinkButton here instead of ImageButton
                            // if you are having Links (not images) as the command button.
                            ButtonField button = new ButtonField();
                            if (button != null && button.CommandName == "Delete")
                                // Add delete confirmation
                                button.onclientclick= "if (!confirm('Are you sure " +
                                       "you want to delete this record?')) return;";
                        }
                    }
}


But here OnclientClck Is not avalilable ,and it is giving me error.
I think my code is wrong.
what is the right way to do this?
Posted
Updated 10-Apr-13 21:49pm
v2

use this line for your code

C#
button.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
 
Share this answer
 
As i understand ButtonField does not support OnClientClick property.

Alternate solution would be to use a TemplateField to define ASP Button and call its Onclick event as follows.

<asp:gridview id="GridView1" runat="server" width="100%" xmlns:asp="#unknown">
                   DataKeyNames="NoticeId" onrowdatabound="GridView1_RowDataBound"
                   onrowdeleting="GridView1_RowDeleting" AutoGenerateEditButton="True"
                   onrowediting="GridView1_RowEditing">
                   <columns>

   <asp:templatefield>
                 <itemtemplate>
                     <asp:button id="Button1" runat="server" text="Delete" commandname="Delete" onclientclick="return ConfirmDeletion();" />
                 </itemtemplate>


                   </asp:templatefield></columns>
               </asp:gridview>




//Script in header tag

<script>

function ConfirmDeletion()

{

 return (confirm("Are you sure you want to delete the record?"));

}

<script>
 
Share this answer
 
v2
show javascript using response.write in code

JavaScript
response.write("<script>return confirm('Are You Sure')</script>");
 
Share this answer
 
v2
Comments
Member 9511889 11-Apr-13 4:02am    
please explain in brif,because i don't know response.write
Gopal Rakhal 11-Apr-13 4:09am    
response.write("<script>return confirm('Are You Sure');</script>");
Orcun Iyigun 11-Apr-13 4:24am    
You answered the same question 3 times??? Why? What is the necessity? Use your Improve solution button if you need to update your answer instead of add another solution..
If you change the line:

ButtonField button = new ButtonField();


into:

LinkButton button = control as LinkButton;


it should work.
 
Share this answer
 
Hi achieve that problem, using the below code, I hope it will be useful for you bye

XML
<script type="text/javascript" language="javascript">

       $(document).ready(function() {

           $(':image').each(function() {
               var valorRunatAntes = $(this).attr("onclick");
               control = $(this);
               $(this).attr("onclick", "");
               $(this).attr("onclick", " if (confirm('¿Esta seguro que desea cambiar la fecha de vigencia?')){" + valorRunatAntes+" ;return true;}else{return false;}");

           });

       });

    </script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900