Answered by:
CommandField Delete button not showing

Question
-
User-1865833014 posted
I'm new to Asp.Net and has just taken over an old project, where I need to do a few changes.
Right now I'm having a task where I need to add a delete button to an existing DetailsView, but so far my Google-Fu has failed me, as all the solutions I have found does not work
In this DetailsView I have activated the following buttons: Insert, Edit, Cancel and Delete. Only the Delete button is the one I have added (or tried to add, as it doesn't show)
Here is the simplified code (I have just removed most fields):
<asp:View ID="viewDetails" runat="server"> <asp:DetailsView DefaultMode="Edit" BorderColor="#CCCCCC" BorderWidth="1px" Font-Names="Verdana" Font-Size="8pt" ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="DistrictHeatSupplyID" DataSourceID="dsDistrictHeatingEdit" Height="50px" Width="325px"> <Fields> <asp:TemplateField HeaderText="ID"> <EditItemTemplate> <asp:Label ID="lblDistrictHeatSupplyID" Text='<%# Bind("DistrictHeatSupplyID") %>' runat="server"></asp:Label> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox CssClass="ekpro_txt" ID="txtDistrictHeatingSupplyID" Text='<%# Bind("DistrictHeatSupplyID") %>' runat="server"></asp:TextBox> </InsertItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Name" HeaderText="Navn" SortExpression="Name" ControlStyle-CssClass="ekpro_txt" ControlStyle-Width="300" /> <asp:CommandField ButtonType="Button" ShowDeleteButton="true" DeleteText="Delete" ShowCancelButton="true" CancelText="Fortryd" ShowInsertButton="true" InsertText="Gem" ShowEditButton="true" UpdateText="Gem" /> </Fields> <AlternatingRowStyle CssClass="tblrowalt" /> <RowStyle CssClass="tblrow" /> </asp:DetailsView> <asp:Button ID="btnAddVAT" runat="server" Text="Tillæg moms" /> <asp:SqlDataSource ID="dsFueltype" runat="server" ConnectionString="<%$ ConnectionStrings:AppConnectV5 %>" SelectCommand="SELECT Fuel FROM dbo.en_stm_fuel WHERE (type = 3)"></asp:SqlDataSource> <asp:SqlDataSource ID="dsDistrictHeatingEdit" runat="server" ConnectionString="<%$ ConnectionStrings:AppConnectV5 %>" SelectCommand="SELECT * FROM [en_stm_districtheating] WHERE ([DistrictHeatSupplyID] = @DistrictHeatSupplyID)" DeleteCommand="UPDATE [en_stm_districtheating] Set Deleted = 1 WHERE [DistrictHeatSupplyID] = @DistrictHeatSupplyID" InsertCommand="INSERT INTO [en_stm_districtheating] ([DistrictHeatSupplyID], [Name]) VALUES (@DistrictHeatSupplyID, @Name)" UpdateCommand="UPDATE [en_stm_districtheating] SET [Name] = @Name WHERE [DistrictHeatSupplyID] = @DistrictHeatSupplyID"> <SelectParameters> <asp:ControlParameter ControlID="gvDistrictHeating" Name="DistrictHeatSupplyID" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> <DeleteParameters> <asp:Parameter Name="DistrictHeatSupplyID" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="Name" Type="String" /> <asp:Parameter Name="DistrictHeatSupplyID" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="DistrictHeatSupplyID" Type="Int32" /> <asp:Parameter Name="Name" Type="String" /> </InsertParameters> </asp:SqlDataSource> </asp:View>
The only code in the codebehind, from what I can see, is code that gets next ID available from database and some code that sets the active view (and changes DetailsViewMode to Insert when clicking an insert button in a different view (but it's never set to Edit anywhere, but I can see when clicking an Edit button it's set to Edit. Is it because the DetailsViewMode is lost during some PostBack or something?)
Is there something I'm missing to add, to get it to actually show the Delete button?
Thursday, April 25, 2019 6:56 AM
Answers
-
User-893317190 posted
Hi Adagio_Botjek,
From your code ,your DetailsView's default mode is set to Edit.
By default , Edit mode will not show delete button.
If you want to show delete button at first show of details view , please change its default mode to ReadOnly.
If you still want to show delete button when in edit mode, you should write your logic in OnItemCommand event.
And click cancel button to see delete button.
<asp:DetailsView DefaultMode="Edit" BorderColor="#CCCCCC" OnItemCommand="DetailsView1_ItemCommand"
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e) { if (e.CommandName == "Cancel") { DetailsView1.DefaultMode = DetailsViewMode.ReadOnly; } }
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 25, 2019 8:56 AM
All replies
-
User-893317190 posted
Hi Adagio_Botjek,
From your code ,your DetailsView's default mode is set to Edit.
By default , Edit mode will not show delete button.
If you want to show delete button at first show of details view , please change its default mode to ReadOnly.
If you still want to show delete button when in edit mode, you should write your logic in OnItemCommand event.
And click cancel button to see delete button.
<asp:DetailsView DefaultMode="Edit" BorderColor="#CCCCCC" OnItemCommand="DetailsView1_ItemCommand"
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e) { if (e.CommandName == "Cancel") { DetailsView1.DefaultMode = DetailsViewMode.ReadOnly; } }
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 25, 2019 8:56 AM -
User-1865833014 posted
Hi Ackerly Xu
Thank you so much, that explains why it didn't show up.
Based on your answer, I did a quick search and found a different solution that seems to fit me a bit better, but couldn't have found it without your help
<asp:TemplateField ShowHeader="False"> <EditItemTemplate> <asp:Button ID="btnUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Gem"></asp:Button> <asp:Button ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Fortryd"></asp:Button> <asp:Button ID="btnDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Deaktiver"></asp:Button> </EditItemTemplate> <InsertItemTemplate> <asp:Button ID="btnUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Gem..."></asp:Button> <asp:Button ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Fortryd..."></asp:Button> </InsertItemTemplate> </asp:TemplateField>
Thursday, April 25, 2019 9:14 AM -
User1158470370 posted
Hello, I am new in asp.net field. I don't have much knowledge about it. Here, I hope, I can get help from here. I want to ask you that you said you are beginner, So, I asked you from where you can start learning.
Thursday, April 25, 2019 2:27 PM -
User-893317190 posted
Hi sound4haudio,
You could refer to asp.net official website.
https://docs.microsoft.com/en-us/aspnet/#pivot=aspnet&panel=aspnet_framework
You could learn webform, mvc and other framework through the link.
Best regards,
Ackerly Xu
Friday, April 26, 2019 1:18 AM -
User-1865833014 posted
Currently I'm just learning by looking at the code in the project I have taken over from previous developers (they are no longer available, so can't ask them)
I do remember a few bits and pieces from last time I worked with ASP.Net, but that was just a few months of development 15 years ago, back when I didn't have any experience with programming at all)
I'm mostly just using Google, when I have a specific problem to solve, but soon I'l need to start digging deeper into it, as I need to update this old website to something newer. I'm not sure where I'll start yet, but will soon find out I hope
Good luck learning
Friday, April 26, 2019 5:09 AM