p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 (http://p2p.wrox.com/forumdisplay.php?f=252)
-   -   Wrox Blog (http://p2p.wrox.com/showthread.php?t=57431)

tblessed23 May 3rd, 2007 01:08 PM

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


Imar May 3rd, 2007 02:57 PM

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.

tblessed23 May 3rd, 2007 09:36 PM

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


Imar May 4th, 2007 01:03 AM

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.

tblessed23 May 7th, 2007 02:49 PM

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


Imar May 7th, 2007 03:41 PM

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.

tblessed23 May 7th, 2007 04:59 PM

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



tblessed23 May 7th, 2007 05:05 PM

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


Imar May 8th, 2007 01:31 AM

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.

tblessed23 May 8th, 2007 09:42 PM

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



All times are GMT -4. The time now is 07:36 AM.

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