Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 31st, 2007, 03:13 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default Wrox CMS Deletions

Hi,

Ref: Content List Deletions. I see this has already been brought up before:http://p2p.wrox.com/topic.asp?TOPIC_ID=45586

I have converted the Delete Button to a template field and translated the gvContent_RowCommand Sub as below:

Protected Sub gvContent_RowCommand(ByVal sender As Object, _
        ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs ) Handles gvContent.RowCommand
        Dim recordIndex As Integer
        Dim recordId As Integer
        Select Case e.CommandName.ToLower()
            Case "edit"
                recordIndex = Convert.ToInt32(e.CommandArgument)
                recordId = Convert.ToInt32(gvContent.DataKeys(recordIndex).Va lue)
                Response.Redirect("AddEditContent.aspx?Id=" & recordId.ToString())
            Case "delete"
                recordId = Convert.ToInt32(e.CommandArgument)
                Content.GetItem(recordId)
                gvContent.DataBind()
        End Select
  End Sub

The delete function now works but I do not understand how. Why does the Templatefield not have the same problems as the Buttonfield? Am I correct in that the CommandArgument='<%#Eval("Id")%>' requests the Delete Parameter "Id". I am a little confused as to how when clicking the Delete Button (presumably running the above code) this then executes the deleteitem stored procedure. Is that what the Content.GetItem(recordId) does?

Sorry for being such a newb :) but just want to understand the code a bit better before I use it.

Thanks,

  #2 (permalink)  
Old January 31st, 2007, 03:43 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

No, Content.GetItem(recordId) *gets* an item, so it's pointless to call it without assigning it to a variable of type Content, like this:

Dim myContent As Content = Content.GetItem(recordId)

I think this is working because the markup of the page may have a Delete method specified that points to the delete method in the Content class.

Is that correct? Otherwise, I don't understand why this works. In that case, you need to post more code and explain the situation in more detail.

CommandArgument='<%#Eval("Id")%>'

is used to assign the ID of the item to the CommandArgument. So, whenever a command event fires you can request its name (CommandName) and then based on this name you can determine what to do. E.g. edit or delete an item. When CommandArgument is bound to the ID of the Content, you can use it directly to retrieve or delete the item.

In a default implementation however, where CommandArgument holds the *index* of the row that triggered the command, you need to use the DataKeys collection to retrieve the ID that belongs to the item you clicked.

Does this help?

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #3 (permalink)  
Old January 31st, 2007, 08:36 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar, thanks for the reply.

The code behind file is basically the code in my previous post (just missing the add new content button code). Markup for the rest of the page is:

<%@ Page Language="VB" MasterPageFile="~/AdminMaster.master" AutoEventWireup="false" CodeFile="ContentList.aspx.vb" Inherits="ContentList" Title="Manage Content" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceholder" runat="Server">
  <h1>Content List</h1>
  Below you find a list of all the content published on this site. Use the drop-down to view either active or deleted content items. Click the Create New button to create a new content item.
  <br />
  <br />
  <span class="ScreenTip">Show all</span>
  <asp:DropDownList ID="lstStatus" runat="server" AutoPostBack="True">
    <asp:ListItem Value="1">Active</asp:ListItem>
    <asp:ListItem Value="0">Deleted</asp:ListItem>
  </asp:DropDownList>
  <span class="ScreenTip">content items</span>
  <p>
  <asp:GridView ID="gvContent" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="sdsContent" Width="720px">
    <Columns>
      <asp:BoundField DataField="Id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="Id" Visible="False" />
      <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title">
        <ItemStyle Width="250px" />
      </asp:BoundField>
      <asp:BoundField DataField="ContentTypeDescription" HeaderText="Content Type" SortExpression="ContentTypeDescription">
        <ItemStyle Width="125px" />
      </asp:BoundField>
      <asp:BoundField DataField="CategoryDescription" HeaderText="Category" SortExpression="CategoryDescription">
        <ItemStyle Width="175px" />
      </asp:BoundField>
      <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit">
        <ItemStyle Width="75px" />
      </asp:ButtonField>
        <asp:TemplateField ShowHeader="False">
            <ItemStyle Width="75px" />
            <ItemTemplate>
                <asp:Button ID="Button1" CommandArgument='<%#Eval("Id")%>' runat="server" CausesValidation="False"
                CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this item?');" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
  </asp:GridView>
  </p>
    <br />
  <asp:Button ID="btnNew" runat="server" Text="Create New" /><br />
  <asp:SqlDataSource ID="sdsContent" runat="server" ConnectionString="<%$ ConnectionStrings:Cms %>"
  SelectCommand="sprocContentSelectList" SelectCommandType="StoredProcedure" DeleteCommandType="StoredProcedure" DeleteCommand="sprocContentDeleteSingleItem">
    <SelectParameters>
      <asp:ControlParameter ControlID="lstStatus" Name="visible" PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    <DeleteParameters>
      <asp:Parameter Name="id" Type="Int32" />
    </DeleteParameters>
  </asp:SqlDataSource>
</asp:Content>

The Content class doesn't appear to have a delete method (like the Customer Support Product Class) - presumably because the records aren't technically deleted. By clicking the button and using the "GetItem(Integer) as Content" method, is the content record ID not being passed to the stored procedure - if not, I don't understand why it works??

Rather than sending the default recordid should the following lines be added (as per the edit select statement)?

recordIndex = Convert.ToInt32(e.CommandArgument)
recordId = Convert.ToInt32(gvContent.DataKeys(recordIndex).Va lue)

Content.GetItem(recordId)
gvContent.DataBind()

Many thanks



  #4 (permalink)  
Old February 1st, 2007, 02:58 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Take a look at this:

<asp:SqlDataSource ID="sdsContent" runat="server" ConnectionString="<%$ ConnectionStrings:Cms %>"
 SelectCommand="sprocContentSelectList" SelectCommandType="StoredProcedure" DeleteCommandType="StoredProcedure" DeleteCommand="sprocContentDeleteSingleItem">

The SqlDataSource has a DeleteMethod set up. So, whenever the Delete command is triggered, sprocContentDeleteSingleItem "deletes" the content item. Your code in the code behind as no effect; you could remove it and it would still do the same.

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #5 (permalink)  
Old February 1st, 2007, 07:45 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Spot on. Commented out code and still "deleted" content as you said.

So to clarify, is there any need to have the code there? The Delete Confirmation message still appears and clicking cancel stops the deletion.

Or is there another reason for having the code? Obviously my attempt to translate the customer support solution has been a failure :(

Thanks for your help by the way.

Im planning to change the deletion method slightly so that deleting an active item removes the visible flag but then deleting a "deleted" item (where visible = false) permanently deleted the content.



  #6 (permalink)  
Old February 2nd, 2007, 02:49 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Correct; there's no need for this code.

In the CMS chapter, a lot of the database interaction is done by SqlDataSource controls. In this case, sprocContentDeleteSingleItem is used to the "delete" the record from the database.

The CustomSupport application on the other hand uses objects and methods on those objects to delete the items. So, instead of calling a stored procedure directly, you can Delete on an object that in turns eventually calls the Delete stored procedure.

Regarding your last requirement: you could do that in the stored procedure with an IF statement. The following pseudo (fake) code should give you an idea:

IF Record @id Exists and Deleted = 1
BEGIN
  DELETE FROM Content WHERE Id = @id
END
ELSE
  UPDATE Content SET Deleted = 1 WHERE Id = @id
END

This way, the item only gets deleted when it was previously "deleted" / disabled.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #7 (permalink)  
Old February 2nd, 2007, 02:50 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the heads up.

May not have gone about it the best way:

I modified sprocContentSelectList to include content.visible

I then added the visible field into the contentlist gridview as a boundfield (visible="false").

I added <asp:Parameter Name="visible" Type="Boolean"/> into my <DeleteParameters>

I then modified sprocContentDeleteSingleItem to the following:

@visible int, @id int
AS
IF @visible = 0
BEGIN
DELETE FROM [Content]
WHERE (Id = @Id)
END
ELSE
UPDATE [Content]
SET visible = 0
WHERE (Id = @Id)
RETURN

I know the SQL works as I have passed in the necessary parameters and the record is updated/deleted accordingly. However, I know the @visible parameter is not getting through. Im pretty sure I have declared the correct data types.

I am sure it is something quite simple that I have missed. Do I need to change the commandargument of the button?

Thanks (again)

retro

  #8 (permalink)  
Old February 2nd, 2007, 07:41 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Since it's a normal parameter and not the data key of the record, you'll need to manually assign the param a value in code, for example in the Selecting event.

However, it's easier to do it directly in the procedure:

@id int

AS

IF EXISTS (SELECT 1 FROM Content WHERE Id = @id and Visible = 0)
BEGIN

DELETE FROM [Content]
WHERE (Id = @Id)

END
ELSE

UPDATE [Content]
SET visible = 0
WHERE (Id = @Id)

RETURN

Basically, this just checks if the current @id record is already invisible. If so, it deletes the record; otherwise it sets deleted to 0.

Does this help?

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
  #9 (permalink)  
Old February 2nd, 2007, 09:27 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar that did the trick.

I knew it would be possible to check this value within the stored proc but my knowledge of tsql is still quite low

Due to the fact that when an item is permanently deleted the lstStatus dropdown will contain 0, I think I will programatically change the message that appears on_clientclick. This will make the user aware that they are now permanently deleting the item.

Thanks for all your help

retro:D

  #10 (permalink)  
Old February 3rd, 2007, 04:55 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You're welcome.
Quote:
quote:I think I will programatically change the message that appears on_clientclick.
Yeah, I think they'll appreciate that. Just deleting stuff directly isn't going to make your users happy... ;)

Good luck

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# Wrox CMS Code jminatel BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 5 June 3rd, 2009 04:41 AM
Wrox CMS in2minds BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 October 15th, 2008 04:14 PM
Wrox CMS - Altering the menu controls Eriskyne BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 October 15th, 2007 12:34 PM
Wrox CMS in C# .Netster BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 10 February 26th, 2007 04:56 PM
Wrox CMS retroviz BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 5 January 31st, 2007 01:23 PM



All times are GMT -4. The time now is 01:48 PM.


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