Results 1 to 8 of 8
  1. #1
    Lounger
    Join Date
    May 2006
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VB.net : Using label controls in a repeater? (VB.net)

    First post on the board, hopefully someone is able to help me out here [img]/forums/images/smilies/smile.gif[/img]
    So I am doing a site in VB.net... sort of a learning experience as I have never really done .net before... and I have come upon an odd problem. I am no stranger to web programming (mostly php) but am pretty new to asp.net... Here's the scenario:

    I have one main page, 3 different types of users. Depending on which type of user is logged in, the page will display different options. Sounds simple.

    Currently, I have the results displayed using a repeater control. My table should basically look like this:
    Name ... Address ... DateModified ... CONTACT ... EDIT ... DELETE

    Obviously, regular users should see "name", "address", "datemodified", and "contact", but not "edit" or "delete".
    Administrator users should see all links, including "edit" and "delete". For example, the links may point to "delete.aspx?id=14" or "edit.aspx?id=14", where 14 is the ID field in the database.

    This works fine and dandy when I need the links to display for *all* users, (that is, when the item template is just hardcoded), but I have yet to find a way to change the item template to display the "privelaged" links depending on who is logged in.
    I thought that it would be possible to use another type of control inside of the repeater, but it turns out that it is not an option... (dang)

    Should I just scrap the repeater control and take a different approach, or is there a simple way to do this? I'm still used to the php-style spaghetti-code approach, and this added level of abstraction is killing me [img]/forums/images/smilies/smile.gif[/img]

    Thanks ahead of time for any sort of direction on this one.

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VB.net : Using label controls in a repeater? (VB.net)

    Hi Lancer - welcome to Woody's Lounge!

    Your problem can be solved by using the ItemDataBound event of the repeater control. You can check the level of the user during the ItemDataBound event then dynamically show or hide the desired controls.

    The functionality in the Repeater will be very similar to that of the DataGrid. Here's an example: http://aspnet.4guysfromrolla.com/articles/020503-1.aspx

    The basic gist of what you'll need to write in that method is:
    - Check item type to make sure it's not header or footer
    - Get reference to the control by like this (depending on control type): Dim link as Hyperlink = (Hyperlink)e.Item("YourHyperlinkName")
    - Check user type
    - Set the visible property depending on your user type (link.Visible = True/False)

    Hang in there - once you get used to the event-driven nature of .NET, you'll LOVE it!!

    Hope this helps!

  3. #3
    Lounger
    Join Date
    May 2006
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VB.net : Using label controls in a repeater? (VB.net)

    Hey, thanks for the quick response, it seems like it will work, except I am having a bit of a hangup in the new method... so far I have this:

    Sub CheckPrivs(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    Dim link as Hyperlink =(Hyperlink)e.Item("DeleteLink")
    [...]
    End If
    End Sub

    When I swapped out "YourHyperlinkName" for "DeleteLink", as I have in the repeater, VStudio tells me that it thinks the statement on line 3 should end right after (Hyperlink) and before e.Item...
    My troubleshooting abilities are minimal in that situation, but everything else seems to be working as planned...!

  4. #4
    Lounger
    Join Date
    May 2006
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VB.net : Using label controls in a repeater? (VB.net)

    On third thought, it turns out everything is OK after all [img]/forums/images/smilies/smile.gif[/img]

    I think I got a little copy-and-paste happy as you can see from my fubar of the header for the CheckPrivs method....
    when one changes it to read "e as RepeaterItemEventArgs" (duh), that helps quite a bit.
    then, referring to the hyperlink as "e.Item.DataItem("DeleteLink")" is perfectly valid.

    Thanks again for the help!

  5. #5
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VB.net : Using label controls in a repeater? (VB.net)

    Be careful with custom method names for events. They can get really confusing if not done consistently.

    Are you using a codebehind file (.vb)? Or are you keeping your server code blocks in the ASPX?

    If you're using Visual Studio, you will probably want to make use of the codebehind. And if you're using codebehind, you should also consider using the default method names for event handlers. You can easily do this by not including the event name attribute in the ASPX page and using the drop-down boxes at the top of the VB screen to add the methods for the desired event (ItemDataBound, in this case). It should do something like "YourRepeater_ItemDataBound(....[arguments]) Handles YourRepeater.ItemDataBound"

    That will automatically add the correct argument types so you don't have to worry about RepeaterEventArgs vs. DataGridEventArgs, etc...

    Glad you've got it working!

  6. #6
    Lounger
    Join Date
    May 2006
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VB.net : Using label controls in a repeater? (VB.net)

    Yep, I am using visual studio and separate codebehind files...
    Per the example, my repeater control refers to my new method by stating OnItemDataBound="CheckPrivs"... but if VS makes it easier to do without making up a custom name, that would probably make better sense [img]/forums/images/smilies/smile.gif[/img]

    And now that I have been fiddling with it, it seems that I may still have a goofup...
    As before, VS wants this line to end right after (Hyperlink)...
    Dim link As HyperLink = (Hyperlink)e.Item.DataItem("deleteLink")

    But when (Hyperlink) is removed, this line appears to be OK:
    Dim link As HyperLink = e.Item.DataItem("deleteLink")

    but, when I load the page it tells me:
    deleteLink is neither a DataColumn nor a DataRelation for table DefaultView.

    So I guess I am still having trouble referring to my hyperlink control after all... I'll go fiddle with it some more and see if I can come up with anything [img]/forums/images/smilies/tongue.gif[/img]

  7. #7
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VB.net : Using label controls in a repeater? (VB.net)

    My apologies on the syntax mixup. I've been using C# for quite some time now and forgot that you have to cast objects differently in VB.NET.

    Try this: Dim link As Hyperlink = CType(e.Item.DataItem("deleteLink"),Hyperlink)

    Also, I think you might want to use the e.Item.FindControl() method rather than DataItem... If the above change doesn't fix it, try using FindControl() instead.

    I apologize for the lack of precision here - I should have prefixed my comments with the fact that I'm a bit rusty on some of these specifics - especially VB.NET.

    Looks like you're on the right track!

  8. #8
    Lounger
    Join Date
    May 2006
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VB.net : Using label controls in a repeater? (VB.net)

    Ah ha [img]/forums/images/smilies/smile.gif[/img]
    I was going to ask about the syntax, I just assumed it was some sort of typecast that I was unfamiliar with!

    Your rusty VB is far better than what I have to work with [img]/forums/images/smilies/wink.gif[/img]

    Thanks much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •