Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding "Delete Confirmation" in DetailsView for .NET 2.0

Posted on 2007-03-29
6
Medium Priority
?
1,603 Views
Last Modified: 2008-01-09
Could anyone tell me how to put delete confirmation pop up in detailsview control?  (in VB.NET)

I created 3 button ( Edit, Delete, New) as commandfield. I tried with <asp:templatefield> and it works but I would like implement in <asp:commandfield>.

Thanks in advance.
 
 <asp:CommandField ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True"    ShowInsertButton="True" />
'I would like to implement on above line.

<%--        <asp:TemplateField>
        <ItemTemplate>
        <asp:LinkButton
        Text="Delete" runat="server" Commandname="delete"  OnClientClick="return confirm('Delete?');">
        </asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>--%>  
0
Comment
Question by:iflash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 13

Expert Comment

by:KarinLoos
ID: 18815813
you can add the onclick attribute in the code behind for the page on the RowDataBound event.

example  (bear in mind index for cells depends in which colum you have the command button )
If e.Row.RowType = DataControlRowType.DataRow Then
   Dim lb As LinkButton
   lb = e.Row.Cells(0).Controls(0)
   lb.Attributes.Add("onclick", "return confirm('Delete?');")
End If
or c#
if ( e.Row.RowType == DataControlRowType.DataRow  )
{
    LinkButton lb =(LinkButton) e.Row.Cells[0].Controls[0] as LinkButton
   if ( lb != null )
  {
      lb.Attributes.Add("onclick", "return confirm('Delete?');")
  }

}
0
 

Author Comment

by:iflash
ID: 18815958
Ahh.. the code you gave doesnt seem to work for DetailsView... or maybe I'm just not experienced enough :(

After surfing for hours I've found a solution in C#... however I need it in VB because my C skills are non-existent. Do you know the VB equivalent for:

--------------------------------------------------------------------------------------------
protected void DetailsView1_ItemCreated(object sender, EventArgs e)
{
   // Test FooterRow to make sure all rows have been created
   if (DetailsView1.FooterRow != null)
   {
     // The command bar is the last element in the Rows collection
     int commandRowIndex = DetailsView1.Rows.Count-1;
     DetailsViewRow commandRow = DetailsView1.Rows[commandRowIndex];
     
     // Look for the DELETE button
     DataControlFieldCell cell = (DataControlFieldCell) commandRow.Controls[0];
     foreach(Control ctl in cell.Controls)
     {
       LinkButton link = ctl as LinkButton;
       if (link != null)
       {
          if (link.CommandName == "Delete")
          {
             link.ToolTip = "Click here to delete";
             link.OnClientClick = "return confirm('Do you really want to delete this record?');";
          }
       }
    }
}
--------------------------------------------------------------------------------------------

That would help heaps!
0
 
LVL 13

Expert Comment

by:KarinLoos
ID: 18816294
mm my vb skills are not that good, more of a c# person. but here goes
If DetailsView1.FooterRow != null Then
  Dim  commandRowIndex as Integer = DetailsView1.Rows.Count - 1
  Dim commandRow as DetalsViewRow = CType( DetailsView1.Rows( commandRowIndex ), DetailsViewRow)
  Dim cell as DataControlFieldCell  = CType( commandRow.Controls(0), DataControlFieldCell  )
  For Each ctl As Control In cell.Controls
      Dim lnk as LinkButton = CType( ctl, LinkButton )
      if lnk != null then
          If lnk.CommandName = "Delete" Then
              lnk.OnClientClick = " return confirm( 'Delete?' ); "
          End If
       End If
   Next
End If
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 13

Expert Comment

by:KarinLoos
ID: 18816311
in case the !=  doesnt work ( not equal ) then use <> 

ps a good compairson site for vb.net versus c# is
http://www.harding.edu/USER/fmccown/WWW/vbnet_csharp_comparison.html
0
 

Author Comment

by:iflash
ID: 18816409
Getting close!

how is:

protected void DetailsView1_ItemCreated(object sender, EventArgs e)

converted to VB?
0
 
LVL 13

Accepted Solution

by:
KarinLoos earned 500 total points
ID: 18822284
full code
Protected Sub DetailsView1_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not (DetailsView1.FooterRow Is Nothing) Then
              Dim  commandRowIndex as Integer = DetailsView1.Rows.Count - 1
              Dim commandRow as DetalsViewRow =  DetailsView1.Rows( commandRowIndex )
             Dim cell as DataControlFieldCell  = CType( commandRow.Controls(0), DataControlFieldCell  )
            ' Loop through controls to find Cancel button
            For Each ctl As Control In cell.Controls
                If TypeOf ctl Is LinkButton  Then
                    Dim lnk As LinkButton  = CType(ctl, LinkButton)
                    If lnk.CommandName = "Delete" Then
                         lnk.OnClientClick = " return confirm( 'Delete?' ); "
                    End If
                End If
            Next
        End If
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question