Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 18th, 2008, 07:57 AM
Authorized User
 
Join Date: Apr 2008
Posts: 25
Thanks: 0
Thanked 1 Time in 1 Post
Default How to enable deleting in DetailsView?

Hi! I am trying to create a delete function in DetailsView. I have already set up an SqlDataSource, generated Insert, Update and Delete statements in that SqlDataSource, clicked on Enable Deleting, Editing and Inserting in the DetailsView Taskbar. The table also has a primary key. But the Delete link does not appear. Here is the ASP code:

Code:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" DefaultMode="Insert" 
            Height="50px" Width="731px" AutoGenerateDeleteButton="True">
            <Fields>
                <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" 
                    InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />
                <asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator 
ID="RequiredFieldValidator1" ControlToValidate="TextBox1" 
runat="server" ErrorMessage="Please enter your first 
name."></asp:RequiredFieldValidator>
                        </EditItemTemplate>

<%-- Code truncated --%>

                </asp:TemplateField>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                    ShowInsertButton="True" ShowSelectButton="True" />
            </Fields>
        </asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:nexConnectionString %>" 
            DeleteCommand="DELETE FROM [Employees] WHERE [EmployeeID] = @EmployeeID" 
            InsertCommand="INSERT INTO [Employees] ([LastName],
 [FirstName], [Designation], [Address], [City], [Telephone], 
[Birthdate], [DepartmentID], [Email]) VALUES (@LastName, @FirstName, 
@Designation, @Address, @City, @Telephone, @Birthdate, @DepartmentID, 
@Email)" 
            SelectCommand="SELECT * FROM [Employees] WHERE ([EmployeeID] = @EmployeeID)" 

            UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName, 
[FirstName] = @FirstName, [Designation] = @Designation, [Address] = 
@Address, [City] = @City, [Telephone] = @Telephone, [Birthdate] = 
@Birthdate, [DepartmentID] = @DepartmentID, [Email] = @Email WHERE 
[EmployeeID] = @EmployeeID">
            <SelectParameters>
                <asp:QueryStringParameter Name="EmployeeID" QueryStringField="EmployeeID" 
                    Type="Int32" />
            </SelectParameters>
            <DeleteParameters>
                <asp:Parameter Name="EmployeeID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="LastName" Type="String" />
<%-- Code Truncated --%>
          <asp:Parameter Name="Email" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
Thanks for your help.
 
Old June 18th, 2008, 10:51 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Take a look at this:
Code:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
  DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" DefaultMode="Insert"
  Height="50px" Width="731px" AutoGenerateDeleteButton="True">
  Notice how you've set the mode to Insert. When the DetailsView is in Insert mode, there is nothing to delete. Remove the attribute, and the Delete link is back.

Alternatively, use its ChangeMode method to switch the control in the relevant method.

Hope this helps

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Enable Property kekec3778 Beginning VB 6 2 January 8th, 2008 06:32 AM
Enable Update and Enable Delete check boxes don't mihabib ASP.NET 2.0 Basics 2 December 7th, 2006 11:44 AM
DetailsView faithfulman ASP.NET 2.0 Basics 1 September 22nd, 2006 11:16 PM
Enable - Macros alannoble26 Excel VBA 2 November 28th, 2005 09:08 AM
Enable Assertions ssmith71 BOOK: Beginning Java 2, JDK 5 Edition 0 June 3rd, 2005 12:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.