How to show EditButton in ListView depending logged on User?

Last post 11-02-2009 11:01 AM by lacito. 8 replies.

Sort Posts:

  • How to show EditButton in ListView depending logged on User?

    10-31-2009, 9:04 AM
    • Member
      2 point Member
    • lacito
    • Member since 10-31-2009, 8:51 AM
    • Posts 6

    I have a ListView that show all posts that users create. And when an Admin is logged on he can Edit all posts.

     protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
            {
                if (!IsPostBack)
                {
                    if (e.Item.ItemType == ListViewItemType.DataItem)
                    {
                        e.Item.FindControl("EditButton").Visible = Page.User.IsInRole("Admins");
                    }
                  
                }
            }


    But I don´t have any idea how to do when a user want to edit his own posts?

    So is there any easy way to show the editbutton depending logged in user?

  • Re: How to show EditButton in ListView depending logged on User?

    10-31-2009, 9:24 AM
    Answer
    • Member
      727 point Member
    • johnyM456
    • Member since 09-28-2009, 9:28 AM
    • Belgium
    • Posts 127

    It's in VB but fairly easy to translate to C#:

    This is what I use in combination with a repeater.

     

    Hope it help...

     

        Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
            Dim lbl As Label
            For Each item As RepeaterItem In rptMessages.Items
                lbl = item.FindControl("lbluser")
                If User.Identity.Name = lbl.Text Or User.IsInRole("Admin") Or User.IsInRole("Moderator") Then
                    item.FindControl("btnDelete").Visible = True
                    item.FindControl("btnEdit").Visible = True
                    item.FindControl("btnMove").Visible = True
                End If
            Next
        End Sub
    


     

  • Re: How to show EditButton in ListView depending logged on User?

    10-31-2009, 9:25 AM
    • Contributor
      5,852 point Contributor
    • irokhes
    • Member since 10-29-2009, 5:46 AM
    • Posts 857

    you can do this

    <asp:Button runat="server" ID="EditButton" Visible='<%# Isvisible() %>' />

    in codebehind

    protected bool IsVisible()
            {

    return Page.User.IsInRole("Admins");

    }


    good luck

    Please mark this post as answered if it helped you!

    mY BlOg
  • Re: How to show EditButton in ListView depending logged on User?

    10-31-2009, 11:59 AM
    • Member
      2 point Member
    • lacito
    • Member since 10-31-2009, 8:51 AM
    • Posts 6

    Thanks for the reply. But that didn´t help me. The codes you guys wrote did the same thing as the code I posted here.

    I want to know how to let the users edit their own posts. But not the others.

    If a user is logged in. He should see the edit button on his posts. But  he shouldn´t see the editbutton on the other posts that was posted by someone else.

    Like in this forum. You can see an editbutton on your posts, but not in my.


  • Re: How to show EditButton in ListView depending logged on User?

    11-01-2009, 8:39 AM
    • Member
      2 point Member
    • lacito
    • Member since 10-31-2009, 8:51 AM
    • Posts 6

    No one knows?

  • Re: How to show EditButton in ListView depending logged on User?

    11-01-2009, 12:38 PM
    Answer
    • Member
      727 point Member
    • johnyM456
    • Member since 09-28-2009, 9:28 AM
    • Belgium
    • Posts 127

    That's exactly what I wrote in my previous post. Put a label in the listview with the name of the user who posted the message.

    Then check if the label.text equals the username of the logged on user. If so, set the visibility of the button to true.

    The code I posted before does just that!

    Good luck, 

  • Re: How to show EditButton in ListView depending logged on User?

    11-01-2009, 4:25 PM
    • Member
      2 point Member
    • lacito
    • Member since 10-31-2009, 8:51 AM
    • Posts 6

    Ahh my bad. forgot to do Foreach loop. Thank you for the help.

    But it´s still not solved. Becuse I have 2 ListViews.

    And it works only for the first ListView.

    This is the code for ListView1 and it works.


    foreach (ListViewItem item in ListView1.Items)
                {
                    Label lbl = item.FindControl("lblUserName") as Label;


                    if (User.Identity.Name == lbl.Text.ToLower())
                    {
                        item.FindControl("EditButton").Visible = true;
                    }
                    else
                    {
                        item.FindControl("EditButton").Visible = false;
                    }

                }
              

    This code is for ListView2 and dosent work. Becuse ListView2.Items cant get any Items. Count = 0, all the time

    foreach (ListViewItem item in ListView2.Items)
                {
                    Label lbl = item.FindControl("lblUserName") as Label;


                    if (User.Identity.Name == lbl.Text.ToLower())
                    {
                        item.FindControl("EditButton").Visible = true;
                    }
                    else
                    {
                        item.FindControl("EditButton").Visible = false;
                    }

                }
              
    How can I fix it? Really stange. Becuse ListView1 is working like a charm.  



  • Re: How to show EditButton in ListView depending logged on User?

    11-01-2009, 8:43 PM
    • Contributor
      2,038 point Contributor
    • pons_saravanan
    • Member since 10-05-2006, 12:20 PM
    • Singapore
    • Posts 373

    you can do looping on second list view only after the data bound . So you have to bind the data first then try looping. then only the items will be created in the list view.

     

     

  • Re: How to show EditButton in ListView depending logged on User?

    11-02-2009, 11:01 AM
    • Member
      2 point Member
    • lacito
    • Member since 10-31-2009, 8:51 AM
    • Posts 6

    Thanks guys!

    Working great!


    Keep up the good work!

Page 1 of 1 (9 items)