Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Confirm delete on a Link button

  Asked By: Almas    Date: Mar 20    Category: Asp.net    Views: 2974
  

I have found examples showing how to confirm a delete with a template column with the delete button being a asp:button command, I haven't though found an example where you can use a link button an still have a confirmation of deletion.

Is this possible or have I just not looked hard enough.

Here is the code for the button for those who are interested :

In your Datagrid_ItemCreated sub procedure add the following:


Select Case e.Item.ItemType

Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem

Dim myDeleteButton As Button

myDeleteButton = e.Item.FindControl("btnDelete")

myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this company?');")

End Select

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Cyka Jansen     Answered On: Mar 20

The same exact code  you have will work for the linkbutton as well

Dim myDeleteButton as LinkButton

 
Answer #2    Answered By: Robin Hayes     Answered On: Mar 20

I changed the code  (below ) but get this error:



ERROR:

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:


Line 51: Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem
Line 52: Dim myDeleteButton As LinkButton
Line 53: myDeleteButton = e.Item.FindControl("btnDelete")




Code <HTML>

<asp:TemplateColumn>

<ItemTemplate>

<asp:LinkButton runat="server" ID="btnDelete" Text="Delete" CommandName="Delete" CausesValidation="false"></asp:LinkButton>

</ItemTemplate>

</asp:TemplateColumn>



</HTML>



<CodeBehind>

Select Case e.Item.ItemType

Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem

Dim myDeleteButton As LinkButton

myDeleteButton = e.Item.FindControl("btnDelete")

myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete  this company?');")

End Select

</codebehind>

Can you see what dummy mistake I am making now?



 
Answer #3    Answered By: Ibtihaj Akhtar     Answered On: Mar 20

You have to cast it to a LinkButton. FindControl method returns a Control object (not a LinkButton one)

myDeleteButton = CType(e.Item.FindControl("btnDelete"), LinkButton)

 
Answer #4    Answered By: Leonardo Costa     Answered On: Mar 20

May need to do this as well



myDeleteButton = CType(e.Item.FindControl("btnDelete"),LinkButton)



also, make sure you have imports System.Web.UI.Controls namespace



I would in C#, so I sometimes forget the vb differences.

 
Answer #5    Answered By: Camille Garrett     Answered On: Mar 20

In my Datagrid I have added the delete  linkbutton from the property builder. So, I can't find the the id or name of this link  button from the HTML. Could you help as to how to find this control in order to attach a message on delete using C#. The code  id pasted below:


<asp:datagrid id=dgCategorie style="Z-INDEX: 112; LEFT: 18px; POSITION: absolute; TOP: 53px" runat="server" Height="130px" Width="739px" HorizontalAlign="Center" Font-Names="Verdana" AllowPaging="True" AllowSorting="True" CellPadding="3" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="Black" AutoGenerateColumns="False"
<Columns>
<asp:BoundColumn DataField="Cat_Nom" HeaderText="Categorie"></asp:BoundColumn>
<asp:BoundColumn DataField="Coul_Cat" HeaderText="Couleur"></asp:BoundColumn>
<asp:BoundColumn DataField="Tmpl_Cat" HeaderText="Template"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Modify" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>

 
Didn't find what you were looking for? Find more on Confirm delete on a Link button Or get search suggestion and latest updates.




Tagged: