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

    Home » Forums » Developers, developers, developers » DevOps Lounge » VB.net : Using label controls in a repeater? (VB.net)

    • This topic has 7 replies, 2 voices, and was last updated 19 years ago.
    Author
    Topic
    #431831

    First post on the board, hopefully someone is able to help me out here
    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

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

    Viewing 0 reply threads
    Author
    Replies
    • #1011780

      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%5B/url%5D

      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!

      • #1011948

        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…!

        • #1011956

          On third thought, it turns out everything is OK after all 🙂

          I think I got a little copy-and-paste happy as you can see from my mess-up 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!

          • #1011959

            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!

            • #1011972

              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

              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

            • #1011981

              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!

            • #1011982

              Ah ha
              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

              Thanks much!

    Viewing 0 reply threads
    Reply To: VB.net : Using label controls in a repeater? (VB.net)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: