Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | 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 Display Modes
  #1 (permalink)  
Old May 3rd, 2007, 01:08 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Wrox Blog

Hello,

I have created user accounts for my users so I wanted to know how I could get the blog entry elements (after login) ex. edit this entry hyperlink. Unique to my user. SO that when in edit mode they are editing their out stuff and adding their own stuff.

Sincerely,
Computergirl

  #2 (permalink)  
Old May 3rd, 2007, 02:57 PM
Imar's Avatar
Wrox Author
Points: 71,632, Level: 100
Points: 71,632, Level: 100 Points: 71,632, Level: 100 Points: 71,632, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,013
Thanks: 79
Thanked 1,576 Times in 1,553 Posts
Default

Hi there,

A MembershipUser exposes a ProviderUserKey which is a Guid by default. When a user creates a Blog entry, you can get the ProviderUserKey from Membership.GetUser and store it in your BlogEntry.

Then when you display items you can show or hide the edit link depending on whether the the current item belongs to the current user.

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 May 3rd, 2007, 09:36 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get that but in which .vb to I put it under the BLog Entries, or Blog Entriese Filter. Exactly What do I write?

  #4 (permalink)  
Old May 4th, 2007, 01:03 AM
Imar's Avatar
Wrox Author
Points: 71,632, Level: 100
Points: 71,632, Level: 100 Points: 71,632, Level: 100 Points: 71,632, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,013
Thanks: 79
Thanked 1,576 Times in 1,553 Posts
Default

I think I would modify the BlogEntries control as that's the one that displays the actual entries. The Filter is only used to,..... filter ;)

Inside BlogEntries, you can handle the ItemCommand event of the DataList, use e.Item.FindControl to find your Edit link and then hide or display it depending on the user's rights.

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 May 7th, 2007, 02:49 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Imar,

This is what I wrote is it correct? If not, what is?

Protected Sub dlBlogEntries_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs ) Handles dlBlogEntries.ItemCommand
        If e.Item.FindControl("EditingId") Then ' Then
            Membership.GetUser().ProviderUserKey.ToString()
        End If
    End Sub

Sincerely,
Computergirl

  #6 (permalink)  
Old May 7th, 2007, 03:41 PM
Imar's Avatar
Wrox Author
Points: 71,632, Level: 100
Points: 71,632, Level: 100 Points: 71,632, Level: 100 Points: 71,632, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,013
Thanks: 79
Thanked 1,576 Times in 1,553 Posts
Default

If it works, than it's correct, isn't it? ;)

But no, I don't think this is correct.

Few things:

1. e.Item.FindControl("EditingId") returns a Control, not a Boolean indicating whether it can be found or not. So, you'll need to do something else with it to make this code more useful.

2. Membership.GetUser().ProviderUserKey.ToString() isn't doing anything. You grab the User, get its ID, convert the Guid to a string and that's it. You're not really using it anywhere.

3. You're not really hiding the Edit link.

I think I put you on the wrong track by saying you need to ItemCommand. It's ItemDataBound that you want. The following (untested) code should probably work:
Code:
Protected Sub dlBlogEntries_ItemDataBound(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
    Handles dlBlogEntries.ItemDataBound
  Dim myDataRowView As DataRowView = CType(e.Item.DataItem, DataRowView)
  Dim userId As Guid = CType(Membership.GetUser().ProviderUserKey, Guid)
  Dim posterId As Guid = CType(myDataRowView("PostedBy"), Guid)
  Dim lnkEdit As LinkButton = CType(e.Item.FindControl("lnkEdit"), LinkButton)
  If userId.Equals(userId) Then
    lnkEdit.Visible = False
  End If
End Sub
You also need to remove the Visible='<%#CanEdit()%>' from the lnkEdit control.

This code also assumes you have a column called PostedBy which holds the Guid of the user that posted the Blog entry.

I haven't tested or run this code, so you may need to tweak it a little to get it to work.

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 May 7th, 2007, 04:59 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. I keep getting this message "Object reference not set to an instance of an object." on This line "Dim userId As Guid = CType(Membership.GetUser().ProviderUserKey, Guid)" What do I need to do?

Computergirl


  #8 (permalink)  
Old May 7th, 2007, 05:05 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Imar,

Do I need to add an UserId column to the BlogEntry table in order to do this? or just change the ID column to an uniqueidentfire?

Computergirl

  #9 (permalink)  
Old May 8th, 2007, 01:31 AM
Imar's Avatar
Wrox Author
Points: 71,632, Level: 100
Points: 71,632, Level: 100 Points: 71,632, Level: 100 Points: 71,632, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,013
Thanks: 79
Thanked 1,576 Times in 1,553 Posts
Default

Quote:
quote:"Object reference not set to an instance of an object." on This line "Dim userId As Guid = CType(Membership.GetUser().ProviderUserKey, Guid)"
You're probably not logged on then.

Membership.GetUser() returns the current user when logged in or Nothing when you're not logged in. So, when you're not logged in, you try to get the ProviderUserKey from Nothing which won't work.
Quote:
quote:Do I need to add an UserId column to the BlogEntry table in order to do this? or just change the ID column to an uniqueidentfire?
You seem to be missing some fundamental knowledge about what it is that you want to do, and how to do it.

I think you need to take a step back from the code, take a piece of paper and write things down. Make a few diagrams, draw some classes that are shown in the book as well, and then decide what you need.

If you do that, you can answer questions like this one easily yourself. Think about the question: how would changing the Id column into a Guid help you? Are all your visitors only allowed to post once? How will you clearly identify unique BlogEntries when people can post more than one entry?

So the answer is: no, don't change it, but add it. That way, you have an Id column to uniquely identify the item in the table, and a PostedBy column to get the Guid of the MembershipUser that posted it.

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.
  #10 (permalink)  
Old May 8th, 2007, 09:42 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Imar,

Your help has been greatly appreciated! I have another question:
I get this message: Specified cast is not valid. On this line: Dim posterId As Guid = CType(myDataRowView("PostedBy"), Guid) How do I fix it.

Sincerely,
Computergirl

 


Thread Tools
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
Wrox Blog in C# madAlan BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 22 June 12th, 2011 04:09 AM
Wrox Blog: Viewing individual blog entries Tawanda BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 7 May 7th, 2007 12:06 PM
Login for Wrox Blog danielson BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 March 20th, 2007 02:12 PM
Wrox blog suggestions nakori BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 June 15th, 2006 04:35 PM
Wrox Blog Admin addstravel BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 April 23rd, 2006 05:41 AM



All times are GMT -4. The time now is 08:16 PM.


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